SQL Continuous Date Ranges Previously Grouped Data












0















I'm working with a data extract that represents claims data for a population. The ultimate goal is to be able to associate specific claims to unique encounters. The way we'd define an encounter is a string of claims that are continuous, or within the same range; meaning the claim-to date is followed up by a claim-from date immediately after, or has an identical to & from date as another claim. However, because there is no key that actually indicates that a group of claims represent a specific encounter, we have to implement logic that does this for us.



For the sake of this example, we've already grouped the data by Patient & Facility.



CREATE TABLE #BillingData (
ClaimID VARCHAR(MAX)
,Patient VARCHAR(MAX)
,Facility VARCHAR(MAX)
,ClaimFromDate DATE
,ClaimToDate DATE
,GroupID VARCHAR(MAX)
)

INSERT INTO #BillingData
VALUES
('Claim1','JOHN DOE','NURSING HOME','2000-01-01','2000-01-01','1')
,('Claim2','JOHN DOE','NURSING HOME','2000-01-01','2000-01-31','1')
,('Claim3','JOHN DOE','NURSING HOME','2000-01-01','2000-01-31','1')
,('Claim4','JOHN DOE','NURSING HOME','2000-01-01','2000-01-31','1')
,('Claim5','JOHN DOE','NURSING HOME','2000-02-01','2000-02-29','1')
,('Claim6','JOHN DOE','NURSING HOME','2000-03-01','2000-03-31','1')
,('Claim7','JOHN DOE','NURSING HOME','2000-04-01','2000-04-30','1')
,('Claim8','JOHN DOE','NURSING HOME','2000-08-01','2000-08-31','1')
,('Claim9','JOHN DOE','NURSING HOME','2000-09-01','2000-09-30','1')
,('Claim10','JANE DOE','HOME HEALTH','2000-02-01','2000-02-29','2')
,('Claim11','JANE DOE','HOME HEALTH','2000-03-01','2000-03-31','2')
,('Claim11','JANE DOE','HOME HEALTH','2000-03-01','2000-03-31','2')


What'd I'd like to see ultimately is an encounter ID. This is how the individual claims should be placed into encounter IDs:



Claims 1,2,3,4,5,6,7 are in Encounter 1.1;
Claims 8,9 are in Encounter 1.2;
Claims 10,11 are in Encounter 2.1



Any guidance would be greatly appreciated!










share|improve this question























  • What's your DBMS?

    – dnoeth
    Nov 14 '18 at 22:22











  • We're using MS SQL Server

    – kostr
    Nov 14 '18 at 22:26
















0















I'm working with a data extract that represents claims data for a population. The ultimate goal is to be able to associate specific claims to unique encounters. The way we'd define an encounter is a string of claims that are continuous, or within the same range; meaning the claim-to date is followed up by a claim-from date immediately after, or has an identical to & from date as another claim. However, because there is no key that actually indicates that a group of claims represent a specific encounter, we have to implement logic that does this for us.



For the sake of this example, we've already grouped the data by Patient & Facility.



CREATE TABLE #BillingData (
ClaimID VARCHAR(MAX)
,Patient VARCHAR(MAX)
,Facility VARCHAR(MAX)
,ClaimFromDate DATE
,ClaimToDate DATE
,GroupID VARCHAR(MAX)
)

INSERT INTO #BillingData
VALUES
('Claim1','JOHN DOE','NURSING HOME','2000-01-01','2000-01-01','1')
,('Claim2','JOHN DOE','NURSING HOME','2000-01-01','2000-01-31','1')
,('Claim3','JOHN DOE','NURSING HOME','2000-01-01','2000-01-31','1')
,('Claim4','JOHN DOE','NURSING HOME','2000-01-01','2000-01-31','1')
,('Claim5','JOHN DOE','NURSING HOME','2000-02-01','2000-02-29','1')
,('Claim6','JOHN DOE','NURSING HOME','2000-03-01','2000-03-31','1')
,('Claim7','JOHN DOE','NURSING HOME','2000-04-01','2000-04-30','1')
,('Claim8','JOHN DOE','NURSING HOME','2000-08-01','2000-08-31','1')
,('Claim9','JOHN DOE','NURSING HOME','2000-09-01','2000-09-30','1')
,('Claim10','JANE DOE','HOME HEALTH','2000-02-01','2000-02-29','2')
,('Claim11','JANE DOE','HOME HEALTH','2000-03-01','2000-03-31','2')
,('Claim11','JANE DOE','HOME HEALTH','2000-03-01','2000-03-31','2')


What'd I'd like to see ultimately is an encounter ID. This is how the individual claims should be placed into encounter IDs:



Claims 1,2,3,4,5,6,7 are in Encounter 1.1;
Claims 8,9 are in Encounter 1.2;
Claims 10,11 are in Encounter 2.1



Any guidance would be greatly appreciated!










share|improve this question























  • What's your DBMS?

    – dnoeth
    Nov 14 '18 at 22:22











  • We're using MS SQL Server

    – kostr
    Nov 14 '18 at 22:26














0












0








0








