我的数据库中有一个表有列名: buildNumber ,结果, versionStatus 。现在在我的SQL语句中,我想显示'buildNumber',其中'versionStatus'是旧,'result'是传递,而versionStatus是 new ,结果是'失败'。所以我想显示今天失败的结果,但上次传递的结果。所以如果我在DB中有这些记录(列用 - 分隔:):
build2--pass--old
build2--fail--new
SQL语句应该只显示“build2”,因为它以“旧”传递,但现在以新版本“失败”。
我试过了:
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')
但没有任何回报。
由于
答案 0 :(得分:2)
如果将IN条件更改为:
,则现有查询应该有效and buildNumber IN (select CSAResults.dbo.Details.buildNumber
或者,性能更好的查询可能是:
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
答案 1 :(得分:2)
此查询执行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'
我在select子句中添加了distinct,因此如果已经传递了多个旧版本的构建,则不会得到重复的结果
答案 2 :(得分:0)
另一种方法是使用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'
答案 3 :(得分:0)
什么都没有回来?不足为奇:你正试图断言
(VersionStatus='New') = (VersionStatus='Old')
尝试这样的事情
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')