简体   繁体   中英

Count consecutive dates in SQL as one instance

I would like to do a date count but count consecutive dates as one instance. In the example table below, 09/28/2013 and 09/29/2013 are consecutive therefore they count as one instance.

user_id      date   
------       ------ 
ABC123       09/28/2013
ABC123       09/29/2013
ABC123       09/30/2013
ABC123       10/01/2013

Output:

user_id      date_count   
------       ------ 
ABC123       3

This was taken from Sean Lange comment. His link ( HERE ) was spot on to what you need. The final code from the link was...

WITH
cteGroupedDates AS
( --=== Find the unique dates and assign them to a group.
 -- The group looks like a date but the date means nothing except that adjacent
 -- dates will be a part of the same group.
SELECT UniqueDate = SomeDate,
    DateGroup  = DATEADD(dd, - ROW_NUMBER() OVER (ORDER BY SomeDate), SomeDate)
FROM #MyHead
GROUP BY SomeDate
)
--===== Now, if we find the MIN and MAX date for each DateGroup, we'll have the
 -- Start and End dates of each group of contiguous daes.  While we're at it,
 -- we can also figure out how many days are in each range of days.
SELECT StartDate = MIN(UniqueDate),
    EndDate   = MAX(UniqueDate),
    Days      = DATEDIFF(dd,MIN(UniqueDate),MAX(UniqueDate))+1
FROM cteGroupedDates
GROUP BY DateGroup
ORDER BY StartDate
;

Making some name changes to try and make it easier to understand...

WITH
dateGroup AS
( --This is used to distinguish the different continuous sets of dates    
SELECT UniqueDate = date,
    DateGroup  = DATEADD(dd, - ROW_NUMBER() OVER (ORDER BY date), date)
FROM userdate
GROUP BY date
)

--Using dateGroup to get the groups of dates we can utilize it to get the count for them
SELECT StartDate = MIN(UniqueDate),
    EndDate   = MAX(UniqueDate),
    Days      = DATEDIFF(dd,MIN(UniqueDate),MAX(UniqueDate))+1,
    u.user_id
FROM dateGroup JOIN userdate u ON u.date = UniqueDate
GROUP BY DateGroup, u.user_id
ORDER BY StartDate
;

I added JOIN userdate u ON u.date = UniqueDate after the FROM dateCount to get the user ID. Also added u.user_id to the group by. Wouldn't work because u.user_id is in the SELECT (need information in the SELECT in the GROUP BY).

Data From Table:

表中的数据

Proof:

证明

----------EDIT 1----------

I am going to take a guess at what you truly want!

This is what I came up. Two different queries both with the same results.

First query:

WITH
dateGroup AS
( --This is used to distinguish the different continuous sets of dates    
SELECT UniqueDate = date,
DateGroup  = DATEADD(dd, - ROW_NUMBER() OVER (ORDER BY date), date),
user_id
FROM userdate
GROUP BY date, user_id
),
userIDGroup AS
( --This is used to get the previous table that you saw in my original answer
SELECT COUNT(d.user_id) as 'Instances',
d.user_id
FROM dateGroup d
GROUP BY DateGroup, d.user_id
)

SELECT
COUNT(u.user_id) AS 'Instances',
u.user_id
FROM userIDGroup u
GROUP BY u.user_id
;

Second Query (the one that I also prefer):

WITH
dateGroup AS
( --This is used to distinguish the different continuous sets of dates    
SELECT UniqueDate = date,
DateGroup  = DATEADD(dd, - ROW_NUMBER() OVER (ORDER BY date), date),
user_id
FROM userdate
GROUP BY date, user_id
)

SELECT count(c.user_id) AS 'Instances', c.user_id
FROM
(
SELECT COUNT(d.user_id) as 'Instances',
d.user_id
FROM dateGroup d
GROUP BY DateGroup, d.user_id
) c GROUP BY c.user_id
;

Proof:

在此处输入图片说明

Calling your source table users_and_dates , the below will drop any date where the next day is also in the data set.

select t1.user_id, 
    count(*)
from users_and_dates t1
join users_and_dates t2
    on t1.user_id=t2.user_id
        and t1.date+1=t2.date
where t2.user_id is null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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