I'm working with a data extract that represents claims data for a population. The ultimate goal is to be able to associate specific claims to unique encounters. The way we'd define an encounter is a string of claims that are continuous, or within the same range; meaning the claim-to date is followed up by a claim-from date immediately after, or has an identical to & from date as another claim. However, because there is no key that actually indicates that a group of claims represent a specific encounter, we have to implement logic that does this for us.



For the sake of this example, we've already grouped the data by Patient & Facility.



CREATE TABLE #BillingData (
ClaimID VARCHAR(MAX)
,Patient VARCHAR(MAX)
,Facility VARCHAR(MAX)
,ClaimFromDate DATE
,ClaimToDate DATE
,GroupID VARCHAR(MAX)
)

INSERT INTO #BillingData
VALUES
('Claim1','JOHN DOE','NURSING HOME','2000-01-01','2000-01-01','1')
,('Claim2','JOHN DOE','NURSING HOME','2000-01-01','2000-01-31','1')
,('Claim3','JOHN DOE','NURSING HOME','2000-01-01','2000-01-31','1')
,('Claim4','JOHN DOE','NURSING HOME','2000-01-01','2000-01-31','1')
,('Claim5','JOHN DOE','NURSING HOME','2000-02-01','2000-02-29','1')
,('Claim6','JOHN DOE','NURSING HOME','2000-03-01','2000-03-31','1')
,('Claim7','JOHN DOE','NURSING HOME','2000-04-01','2000-04-30','1')
,('Claim8','JOHN DOE','NURSING HOME','2000-08-01','2000-08-31','1')
,('Claim9','JOHN DOE','NURSING HOME','2000-09-01','2000-09-30','1')
,('Claim10','JANE DOE','HOME HEALTH','2000-02-01','2000-02-29','2')
,('Claim11','JANE DOE','HOME HEALTH','2000-03-01','2000-03-31','2')
,('Claim11','JANE DOE','HOME HEALTH','2000-03-01','2000-03-31','2')


What'd I'd like to see ultimately is an encounter ID. This is how the individual claims should be placed into encounter IDs:



Claims 1,2,3,4,5,6,7 are in Encounter 1.1;
Claims 8,9 are in Encounter 1.2;
Claims 10,11 are in Encounter 2.1



Any guidance would be greatly appreciated!










share|improve this question














I'm working with a data extract that represents claims data for a population. The ultimate goal is to be able to associate specific claims to unique encounters. The way we'd define an encounter is a string of claims that are continuous, or within the same range; meaning the claim-to date is followed up by a claim-from date immediately after, or has an identical to & from date as another claim. However, because there is no key that actually indicates that a group of claims represent a specific encounter, we have to implement logic that does this for us.



For the sake of this example, we've already grouped the data by Patient & Facility.



CREATE TABLE #BillingData (
ClaimID VARCHAR(MAX)
,Patient VARCHAR(MAX)
,Facility VARCHAR(MAX)
,ClaimFromDate DATE
,ClaimToDate DATE
,GroupID VARCHAR(MAX)
)

INSERT INTO #BillingData
VALUES
('Claim1','JOHN DOE','NURSING HOME','2000-01-01','2000-01-01','1')
,('Claim2','JOHN DOE','NURSING HOME','2000-01-01','2000-01-31','1')
,('Claim3','JOHN DOE','NURSING HOME','2000-01-01','2000-01-31','1')
,('Claim4','JOHN DOE','NURSING HOME','2000-01-01','2000-01-31','1')
,('Claim5','JOHN DOE','NURSING HOME','2000-02-01','2000-02-29','1')
,('Claim6','JOHN DOE','NURSING HOME','2000-03-01','2000-03-31','1')
,('Claim7','JOHN DOE','NURSING HOME','2000-04-01','2000-04-30','1')
,('Claim8','JOHN DOE','NURSING HOME','2000-08-01','2000-08-31','1')
,('Claim9','JOHN DOE','NURSING HOME','2000-09-01','2000-09-30','1')
,('Claim10','JANE DOE','HOME HEALTH','2000-02-01','2000-02-29','2')
,('Claim11','JANE DOE','HOME HEALTH','2000-03-01','2000-03-31','2')
,('Claim11','JANE DOE','HOME HEALTH','2000-03-01','2000-03-31','2')


What'd I'd like to see ultimately is an encounter ID. This is how the individual claims should be placed into encounter IDs:



Claims 1,2,3,4,5,6,7 are in Encounter 1.1;
Claims 8,9 are in Encounter 1.2;
Claims 10,11 are in Encounter 2.1



Any guidance would be greatly appreciated!







sql loops common-table-expression






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 22:13









kostrkostr

435310




435310













  • What's your DBMS?

    – dnoeth
    Nov 14 '18 at 22:22











  • We're using MS SQL Server

    – kostr
    Nov 14 '18 at 22:26



















  • What's your DBMS?

    – dnoeth
    Nov 14 '18 at 22:22











  • We're using MS SQL Server

    – kostr
    Nov 14 '18 at 22:26

















What's your DBMS?

– dnoeth
Nov 14 '18 at 22:22





