T-SQL将计数器添加到分组数据

时间:2019-04-02 16:50:56

标签: sql-server tsql

我试图产生一个查询,该查询显示一列,该列为每组数据集增加(计数)。结果的总体顺序无关紧要,除非出现必须按日期计数(最旧= 1),并且应为每组分组数据重置。这是一个示例表ProductInteractions。

+---------+------------+----------------+------------+
| User ID | Product ID | Date Purchased | Occurrence |
+---------+------------+----------------+------------+
| user15  | b1290      | 1/1/2012       |          1 |
| user15  | b1290      | 1/15/2013      |          2 |
| user15  | b1290      | 3/15/2019      |          3 |
| user15  | a7983      | 7/22/2017      |          1 |
| user2   | a7983      | 12/3/2015      |          1 |
| user2   | a7983      | 5/6/2016       |          2 |
| user3   | a7983      | 3/24/2017      |          1 |
+---------+------------+----------------+------------+

原始数据:

+---------+------------+-----------+
| User ID | Product ID |   Date    |
+---------+------------+-----------+
| user15  | b1290      | 1/1/2012  |
| user2   | a7983      | 5/6/2016  |
| user15  | b1290      | 3/15/2019 |
| user15  | a7983      | 7/22/2017 |
| user2   | a7983      | 12/3/2015 |
| user15  | b1290      | 1/15/2013 |
| user3   | a7983      | 3/24/2017 |
+---------+------------+-----------+

请注意,在上面的示例中,user15和产品b1290具有3个交互。重要的是,第一次出现应与初始交互日期联系在一起,随后的交互应按递增日期进行计数。

我相信查询的基本格式为:

    SELECT [User ID],
           [Product ID],
           [Date Purchased]
           -- Something here utilizing IDENTITY, maybe?
      FROM ProductInteractions
     GROUP BY [User ID],
              [Product ID];


2 个答案:

答案 0 :(得分:4)

使用 ROW_NUMBER()

下面是测试/验证脚本的代码:用您自己的表替换ProductInteractions

declare @ProductInteractions as table([User ID] varchar(50),[Product ID] varchar(50),[Date] datetime)
insert into @ProductInteractions values
('user15'  , 'b1290'      , '1/1/2012'  ),
('user2'   , 'a7983'      , '5/6/2016'  ),
('user15'  , 'b1290'      , '3/15/2019' ),
('user15'  , 'a7983'      , '7/22/2017' ),
('user2'   , 'a7983'      , '12/3/2015' ),
('user15'  , 'b1290'      , '1/15/2013' ),
('user3'   , 'a7983'      , '3/24/2017' )

select [User ID],[Product ID],[Date],
row_number() over(partition by [User ID],[Product ID] order by [date]) [occurence]
from @ProductInteractions order by [Product ID] desc

enter image description here

答案 1 :(得分:3)

一个简单的ROW_NUMBER就是完美的选择。

SELECT [User ID],
    [Product ID],
    [Date Purchased]
    , ROW_NUMBER() over(partition by [User ID], [Product ID] order by [Date Purchased])
FROM ProductInteractions
GROUP BY [User ID],
        [Product ID];