我有一个查询,显示来自客户的交易.我想做的是,只显示在一段时间内出现3次的结果.
例如:
如果我做: Select * from Table where dtcreated between @startdate and @enddate and result = 'Declined'
lcustomerid dtcreated result
1 8/1/15 Declined
1 8/2/15 Declined
1 8/3/15 Declined
2 8/1/15 Declined
2 8/2/15 Declined
2 8/3/15 Declined
3 8/1/15 Declined
3 8/3/15 Declined
4 8/1/15 Declined
Run Code Online (Sandbox Code Playgroud)
我想做的只是显示已经下降3次的那些.所以上面我只想看ID 1和2.
所以我正在寻找的结果是:
Select lcustomerid where dtcreated between @startdate and @enddate and result = 'Declined'
lcustomerid
1
2
Run Code Online (Sandbox Code Playgroud)
使用group by和having:
Select lcustomerid
from . . .
where dtcreated between @startdate and @enddate and result = 'Declined'
group by lcustomerid
having count(*) >= 3;
Run Code Online (Sandbox Code Playgroud)