What's your DBMS?

– dnoeth
Nov 14 '18 at 22:22













We're using MS SQL Server

– kostr
Nov 14 '18 at 22:26





We're using MS SQL Server

– kostr
Nov 14 '18 at 22:26












1 Answer
1






active

oldest

votes


















1














You need to find the gaps, i.e. the current start date is greater than any previous end date (plus one day)



with gaps as
(
select *,
case
when max(ClaimToDate) -- max previous end date
over (partition by Patient, Facility
order by ClaimFromDate, ClaimID
rows between unbounded preceding and 1 preceding)
< dateadd(day,-1,ClaimFromDate) -- compare to current start date
then 1 -- gap
else 0 -- overlapping ranges
end as flag
from #BillingData
)
select *,
sum(flag) -- cumulative sum over 0/1 to create encounter number
over (partition by Patient, Facility
order by ClaimFromDate, ClaimID
rows unbounded preceding) +1 as encounter
from gaps
order by GroupID, encounter, ClaimFromDate


See fiddle






share|improve this answer
























  • Genius. Thanks for the quick response. Worked perfect!

    – kostr
    Nov 15 '18 at 3:21











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309531%2fsql-continuous-date-ranges-previously-grouped-data%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You need to find the gaps, i.e. the current start date is greater than any previous end date (plus one day)



with gaps as
(
select *,
case
when max(ClaimToDate) -- max previous end date
over (partition by Patient, Facility
order by ClaimFromDate, ClaimID
rows between unbounded preceding and 1 preceding)
< dateadd(day,-1,ClaimFromDate) -- compare to current start date
then 1 -- gap
else 0 -- overlapping ranges
end as flag
from #BillingData
)
select *,
sum(flag) -- cumulative sum over 0/1 to create encounter number
over (partition by Patient, Facility
order by ClaimFromDate, ClaimID
rows unbounded preceding) +1 as encounter
from gaps
order by GroupID, encounter, ClaimFromDate


See fiddle






share|improve this answer
























  • Genius. Thanks for the quick response. Worked perfect!

    – kostr
    Nov 15 '18 at 3:21
















1














You need to find the gaps, i.e. the current start date is greater than any previous end date (plus one day)



with gaps as
(
select *,
case
when max(ClaimToDate) -- max previous end date
over (partition by Patient, Facility
order by ClaimFromDate, ClaimID
rows between unbounded preceding and 1 preceding)
< dateadd(day,-1,ClaimFromDate) -- compare to current start date
then 1 -- gap
else 0 -- overlapping ranges
end as flag
from #BillingData
)
select *,
sum(flag) -- cumulative sum over 0/1 to create encounter number
over (partition by Patient, Facility
order by ClaimFromDate, ClaimID
rows unbounded preceding) +1 as encounter
from gaps
order by GroupID, encounter, ClaimFromDate


See fiddle






share|improve this answer
























  • Genius. Thanks for the quick response. Worked perfect!

    – kostr
    Nov 15 '18 at 3:21














1












1








1







You need to find the gaps, i.e. the current start date is greater than any previous end date (plus one day)



with gaps as
(
select *,
case
when max(ClaimToDate) -- max previous end date
over (partition by Patient, Facility
order by ClaimFromDate, ClaimID
rows between unbounded preceding and 1 preceding)
< dateadd(day,-1,ClaimFromDate) -- compare to current start date
then 1 -- gap
else 0 -- overlapping ranges
end as flag
from #BillingData
)
select *,
sum(flag) -- cumulative sum over 0/1 to create encounter number
over (partition by Patient, Facility
order by ClaimFromDate, ClaimID
rows unbounded preceding) +1 as encounter
from gaps
order by GroupID, encounter, ClaimFromDate


See fiddle






share|improve this answer













You need to find the gaps, i.e. the current start date is greater than any previous end date (plus one day)



with gaps as
(
select *,
case
when max(ClaimToDate) -- max previous end date
over (partition by Patient, Facility
order by ClaimFromDate, ClaimID
rows between unbounded preceding and 1 preceding)
< dateadd(day,-1,ClaimFromDate) -- compare to current start date
then 1 -- gap
else 0 -- overlapping ranges
end as flag
from #BillingData
)
select *,
sum(flag) -- cumulative sum over 0/1 to create encounter number
over (partition by Patient, Facility
order by ClaimFromDate, ClaimID
rows unbounded preceding) +1 as encounter
from gaps
order by GroupID, encounter, ClaimFromDate


See fiddle







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 23:13









dnoethdnoeth

45.5k31839




45.5k31839













  • Genius. Thanks for the quick response. Worked perfect!

    – kostr
    Nov 15 '18 at 3:21



















  • Genius. Thanks for the quick response. Worked perfect!

    – kostr
    Nov 15 '18 at 3:21

















Genius. Thanks for the quick response. Worked perfect!

– kostr
Nov 15 '18 at 3:21





Genius. Thanks for the quick response. Worked perfect!

– kostr
Nov 15 '18 at 3:21




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309531%2fsql-continuous-date-ranges-previously-grouped-data%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python