过滤计数会导致sql查询

时间:2012-07-10 19:24:50

标签: sql sql-server-2008 tsql

如何过滤下面查询中的计数列,只显示计数>的行数1?

select FirstName, COUNT(*) as [CountTickets] from tblParkingTickets
group by FirstName  
order by [CountTickets] desc

这不起作用:

select FirstName, COUNT(*) as [CountTickets] from tblParkingTickets
where [CountTickets] > 1
group by FirstName  
order by [CountTickets] desc

由于

2 个答案:

答案 0 :(得分:7)

HAVING

HAVING COUNT(*) > 1
分组前的

WHERE过滤器(因此COUNT并不意味着什么)。分组后HAVING过滤。

答案 1 :(得分:0)

select * from (select FirstName, COUNT(*) mycount from tblParkingTickets
group by FirstName  
order by 2 desc) where mycount > 1

试试这个