February 5, 2018 at 11:22 am
Good Day
For the sake of simplicity, I just added one members records. What you see is this member had enrollment from months 6-12 in 2016.
Question; How can we know for sure if a member has had continuous enrollment ( you know what I mean ) for any 3 months in the year ( Select BeneficiaryID ........... from #t .............)
( Just to make it simple you can just count the records for 2016 .. I can figure out the rest )
Also this table can have other members as well. But lets figure out the SQl using this small set of data and we can go on from there.
Thanking you in advance. I hope I am clear about what I need.
if object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;
CREATE TABLE #t( BeneficiaryID VARCHAR(10), EligYear INT, EligMonth INT );
INSERT INTO #t(BeneficiaryID, EligYear, EligMonth )
Select '0068576102',2016,6 UNION
Select '0068576102',2016,7 UNION
Select '0068576102',2016,8 UNION
Select '0068576102',2016,9 UNION
Select '0068576102',2016,10 UNION
Select '0068576102',2016,11 UNION
Select '0068576102',2016,12 UNION
Select '0068576102',2017,10 UNION
Select '0068576102',2017,11 UNION
Select '0068576102',2017,12 ;
Select * FROM #t;
February 5, 2018 at 11:29 am
That's an Itzik Ben-Gap "Gaps and Islands" query. Sorry, I don't have time now to fully flesh it out, but someone should come along shortly who can.
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
February 5, 2018 at 12:01 pm
Managed to find the solution a similar solution in "STACK OVER FLOW" and then had to modify it slightly. So it works .. Here you go
;WITH T AS
(
SELECT *,
DENSE_RANK() OVER (ORDER BY EligMonth) - EligMonth AS Grp
FROM #t
),
CONSECUTIVE_MONTHS_TOGETHER as
(
SELECT
BeneficiaryID,
MIN(EligMonth) AS RangeStart,
MAX(EligMonth) AS RangeEnd,
MAX(EligMonth) - MIN(EligMonth) as DIFF
FROM T
GROUP BY BeneficiaryID, Grp
)
Select * FROM CONSECUTIVE_MONTHS_TOGETHER WHERE DIFF > 2 --( See Explanation )
/*
This solution works when the numbers are in a sequence ( that is the case with my example )
So if you had any 3 months of consecutive coverage the difference between the largest and smallest must be > 2 -- OR ( >= (3) )
*/
Here is the internet article that helped me....
https://stackoverflow.com/questions/7608370/how-can-i-check-a-group-of-numbers-are-consecutive-in-t-sql
CASE CLOSED ... NO FURTHER HELP NEEDED. Have a wonderful day .
February 5, 2018 at 12:08 pm
ScottPletcher - Monday, February 5, 2018 11:29 AMThat's an Itzik Ben-Gap "Gaps and Islands" query. Sorry, I don't have time now to fully flesh it out, but someone should come along shortly who can.
Or he could search for one of the multiple solutions available online with detailed explanations.
February 5, 2018 at 12:08 pm
ScottPletcher - Monday, February 5, 2018 11:29 AMThat's an Itzik Ben-Gap "Gaps and Islands" query. Sorry, I don't have time now to fully flesh it out, but someone should come along shortly who can.
Or he could search for one of the multiple solutions available online with detailed explanations.
February 5, 2018 at 12:45 pm
mw112009 - Monday, February 5, 2018 12:01 PMManaged to find the solution a similar solution in "STACK OVER FLOW" and then had to modify it slightly. So it works .. Here you go
;WITH T AS
(
SELECT *,
DENSE_RANK() OVER (ORDER BY EligMonth) - EligMonth AS Grp
FROM #t
),
CONSECUTIVE_MONTHS_TOGETHER as
(
SELECT
BeneficiaryID,
MIN(EligMonth) AS RangeStart,
MAX(EligMonth) AS RangeEnd,
MAX(EligMonth) - MIN(EligMonth) as DIFF
FROM T
GROUP BY BeneficiaryID, Grp
)
Select * FROM CONSECUTIVE_MONTHS_TOGETHER WHERE DIFF > 2 --( See Explanation )/*
This solution works when the numbers are in a sequence ( that is the case with my example )
So if you had any 3 months of consecutive coverage the difference between the largest and smallest must be > 2 -- OR ( >= (3) )
*/Here is the internet article that helped me....
https://stackoverflow.com/questions/7608370/how-can-i-check-a-group-of-numbers-are-consecutive-in-t-sqlCASE CLOSED ... NO FURTHER HELP NEEDED. Have a wonderful day .
THIS DOES NOT WORK. You haven't bothered to understand what this is doing, and you are not validating your results that you do get. For instance, with this amended data, there is no gap, but this so-called solution is saying that there is a gap of eleven months
INSERT INTO #t(BeneficiaryID, EligYear, EligMonth )
Select '0068576102',2016,6 UNION
Select '0068576102',2016,7 UNION
Select '0068576102',2016,8 UNION
Select '0068576102',2016,9 UNION
Select '0068576102',2016,10 UNION
Select '0068576102',2016,11 UNION
Select '0068576102',2016,12 UNION
Select '0068576102',2017,1 UNION
Select '0068576102',2017,2 UNION
Select '0068576102',2017,3 UNION
Select '0068571234',2017,4 UNION
Select '0068571234',2017,5
The reason that this code doesn't work is that it depends on numbers that are sequential, but you're IGNORING THE YEAR, which means that the month numbers are cyclical, not sequential.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
February 5, 2018 at 1:07 pm
Mr Drew: Agreed! Thanks for validating.
I did modify the query ( Also added more test data, Added 2 more users )
BTW - I am looking for at least 3 consecutive months in any given year.
Yes, this works when the numbers are sequential ( the difference between 2 consecutive numbers is one )
If object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;
CREATE TABLE #t( BeneficiaryID VARCHAR(10), EligYear INT, EligMonth INT );
INSERT INTO #t(BeneficiaryID, EligYear, EligMonth )
Select '0068576102',2016,3 UNION
Select '0068576102',2016,6 UNION
Select '0068576102',2016,7 UNION
Select '0068576102',2016,8 UNION
Select '0068576102',2016,9 UNION
Select '0068576102',2016,10 UNION
Select '0068576102',2016,11 UNION
Select '0068576102',2016,12 UNION
Select '0068576102',2017,10 UNION
Select '0068576102',2017,11 UNION
Select '0068576102',2017,12 UNION
Select '0078576103',2016,8 UNION
Select '0078576103',2016,9 UNION
Select '78576103',2016,8 UNION
Select '78576103',2016,9 UNION
Select '78576103',2016,10
;
WITH T AS
(
SELECT *,
DENSE_RANK() OVER (ORDER BY EligMonth) - EligMonth AS Grp
FROM #t
)
,
CONSECUTIVE_MONTHS_TOGETHER as
(
SELECT
BeneficiaryID,
EligYear,
MIN(EligMonth) AS RangeStart,
MAX(EligMonth) AS RangeEnd,
MAX(EligMonth) - MIN(EligMonth) as DIFF
FROM T
GROUP BY BeneficiaryID,EligYear, Grp
)
Select * FROM CONSECUTIVE_MONTHS_TOGETHER WHERE DIFF >= 2 --( See Explanation )
/*
This solution works when the numbers are in a sequence ( that is the case with my example )
So if you had any 3 months of consecutive coverage the difference between the largest and smallest must be > 1 -- OR ( >= (2) )
*/
February 5, 2018 at 1:10 pm
mw112009 - Monday, February 5, 2018 1:07 PMMr Drew: Agreed! Thanks for validating.
I did modify the query ( Also added more test data, Added 2 more users )
BTW - I am looking for at least 3 consecutive months in any given year.
Yes, this works when the numbers are sequential ( the difference between 2 consecutive numbers is one )
If object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;
CREATE TABLE #t( BeneficiaryID VARCHAR(10), EligYear INT, EligMonth INT );INSERT INTO #t(BeneficiaryID, EligYear, EligMonth )
Select '0068576102',2016,3 UNION
Select '0068576102',2016,6 UNION
Select '0068576102',2016,7 UNION
Select '0068576102',2016,8 UNION
Select '0068576102',2016,9 UNION
Select '0068576102',2016,10 UNION
Select '0068576102',2016,11 UNION
Select '0068576102',2016,12 UNION
Select '0068576102',2017,10 UNION
Select '0068576102',2017,11 UNION
Select '0068576102',2017,12 UNION
Select '0078576103',2016,8 UNION
Select '0078576103',2016,9 UNION
Select '78576103',2016,8 UNION
Select '78576103',2016,9 UNION
Select '78576103',2016,10;
WITH T AS
(
SELECT *,
DENSE_RANK() OVER (ORDER BY EligMonth) - EligMonth AS Grp
FROM #t
)
,
CONSECUTIVE_MONTHS_TOGETHER as
(
SELECT
BeneficiaryID,
EligYear,
MIN(EligMonth) AS RangeStart,
MAX(EligMonth) AS RangeEnd,
MAX(EligMonth) - MIN(EligMonth) as DIFF
FROM T
GROUP BY BeneficiaryID,EligYear, Grp
)
Select * FROM CONSECUTIVE_MONTHS_TOGETHER WHERE DIFF >= 2 --( See Explanation )/*
This solution works when the numbers are in a sequence ( that is the case with my example )
So if you had any 3 months of consecutive coverage the difference between the largest and smallest must be > 1 -- OR ( >= (2) )
*/
Please ignore this.. I will post another modified query.. I did notice another error...
February 5, 2018 at 1:31 pm
Thanks for all the input. Managed to get it to work.
So this time, it considers the year.
Run the code and you will see that 78576109 does not get counted ( He/she only has 2 months continuously )
--Select top 100 * FROM [EDW].[MEMBER].[MemberEligibilityByMonth] c where
--c.BeneficiaryID = '0068576102'
If object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;
CREATE TABLE #t( BeneficiaryID VARCHAR(10), EligYear INT, EligMonth INT );
INSERT INTO #t(BeneficiaryID, EligYear, EligMonth )
Select '0068576102',2016,3 UNION
Select '0068576102',2016,6 UNION
Select '0068576102',2016,7 UNION
Select '0068576102',2016,8 UNION
Select '0068576102',2016,9 UNION
Select '0068576102',2016,10 UNION
Select '0068576102',2016,11 UNION
Select '0068576102',2016,12 UNION
Select '0068576102',2017,10 UNION
Select '0068576102',2017,11 UNION
Select '0068576102',2017,12 UNION
Select '0078576103',2016,8 UNION
Select '0078576103',2016,9 UNION
Select '78576103',2016,8 UNION
Select '78576103',2016,9 UNION
Select '78576103',2016,10 UNION
Select '78576109',2016,8 UNION
Select '78576109',2016,9 UNION
Select '78576109',2016,11 UNION
Select '78576109',2016,12
;
WITH T AS
(
SELECT *
,DENSE_RANK() OVER (PARTITION BY BeneficiaryID, EligYear ORDER BY EligMonth) - EligMonth AS Grp2
--,DENSE_RANK() OVER (ORDER BY EligMonth) - EligMonth AS Grp
FROM #t
)
--Select * FROM T ORDER BY 1
,
CONSECUTIVE_MONTHS_TOGETHER as
(
SELECT
BeneficiaryID,
EligYear,
MIN(EligMonth) AS RangeStart,
MAX(EligMonth) AS RangeEnd,
MAX(EligMonth) - MIN(EligMonth) as DIFF
FROM T
GROUP BY BeneficiaryID,EligYear, Grp2
)
Select * FROM CONSECUTIVE_MONTHS_TOGETHER WHERE DIFF >= 2 --( See Explanation )
/*
This solution works when the numbers are in a sequence ( that is the case with my example )
So if you had any 3 months of consecutive coverage the difference between the largest and smallest must be > 1 -- OR ( >= (2) )
*/
February 5, 2018 at 1:39 pm
So you're okay with someone who has a gap that crosses years?
INSERT INTO #t(BeneficiaryID, EligYear, EligMonth )
Select '12345678',2016,6 UNION
Select '12345678',2016,7 UNION
Select '12345678',2016,8 UNION
Select '12345678',2016,9 UNION
Select '12345678',2016,10 UNION
Select '12345678',2017,3 UNION
Select '12345678',2017,4 UNION
Select '12345678',2017,5
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
February 5, 2018 at 2:25 pm
drew.allen - Monday, February 5, 2018 1:39 PMSo you're okay with someone who has a gap that crosses years?
INSERT INTO #t(BeneficiaryID, EligYear, EligMonth )
Select '12345678',2016,6 UNION
Select '12345678',2016,7 UNION
Select '12345678',2016,8 UNION
Select '12345678',2016,9 UNION
Select '12345678',2016,10 UNION
Select '12345678',2017,3 UNION
Select '12345678',2017,4 UNION
Select '12345678',2017,5Drew
Yep! The code will check for each member per year. So if a member has 3 months in 2 different years, her gets listed 2 times ( my requirement is to capture a member who has had 3 or more months in a given year )
February 5, 2018 at 2:52 pm
I did insert your sample records and ran the code, See attached. It works well.
If object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;
CREATE TABLE #t( BeneficiaryID VARCHAR(10), EligYear INT, EligMonth INT );
INSERT INTO #t(BeneficiaryID, EligYear, EligMonth )
Select '0068576102',2016,3 UNION
Select '0068576102',2016,6 UNION
Select '0068576102',2016,7 UNION
Select '0068576102',2016,8 UNION
Select '0068576102',2016,9 UNION
Select '0068576102',2016,10 UNION
Select '0068576102',2016,11 UNION
Select '0068576102',2016,12 UNION
Select '0068576102',2017,10 UNION
Select '0068576102',2017,11 UNION
Select '0068576102',2017,12 UNION
Select '0078576103',2016,8 UNION
Select '0078576103',2016,9 UNION
Select '78576103',2016,8 UNION
Select '78576103',2016,9 UNION
Select '78576103',2016,10 UNION
Select '78576109',2016,8 UNION
Select '78576109',2016,9 UNION
Select '78576109',2016,11 UNION
Select '78576109',2016,12;
INSERT INTO #t(BeneficiaryID, EligYear, EligMonth )
Select '12345678',2016,6 UNION
Select '12345678',2016,7 UNION
Select '12345678',2016,8 UNION
Select '12345678',2016,9 UNION
Select '12345678',2016,10 UNION
Select '12345678',2017,3 UNION
Select '12345678',2017,4 UNION
Select '12345678',2017,5
;
WITH T AS
(
SELECT *
,DENSE_RANK() OVER (PARTITION BY BeneficiaryID, EligYear ORDER BY EligMonth) - EligMonth AS Grp2
FROM #t
)
--Select * FROM T ORDER BY 1
,
CONSECUTIVE_MONTHS_TOGETHER as
(
SELECT
BeneficiaryID,
EligYear,
MIN(EligMonth) AS RangeStart,
MAX(EligMonth) AS RangeEnd,
MAX(EligMonth) - MIN(EligMonth) as DIFF
FROM T
GROUP BY BeneficiaryID,EligYear, Grp2
)
Select * FROM CONSECUTIVE_MONTHS_TOGETHER WHERE DIFF >= 2 --( See Explanation )
/*
This solution works when the numbers are in a sequence ( that is the case with my example )
So if you had any 3 months of consecutive coverage the difference between the largest and smallest must be > 1 -- OR ( >= (2) )
*/
February 13, 2018 at 4:02 pm
mw112009 - Monday, February 5, 2018 2:25 PMdrew.allen - Monday, February 5, 2018 1:39 PMSo you're okay with someone who has a gap that crosses years?
INSERT INTO #t(BeneficiaryID, EligYear, EligMonth )
Select '12345678',2016,6 UNION
Select '12345678',2016,7 UNION
Select '12345678',2016,8 UNION
Select '12345678',2016,9 UNION
Select '12345678',2016,10 UNION
Select '12345678',2017,3 UNION
Select '12345678',2017,4 UNION
Select '12345678',2017,5Drew
Yep! The code will check for each member per year. So if a member has 3 months in 2 different years, her gets listed 2 times ( my requirement is to capture a member who has had 3 or more months in a given year )
Mr Drew:
I thought about this...
I think what your asking is if a members enrollment spans across years can you find out whether he/she has enrollment for more than 3 or more months ? ( example: 2016/12,207/1,2017/2 will qualify for a 3 month span )
Yes, the code below takes care of it.....
If object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;
CREATE TABLE #t( BeneficiaryID VARCHAR(10), EligYear INT, EligMonth INT );
INSERT INTO #t(BeneficiaryID, EligYear, EligMonth )
Select '12345678',2016,6 UNION
Select '12345678',2016,7 UNION
Select '12345678',2016,8 UNION
Select '12345678',2016,9 UNION
Select '12345678',2016,10 UNION
Select '12345678',2017,2 UNION
Select '12345678',2017,3 UNION
Select '12345678',2017,4 UNION
Select '82345678',2016,6 UNION
Select '82345678',2016,7 UNION
Select '82345678',2016,8 UNION
Select '82345678',2016,9 UNION
Select '82345678',2016,10 UNION
Select '82345678',2016,11 UNION
Select '82345678',2016,12 UNION
Select '82345678',2017,1 UNION
Select '82345678',2017,2 UNION
Select '82345678',2017,3 UNION
Select '82345678',2017,4 UNION
Select '82345678',2017,5 UNION
Select '92345678',2016,12 UNION
Select '92345678',2017,1
;
With START_POINT as
(
Select
BeneficiaryID, MIN(EligYear) MIN_YEAR, MIN(EligMonth) MIN_MONTH
FROM #t
GROUP BY BeneficiaryID
)
,
MONTH_POSITION as
(
Select T.*, B.MIN_YEAR, B.MIN_MONTH,
((T.ELIGYear - B.MIN_YEAR )*(12) + (EligMonth)) as MONTH_SEQ
FROM
#t T
INNER JOIN START_POINT B ON ( B.BeneficiaryID = T.BeneficiaryID )
)
,
T AS
(
SELECT *
,DENSE_RANK() OVER (PARTITION BY BeneficiaryID ORDER BY MONTH_SEQ) - MONTH_SEQ AS Grp2
FROM MONTH_POSITION
)
,
CONSECUTIVE_MONTHS_TOGETHER as
(
SELECT
BeneficiaryID,
MIN(MONTH_SEQ) AS RangeStart,
MAX(MONTH_SEQ) AS RangeEnd,
MAX(MONTH_SEQ) - MIN(MONTH_SEQ) as DIFF
FROM T
GROUP BY BeneficiaryID, Grp2
)
Select *
FROM
CONSECUTIVE_MONTHS_TOGETHER
WHERE DIFF >= 2 -- Continuous Months for 3 or more
ORDER BY 1,2
February 14, 2018 at 6:31 am
The biggest problem with this whole thing is the notion of storing the year and month in separate columns instead of as whole dates that represent the first of the month. Any chance of you changing that or adding a persisted computed column?
--Jeff Moden
Change is inevitable... Change for the better is not.
February 14, 2018 at 7:40 am
Jeff Moden - Wednesday, February 14, 2018 6:31 AMThe biggest problem with this whole thing is the notion of storing the year and month in separate columns instead of as whole dates that represent the first of the month. Any chance of you changing that or adding a persisted computed column?
Couldn't you just multiply the year by 12 and add the months, then check for sequential numbers?
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply