【问题标题】:SQL consecutive occurrences countSQL 连续出现次数
【发布时间】:2013-09-11 04:39:33
【问题描述】:

我想根据某个时间段及其以下时间段的出现来显示条件的计数。

假设这是表格布局

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     | 

【问题讨论】:

    标签: sql sql-server


    【解决方案1】:

    我认为这正是 row_number() 所做的:

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

    【讨论】:

      猜你喜欢
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多