SQL连续出现次数计数
我想根据某个时间段及其以下时间段的条件出现情况显示该条件的计数 假设这是表格布局SQL连续出现次数计数,sql,sql-server,Sql,Sql Server,我想根据某个时间段及其以下时间段的条件出现情况显示该条件的计数 假设这是表格布局 Key1 | Key2 | Key3 | emailSent_Date | 08/01/2013| 0097A | 0097A | 07/01/2013 | 07/01/2013| 0097A | 0097A | 08/01/2013 | 06/01/2013
Key1 | Key2 | Key3 | emailSent_Date |
08/01/2013| 0097A | 0097A | 07/01/2013 |
07/01/2013| 0097A | 0097A | 08/01/2013 |
06/01/2013| 0097A | 0097A | 08/01/2013 |
所需输出:SQL查询以获取某个时间段及其以下时间段的key2和key3出现次数。计数必须是连续的
在这里,8月份的key2和key3出现了3次。但是,在7月份,key2和key3只出现了两次。对不起,我把事情弄复杂了
Key1 | Key2 | Key3 | emailSent_Date | Count |
08/01/2013| 0097A | 0097A | 07/01/2013 | 3 |
07/01/2013| 0097A | 0097A | 08/01/2013 | 2 |
06/01/2013| 0097A | 0097A | 08/01/2013 | 1 |
我认为这正是
row\u number()
所做的:
select key1, key2, key3, emailSent_Date,
row_number() over (partition by key2, key3 order by key1) as "Count"
from "table";