﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Harsha Majety  / Counting distinct periods of consecutive days / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 23 May 2013 20:07:38 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Counting distinct periods of consecutive days</title><link>http://www.sqlservercentral.com/Forums/Topic1292949-2846-1.aspx</link><description>Here's a good reference article. Group Islands of Contiguous Dates (SQL Spackle)[url]http://www.sqlservercentral.com/articles/T-SQL/71550/[/url]</description><pubDate>Sat, 06 Oct 2012 06:31:18 GMT</pubDate><dc:creator>a.myasnikov</dc:creator></item><item><title>RE: Counting distinct periods of consecutive days</title><link>http://www.sqlservercentral.com/Forums/Topic1292949-2846-1.aspx</link><description>[quote][b]jcrawf02 (10/4/2012)[/b][hr]report logic might have a slight flaw. I get why you'd do this for ER visits over midnight, but what if the patient comes in on Friday at noon, goes home and gets sick again or hit by a bus, shows up Saturday afternoon? Unless you're trying to treat these as root cause issues, and want to know that the 2nd visit was avoidable because the 1st didn't treat everything, which is very subjective. just something to think about, there's probably not a 100% correct answer to this one without clinical review[/quote]Actually, we do that quite often when handling claims. We call them interim claims and allow up to three days between when combining. It's far more common to have someone come back because of further complications rather than something unrelated.Harsha, I've found that when you given one report like that, you'll be asked to do it another way. For a DOJ project, I used this so it could be cut many ways.[code="sql"]IF OBJECT_ID('tempdb..#Claims') IS NOT NULL  DROP TABLE #ClaimsIF OBJECT_ID('tempdb..#ClaimSpans') IS NOT NULL  DROP TABLE #ClaimSpansCREATE TABLE #Claims (ClaimID int IDENTITY(1,1) PRIMARY KEY, PCN varchar(10), Admit date, Discharge date)INSERT INTO #Claims (PCN, Admit, Discharge)  VALUES     ('000000000A', '2012-01-01', '2012-01-03')    , ('000000000A', '2012-01-05', '2012-01-09')    , ('000000000A', '2012-01-10', '2012-01-11')    , ('000000000A', '2012-01-12', '2012-01-12')    , ('000000000A', '2012-01-14', '2012-01-19')    , ('000000000A', '2012-01-20', '2012-01-31')    , ('000000000B', '2012-01-05', '2012-01-09')    , ('000000000B', '2012-01-10', '2012-01-11')    , ('000000000B', '2012-02-05', '2012-02-09')    , ('000000000B', '2012-02-10', '2012-02-11')    , ('000000000B', '2012-02-12', '2012-02-14')    , ('000000000B', '2012-02-15', '2012-02-16')    , ('000000000B', '2012-02-27', '2012-02-28')    , ('000000000B', '2012-03-9', '2012-03-11')    , ('000000000C', '2012-03-05', '2012-03-09')    , ('000000000D', '2012-04-10', '2012-04-11');WITH Buckets (RowNum, PCN, Yr, Mnth, Admit) as(	SELECT 		ROW_NUMBER() OVER (PARTITION BY PCN ORDER BY PCN, Admit) as RowNum,		PCN,		YEAR(Admit) as Yr,		MONTH(Admit) as Mnth,		Admit	FROM #Claims	GROUP BY PCN, YEAR(Admit), MONTH(Admit), Admit)SELECT z.PCN, z.Yr, z.Mnth, z.Admit, y.Admit as PrevAdmit, DATEDIFF(d, y.Admit, z.Admit) as Days, z.RowNum, y.RowNum as PrevRowNumINTO #ClaimSpansFROM Buckets as zLEFT JOIN Buckets as y	ON z.RowNum - 1 = y.RowNum AND z.PCN = y.PCNSELECT * FROM #ClaimSpans-- find claims for patients that came back between 2-4 daysSELECT * FROM #ClaimSpans WHERE Days BETWEEN 2 and 4-- find the count of patients that started in one group below and either stayed within the same group or moved to another span;WITH Buckets (PCN, Bucket, Days, OldRowNum, RowNum)as(SELECT	PCN,	CASE		WHEN Days BETWEEN 0 AND 1 THEN '0 to 1'		WHEN Days BETWEEN 2 AND 4 THEN '2 to 4'		WHEN Days BETWEEN 4 AND 7 THEN '4 to 7'		WHEN Days BETWEEN 8 AND 10 THEN '8 to 10'		WHEN Days BETWEEN 11 AND 14 THEN '11 to 14'		ELSE '15 to 20' END as Bucket,	Days,	RowNum as OldRowNum, 	ROW_NUMBER() OVER (PARTITION BY PCN ORDER BY PCN, RowNum) as RowNum -- recalc RowNumFROM #ClaimSpansWHERE RowNum &amp;gt; 1 -- first claim won't join to a previous claim  AND Days &amp;lt;= 20 -- claims over 20 days won't count)SELECT	r.FromBucket,	r.ToBucket,	COUNT(*) as CntFROM(	SELECT		a.Bucket as FromBucket,		b.Bucket as ToBucket	FROM Buckets as a	JOIN Buckets as b		ON a.PCN = b.PCN AND a.RowNum = b.RowNum - 1 AND a.OldRowNum = b.OldRowNum - 1 -- OldRowNum makes sure that there wasn't a break in the sequence, like when the 20+ days were pulled) as r		GROUP BY FromBucket, ToBucket-- clean upIF OBJECT_ID('tempdb..#Claims') IS NOT NULL  DROP TABLE #ClaimsIF OBJECT_ID('tempdb..#ClaimSpans') IS NOT NULL  DROP TABLE #ClaimSpans[/code]</description><pubDate>Thu, 04 Oct 2012 21:49:32 GMT</pubDate><dc:creator>Gatekeeper</dc:creator></item><item><title>RE: Counting distinct periods of consecutive days</title><link>http://www.sqlservercentral.com/Forums/Topic1292949-2846-1.aspx</link><description>[quote][b]casey-172678 (10/4/2012)[/b][hr]It looks to me like your example counts the last day in a consecutive day grouping as a visit.  Here are some wrinkles:First, your data are pure dates.  I see this sort of problem more often with input data that are date/time.  Second, you are counting the visit on the last day.  This is all well and good if you're selecting from an unfiltered data set.  But what if you're needing to use this technique against a date range, say patient visits this month?  How would you set it up so that your results from querying each month in the year will logically match the results for the whole year, etc.[/quote]I think the script actually counts the number of days that don't have a preceding day, so it should work correctly if you want to select ranges of days for reports.So if you want the number of visits that started in Jan 2012 you would add[code="sql"]where tbl1.er_date between '1/1/2012' and '1/31/2012'[/code]</description><pubDate>Thu, 04 Oct 2012 15:53:44 GMT</pubDate><dc:creator>john.rees-894283</dc:creator></item><item><title>RE: Counting distinct periods of consecutive days</title><link>http://www.sqlservercentral.com/Forums/Topic1292949-2846-1.aspx</link><description>report logic might have a slight flaw. I get why you'd do this for ER visits over midnight, but what if the patient comes in on Friday at noon, goes home and gets sick again or hit by a bus, shows up Saturday afternoon? Unless you're trying to treat these as root cause issues, and want to know that the 2nd visit was avoidable because the 1st didn't treat everything, which is very subjective. just something to think about, there's probably not a 100% correct answer to this one without clinical review</description><pubDate>Thu, 04 Oct 2012 06:44:15 GMT</pubDate><dc:creator>jcrawf02</dc:creator></item><item><title>RE: Counting distinct periods of consecutive days</title><link>http://www.sqlservercentral.com/Forums/Topic1292949-2846-1.aspx</link><description>It looks to me like your example counts the last day in a consecutive day grouping as a visit.  Here are some wrinkles:First, your data are pure dates.  I see this sort of problem more often with input data that are date/time.  Second, you are counting the visit on the last day.  This is all well and good if you're selecting from an unfiltered data set.  But what if you're needing to use this technique against a date range, say patient visits this month?  How would you set it up so that your results from querying each month in the year will logically match the results for the whole year, etc.</description><pubDate>Thu, 04 Oct 2012 06:01:28 GMT</pubDate><dc:creator>casey-172678</dc:creator></item><item><title>Counting distinct periods of consecutive days</title><link>http://www.sqlservercentral.com/Forums/Topic1292949-2846-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/scripts/89882/"&gt;Counting distinct periods of consecutive days&lt;/A&gt;[/B]</description><pubDate>Tue, 01 May 2012 00:55:47 GMT</pubDate><dc:creator>harsha.majety</dc:creator></item></channel></rss>