标签: tsql
将表“摘要”简化为2列:CoID,类型
每个CoID可以有多种类型:
----------- CoID | Type ----------- 150 | 2 150 | 5 233 | 2 120 | 1 120 | 2
我想获取仅具有1种类型的CoID的数量。在这种情况下,CoID 233将是我唯一想要选择的。
谢谢!
答案 0 :(得分:0)
查看here
SELECT COUNT(Type), COID FROM Customers GROUP BY COID HAVING COUNT(TYPE) = 1;
答案 1 :(得分:0)
只需在Group by子句中使用Having进行过滤:
Group by
Having
SELECT COID, COUNT(Type) FROM SUMMARY GROUP BY COID HAVING COUNT(COID) = 1
输出:
COID | COUNT(Type) =====|================= 233 | 1
Live DEMO