我想COUNT我的数据库中有多少条记录具有相同的标题,相同的User_ID并且发生在同一天。
Table_01:
------------------------------------------
EVENT_DATE | Title | User_ID
------------------------------------------
2014-08-28 00:00:00.000 | #0001 | 1111
2014-08-28 00:00:00.000 | #0001 | 1113
2014-08-28 00:00:00.000 | #0001 | 1113
2014-08-30 00:00:00.000 | #0001 | 1111
2014-08-03 00:00:00.000 | #0002 | 1111
2014-08-28 00:00:00.000 | #0001 | 1111
2014-08-15 00:00:00.000 | #0002 | 1111
2014-08-28 00:00:00.000 | #0004 | 1112
2014-08-30 00:00:00.000 | #0001 | 1111
2014-08-02 00:00:00.000 | #0001 | 1111
2014-08-28 00:00:00.000 | #0004 | 1112
2014-08-01 00:00:00.000 | #0003 | 1111
------------------------------------------
EVENT_DATE | Title | User_ID
------------------------------------------
2014-08-28 00:00:00.000 | #0001 | 1111
2014-08-28 00:00:00.000 | #0001 | 1111
2014-08-28 00:00:00.000 | #0001 | 1113
2014-08-28 00:00:00.000 | #0001 | 1113
2014-08-30 00:00:00.000 | #0001 | 1111
2014-08-30 00:00:00.000 | #0001 | 1111
2014-08-28 00:00:00.000 | #0004 | 1112
2014-08-28 00:00:00.000 | #0004 | 1112
2014-08-03 00:00:00.000 | #0002 | 1111
2014-08-15 00:00:00.000 | #0002 | 1111
2014-08-02 00:00:00.000 | #0001 | 1111
2014-08-01 00:00:00.000 | #0003 | 1111
所以答案是4,我不需要细节计数。
Microsoft SQL Server 2016.兼容级别:SQL Server 2005(90)
答案 0 :(得分:3)
如果我理解正确,你只需要两个级别的聚合:
ImageView
答案 1 :(得分:1)
这是一种计算至少有1个重复event_date,title和user_id的所有行的方法:
select count(*) from (
select row_number() over (partition by event_date, title, user_id) rn
from mytable
) t where rn = 2