返回具有单个值的列

问题描述 投票:0回答:1

我需要 sql 代码方面的帮助。

enter image description here

仅返回带有“采样”的 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
sql
1个回答
0
投票

您的

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;
© www.soinside.com 2019 - 2024. All rights reserved.