Samay
Samay

Reputation: 503

SQL Server: grouping on rows

I have data like below:

enter image description here

I want to group the rows for the same visitors having purchase = 1 and all their previous visits where purchase = 0. For the above data example, the rows should be grouped as:

How could this be achieved? I am using SQL Server 2012.

I am expecting output similar to below:

enter image description here

Code to generate the above data:

CREATE TABLE [#tmp_data]
(
    [visitor]       INT, 
    [visit_id]      INT, 
    [visit_time]    DATETIME, 
    [purchase]      BIT
);

INSERT INTO #tmp_data( visitor, visit_id, visit_time, purchase )
VALUES( 1, 1001, '2020-01-01 10:00:00', 0 ), 
( 1, 1002, '2020-01-02 11:00:00', 1 ), 
( 1, 1003, '2020-01-02 14:00:00', 1 ), 
( 2, 2001, '2020-01-01 10:00:00', 1 ), 
( 2, 2002, '2020-01-07 11:00:00', 0 ), 
( 2, 2003, '2020-01-08 14:00:00', 0 ), 
( 2, 2004, '2020-01-11 14:00:00', 1 );

Upvotes: 0

Views: 58

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269563

I'm not sure what you mean by "grouped". But your description of a grouping is the number of 1 values on or after a given value. So, this assigns a value per visitor

select td.*,
       sum(case when purchase = 1 then 1 else 0 end) over (partition by visitor order by visit_time desc) as grouping
from #tmp_data td;

This can be simplified to:

select td.*,
       sum( convert(int, purchase) ) over (partition by visitor order by visit_time desc) as grouping
from tmp_data td
order by visitor, visit_time;

Note: This just assigns a "grouping". You can aggregate however you want after that.

Here is a db<>fiddle.

Upvotes: 2

Related Questions