GROUP BY带有额外的过滤器

时间:2017-06-13 20:44:39

标签: sql-server

disposition    uniqueid
ANSWERED       1497366029.125604
NO ANSWER      1497366029.125604
NO ANSWER      1497366029.125604
NO ANSWER      1497368123.126371
NO ANSWER      1497368123.126371
ANSWERED       1497368123.126371
NO ANSWER      1497369528.126781
NO ANSWER      1497369528.126781
NO ANSWER      1497369528.126781

SELECT disposition, uniqueid
FROM cdr
GROUP BY disposition,uniqueid

我只想返回“没有答案”的分组行,任何有一个或多个非“无答案”的唯一标识符'处置应该被忽略。

在此示例中,我尝试返回单行

NO ANSWER      1497369528.126781

我该怎么做?

由于

2 个答案:

答案 0 :(得分:0)

一种方法是

SELECT MAX(disposition) AS disposition,
       uniqueid
FROM   cdr
GROUP  BY uniqueid
HAVING SUM(CASE WHEN disposition = 'NO ANSWER' THEN 0 ELSE 1 END) = 0 

您也可以

SELECT uniqueid
FROM   cdr
WHERE disposition = 'NO ANSWER'
EXCEPT
SELECT uniqueid
FROM   cdr
WHERE disposition IS NULL OR  disposition <> 'NO ANSWER'

答案 1 :(得分:0)

使用'拥有'

尝试这种方式
select * 
from [cdr] 
Where disposition='NO ANSWER'
group by disposition,uniqueid
having count(uniqueid) <=1;