SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help me with the SELECT statement please ?


Help me with the SELECT statement please ?

Author
Message
mw_sql_developer
mw_sql_developer
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13006 Visits: 1232
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;

ScottPletcher
ScottPletcher
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63514 Visits: 8699
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) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial:
If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.
mw_sql_developer
mw_sql_developer
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13006 Visits: 1232
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 .
Luis Cazares
Luis Cazares
SSC Guru
SSC Guru (126K reputation)SSC Guru (126K reputation)SSC Guru (126K reputation)SSC Guru (126K reputation)SSC Guru (126K reputation)SSC Guru (126K reputation)SSC Guru (126K reputation)SSC Guru (126K reputation)

Group: General Forum Members
Points: 126111 Visits: 21890
ScottPletcher - Monday, February 5, 2018 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.


Or he could search for one of the multiple solutions available online with detailed explanations.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Luis Cazares
Luis Cazares
SSC Guru
SSC Guru (126K reputation)SSC Guru (126K reputation)SSC Guru (126K reputation)SSC Guru (126K reputation)SSC Guru (126K reputation)SSC Guru (126K reputation)SSC Guru (126K reputation)SSC Guru (126K reputation)

Group: General Forum Members
Points: 126111 Visits: 21890
ScottPletcher - Monday, February 5, 2018 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.


Or he could search for one of the multiple solutions available online with detailed explanations.



Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
drew.allen
drew.allen
SSC-Forever
SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)

Group: General Forum Members
Points: 48877 Visits: 14720
mw112009 - Monday, February 5, 2018 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 .

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
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
mw_sql_developer
mw_sql_developer
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13006 Visits: 1232
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) )
*/

mw_sql_developer
mw_sql_developer
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13006 Visits: 1232
mw112009 - Monday, February 5, 2018 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) )
*/

Please ignore this.. I will post another modified query.. I did notice another error...

mw_sql_developer
mw_sql_developer
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13006 Visits: 1232
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) )
*/














drew.allen
drew.allen
SSC-Forever
SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)

Group: General Forum Members
Points: 48877 Visits: 14720
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
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search