Help me with the SELECT statement please ?

  • mw_sql_developer

    SSCoach

    Points: 19436

    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

    SSC Guru

    Points: 97739

    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

    SSCoach

    Points: 19436

    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

    SSC Guru

    Points: 183476

    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

    SSC Guru

    Points: 183476

    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

    SSC Guru

    Points: 76386

    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[/url].How to Post Performance Problems[/url]

  • mw_sql_developer

    SSCoach

    Points: 19436

    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

    SSCoach

    Points: 19436

    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

    SSCoach

    Points: 19436

    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

    SSC Guru

    Points: 76386

    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[/url].How to Post Performance Problems[/url]

  • mw_sql_developer

    SSCoach

    Points: 19436

    drew.allen - Monday, February 5, 2018 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

    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 )

  • mw_sql_developer

    SSCoach

    Points: 19436

    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) )
    */

  • mw_sql_developer

    SSCoach

    Points: 19436

    mw112009 - Monday, February 5, 2018 2:25 PM

    drew.allen - Monday, February 5, 2018 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

    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

  • Jeff Moden

    SSC Guru

    Points: 993381

    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


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
    "If you think its expensive to hire a professional to do the job, wait until you hire an amateur."--Red Adair
    "Change is inevitable... change for the better is not."
    When you put the right degree of spin on it, the number 3|8 is also a glyph that describes the nature of a DBAs job. 😉

    Helpful Links:
    How to post code problems

  • ScottPletcher

    SSC Guru

    Points: 97739

    Jeff Moden - Wednesday, February 14, 2018 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?

    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) 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.

Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic. Login to reply