How to self join this SQL?
-
I have one table in my Database that has column names: **buildNumber**, **result**, **versionStatus**. Now in my SQL statement I want to display the 'buildNumber' where the 'versionStatus' is **old** and the 'result' is **pass** and where the versionStatus is **new** and the result is **'fail**'. So I want to display anything that has a result of fail today but had a result of pass last time. So if I have these records in the DB (column separated by --): build2--pass--old build2--fail--new The SQL statement should only display "build2" because it passed with "old" but now 'failed' with new version. I have tried: select * from CSAResults.dbo.Details where result = 'pass' and versionStatus = 'Old' and versionStatus IN (select CSAResults.dbo.Details.versionStatus from CSAResults.dbo.Details where versionStatus = 'New' and result = 'fail') but nothing is returned. Thanks
-
Answer:
select * from CSAResults.dbo.Details where result = 'pass' and versionStatus = 'Old' and buildNumber in (select buildNumber from Details where VersionStatus = 'New' and result = 'Fail')
adamskii... at Yahoo! Answers Visit the source
Other answers
select * from CSAResults.dbo.Details a INNER JOIN CSAResults.dbo.Details b ON a.buildNumber=b.buildNumber AND a.result = 'pass' AND versionStatus = 'Old' AND b.result = 'fail' AND versionStatus = 'New'
SELECT o.buildNumber FROM someTable o, someTable n WHERE o.buildNumber = n.buildNumber AND o.versionStatus = "old" AND n.versionStatus = "new" AND o.result = "pass" AND n.result = "fail"
Related Q & A:
- How to Compare Rows in SQL?Best solution by Stack Overflow
- How to do GROUP_CONCAT in SQL Server?Best solution by Stack Overflow
- How to make only ONE Sql query?Best solution by Stack Overflow
- How to import XML into SQL Server database?Best solution by Stack Overflow
- How do I connect to SQL Server using C#?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.