我需要 sql 代码方面的帮助。
仅返回带有“采样”的 Snum,即 1、2、5。其他包括采样但也有其他测试名称的名称不应返回。
我使用了以下sql代码,但没有返回我想要的:
select s.Snum,t.TestName
from sample s
join test t on s.Snum = t.Snum
where t.ANALYSIS = 'Sampling'
group by s.TEXT_ID ,s.SAMPLE_NUMBER,t.TEST_NUMBER,t.ANALYSIS
having count(t.TestName) = 1
order by Snum
您的
HAVING
不正确。您只是计算行数。您需要计算不是 'Sampling'
的行数。
您的
group by
也有太多列。
select
s.Snum
from sample s
join test t on s.Snum = t.Snum
group by
s.Snum
having count(case when t.ANALYSIS <> 'Sampling' then 1 end) = 0;