简体   繁体   English

SQL连续出现次数

[英]SQL consecutive occurrences count

I want to display the count of a criteria based on its occurrence for a time period and its below time periods. 我想基于某个时间段及其以下时间段的出现次数来显示条件的计数。

Let's say this is the table layout 假设这是表格布局

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     | 

Desired output : SQL query to get the count of key2 and key3 occurrences for a time period and its below timperiods. 所需的输出:SQL查询以获取一段时间内及其以下时间段内key2和key3出现的次数。 The count has to be consecutive. 计数必须是连续的。

Here, for august month key2 and key3 occurred for 3 times. 在这里,8月的key2和key3发生了3次。 But, for the july month, key2 and key3 occurred for two times only. 但是,对于7月,key2和key3仅发生了两次。 Sorry if I made it complex. 对不起,如果我把它弄复杂了。

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     | 

I think this is exactly what row_number() does: 我认为这正是row_number()作用:

select key1, key2, key3, emailSent_Date,
       row_number() over (partition by key2, key3 order by key1) as "Count"
from "table";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM