如何通过`count(*)`过滤intems而不用`having`?

时间:2015-08-04 13:15:26

标签: sql

我有一些疑问:

select disconnect_reason as disconnectReason, disconnect_cause, count(*) as callsCount 
from calls group by disconnect_reason, disconnect_cause 

我如何才能获得callCount = 1的项目 如何在没有having的情况下获得它?

3 个答案:

答案 0 :(得分:4)

总是可以使用Having子句来过滤组,但如果你想要另一种方式,那么试试这样的事情

select * from 
(
select disconnect_reason as disconnectReason, disconnect_cause, count(*) as callsCount 
from calls group by disconnect_reason, disconnect_cause
) A
Where callsCount = 1

答案 1 :(得分:1)

尝试使用 CTE 对于 MS SQL SERVER

with cte
as
(
select disconnect_reason as disconnectReason, disconnect_cause, count(*) as callsCount 
from calls group by disconnect_reason, disconnect_cause 
)
select * from cte where callsCount >0

答案 2 :(得分:1)

select *
from
(
select disconnect_reason as disconnectReason, disconnect_cause, count(*) as callsCount 
from calls group by disconnect_reason, disconnect_cause
)base
where  callsCount = 1