按编号> X的ID分组,其中日期> X

时间:2018-12-18 20:55:22

标签: sql sql-server

我正在尝试查询数据库,并返回在给定日期到现在之间出现五次以上的所有ID的列表。

以下代码是我产生错误的尝试。

Select id, res_date, count(id) as res_count
from reservations 
Where cancel = 0 and seated = 1 and id <> 0
group by id 
Having COUNT(id) >= 5 and res_date > 5/1/18   

1 个答案:

答案 0 :(得分:2)

您要在聚合之前 进行过滤:

Select id, count(id) as res_count
from reservations 
Where cancel = 0 and seated = 1 and id <> 0 and
      res_date >= '2018-05-01'
group by id 
Having count(*) >= 5;