简体   繁体   English

如何自我加入这个SQL

[英]How to self join this SQL

I have one table in my Database that has column names: buildNumber , result , versionStatus . 我的数据库中有一个表,该表的列名称为: buildNumberresultversionStatus 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 '. 现在在我的SQL语句中,我想显示“ buildNumber”,其中“ versionStatus”是旧的 ,“ result”是通过的 ,而versionStatus是新的 ,结果是“ 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. SQL语句应仅显示“ build2”,因为它以“ old”传递但现在以新版本“ failed”。

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 谢谢

Your existing query should work if you change the IN condition to be: 如果将IN条件更改为:您现有的查询应该可以工作:

and buildNumber IN (select CSAResults.dbo.Details.buildNumber

Alternatively, a better performing query might be: 或者,性能更好的查询可能是:

Select buildNumber
from CSAResults.dbo.Details
group by buildNumber
having count(distinct case
                          when result = 'pass' and versionStatus = 'Old' then 1
                          when result = 'fail' and versionStatus = 'New' then 2
                      end) = 2

This query does a self-joins of Details table to get the result you want. 该查询将对Details表进行自联接以获取所需的结果。

SELECT distinct new.buildNumber
FROM CSAResults.dbo.Details old
JOIN CSAResults.dbo.Details new ON old.buildNumber = new.buildNumber
WHERE old.result = 'pass'
  AND old.versionStatus = 'Old'
  AND new.result='fail'
  AND new.versionStatus='New'

I added the distinct in the select clause so you wouldn't get duplicate results if there were multiple old versions of the build that had passed 我在select子句中添加了distinct,因此如果有多个旧版本的构建已通过,则不会得到重复的结果

An alternative would be to use an INNER JOIN as so: 一种替代方法是按如下方式使用INNER JOIN:

select t.* 
from CSAResults.dbo.Details t INNER JOIN (SELECT t2.buildNumber
                                    FROM CSAResults.dbo.Details t2
                                    WHERE t2.versionStatus = 'New'                         
                                    and t2.result = 'fail') t1
                              ON t.buildNumber = t1.buildNumber
where t.result = 'pass' 
and t.versionStatus = 'Old' 

Nothing returned? 什么都没回来? Hardly surprising: you're trying to assert 不足为奇:您试图断言

(VersionStatus='New') = (VersionStatus='Old')

Try something like this instead 尝试这样的事情

select *
from CSAResults.dbo.Details
where result = 'pass'
and versionStatus = 'Old'
and buildNumber IN (select CSAResults.dbo.Details.buildNumber                           from CSAResults.dbo.Details
            where versionStatus = 'New'
            and result = 'fail')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM