SELECT statement issue, spread by YYYYMM

  • Ok the data..

    IF OBJECT_ID('tempdb..#tempData') IS NOT NULL DROP TABLE #tempData;

    CREATE TABLE #tempData(

    Code INT,

    Subs_End DATETIME

    )

    INSERT INTO #tempData VALUES (102,'2012-02-12 00:00:00.000')

    INSERT INTO #tempData VALUES (102,'2012-02-12 00:00:00.000')

    INSERT INTO #tempData VALUES (5,'2012-02-12 00:00:00.000')

    INSERT INTO #tempData VALUES (802,'2012-02-13 00:00:00.000')

    INSERT INTO #tempData VALUES (802,'2012-02-13 00:00:00.000')

    INSERT INTO #tempData VALUES (5,'2012-02-14 00:00:00.000')

    INSERT INTO #tempData VALUES (5,'2012-02-14 00:00:00.000')

    INSERT INTO #tempData VALUES (5,'2012-02-15 00:00:00.000')

    INSERT INTO #tempData VALUES (104,'2012-02-16 00:00:00.000')

    INSERT INTO #tempData VALUES (102,'2012-02-20 00:00:00.000')

    INSERT INTO #tempData VALUES (802,'2012-02-20 00:00:00.000')

    INSERT INTO #tempData VALUES (102,'2012-02-23 00:00:00.000')

    INSERT INTO #tempData VALUES (102,'2012-02-29 00:00:00.000')

    INSERT INTO #tempData VALUES (102,'2012-03-07 00:00:00.000')

    INSERT INTO #tempData VALUES (104,'2012-03-09 00:00:00.000')

    INSERT INTO #tempData VALUES (102,'2012-03-12 00:00:00.000')

    INSERT INTO #tempData VALUES (102,'2012-03-13 00:00:00.000')

    INSERT INTO #tempData VALUES (102,'2012-03-16 00:00:00.000')

    SELECT * FROM #tempData

    IF OBJECT_ID('tempdb..#tempData') IS NOT NULL DROP TABLE #tempData;

    I am trying to get this output, Current Month year is 201202, for 12 months to 201301

    Code 201202 201203 201204 201205 201206 201207 201208 .. 201301

    5 6 1

    102 4 4

    A horizontal spread by YYYYMM of the dates counted (total dates per each YYYYMM), with the spread always being a rolling 12 months out

    Any ideas:-)

  • To get all the month in the time range you need a calendar table (either a permanent or on-the-fly).

    For the Pivot I'd use the DynamicCrossTab approach as described in the related link in my signature.

    For a better understanding on how to use I'd recommend to start with the CrossTab article.

    Give it a try and post back if you have any further questions.



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Thanks, but that stuff hurts my head...:w00t:

    Any one else wishing help out much appreciated...

  • Digs (2/11/2012)


    Thanks, but that stuff hurts my head...:w00t:

    Any one else wishing help out much appreciated...

    Hmmm....

    Let's assume you'll get a coded version. How will you actually use it?

    Would you just put it into production? How would you explain to others how it works?



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Sir, I am an old fart working on a private project...just needed a little help !

    Thanks πŸ™‚

  • Digs (2/11/2012)


    Sir, I am an old fart working on a private project...just needed a little help !

    Thanks πŸ™‚

    Jeez, Digs. The 2nd Cross Tab article that Lutz pointed you to does what you need to do. Give it a try and stop complaining about how learning something new is hurting that tender ol' head of yours. πŸ˜‰

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

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Digs (2/11/2012)


    Sir, I am an old fart working on a private project...just needed a little help !

    Thanks πŸ™‚

    "A little help" in the meaning of "Give me some fish" or "I want to learn how to fish"?



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Whatever you do, just don’t mess-up with shark... :Whistling:

  • LutzM (2/12/2012)


    Digs (2/11/2012)


    Sir, I am an old fart working on a private project...just needed a little help !

    Thanks πŸ™‚

    "A little help" in the meaning of "Give me some fish" or "I want to learn how to fish"?

    Brings back somthing I heard while in the Bahamas.

    Give a man a fish and you feed him for a day. Teach him how to fish and you feed him for the remainder of his life.

    Spelling errors corrected (I hope)

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • bitbucket-25253 (2/12/2012)


    LutzM (2/12/2012)


    Digs (2/11/2012)


    Sir, I am an old fart working on a private project...just needed a little help !

    Thanks πŸ™‚

    "A little help" in the meaning of "Give me some fish" or "I want to learn how to fish"?

    Bromgs bacl a somting I heard while in the Bahamas.

    Give a man a fish and you feed him for a day. Teach him how to fish and you feed him for the remainder of his life.

    cant remember where I heard this .........

    "catch a man a fish and you can sell it to him"

    "teach a man to fish and you have lost a business opportunity"

    ________________________________________________________________
    you can lead a user to data....but you cannot make them think
    and remember....every day is a school day

  • J Livingston SQL (2/12/2012)


    ...

    "catch a man a fish and you can sell it to him"

    "teach a man to fish and you have lost a business opportunity"

    ROFL!!

    That's just great!

    Just another example of the effect of a different perspective.



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • my go..

    DECLARE @CurrentDate DATETIME

    SET @CurrentDate = GETUTCDATE()

    IF OBJECT_ID('tempdb..#tempData') IS NOT NULL DROP TABLE #tempData;

    CREATE TABLE #tempData(

    Code INT,

    Subs_End DATETIME

    )

    INSERT INTO #tempData VALUES (102,'2012-02-12 00:00:00.000')

    INSERT INTO #tempData VALUES (102,'2012-02-12 00:00:00.000')

    INSERT INTO #tempData VALUES (5,'2012-02-12 00:00:00.000')

    INSERT INTO #tempData VALUES (802,'2012-02-13 00:00:00.000')

    INSERT INTO #tempData VALUES (802,'2012-02-13 00:00:00.000')

    INSERT INTO #tempData VALUES (5,'2012-02-14 00:00:00.000')

    INSERT INTO #tempData VALUES (5,'2012-02-14 00:00:00.000')

    INSERT INTO #tempData VALUES (5,'2012-02-15 00:00:00.000')

    INSERT INTO #tempData VALUES (104,'2012-02-16 00:00:00.000')

    INSERT INTO #tempData VALUES (102,'2012-02-20 00:00:00.000')

    INSERT INTO #tempData VALUES (802,'2012-02-20 00:00:00.000')

    INSERT INTO #tempData VALUES (102,'2012-02-23 00:00:00.000')

    INSERT INTO #tempData VALUES (102,'2012-02-29 00:00:00.000')

    INSERT INTO #tempData VALUES (102,'2012-03-07 00:00:00.000')

    INSERT INTO #tempData VALUES (104,'2012-03-09 00:00:00.000')

    INSERT INTO #tempData VALUES (102,'2012-03-12 00:00:00.000')

    INSERT INTO #tempData VALUES (102,'2012-03-13 00:00:00.000')

    INSERT INTO #tempData VALUES (102,'2012-03-16 00:00:00.000')

    SELECT Code,

    COUNT(CASE WHEN YEAR(@CurrentDate)*100 + MONTH(@CurrentDate) = YEAR(Subs_End)*100 + MONTH(Subs_End) THEN 1 ELSE 0 END) AS '0'

    ,COUNT(CASE WHEN YEAR(DATEADD(month,1,@CurrentDate))*100 + DATEADD(month,1,@CurrentDate) = YEAR(Subs_End)*100 + MONTH(Subs_End) THEN 1 ELSE 0 END) AS '1'

    FROM #tempData

    GROUP BY Code

    IF OBJECT_ID('tempdb..#tempData') IS NOT NULL DROP TABLE #tempData;

    Code01

    544

    10299

    10422

    80233

    The column 1 is incorrect, why did it not pick up March 2012 dates ?

    Any ideas??:-)

    PS: Fish for breakfast, lunch and dinner it seams !

  • Here's a solution using a on-the-fly calendar table and the CrossTab method.

    If you need this solution as a "sliding window" I still recommend to use the CrossTab article referenced in my signature (to match the concept I used and the sample Jeff used in his article) and then continue and read the DynamicCrossTab article.

    WITH cte_calendar AS

    (

    SELECT TOP 12

    DATEADD(mm,number,'2012-01-01') AS month_from,

    DATEADD(mm,number+1,'2012-01-01') AS month_to

    FROM master..spt_values

    WHERE type='P'

    ORDER BY number

    ),

    cte_subresult as

    (

    SELECT

    month_from,

    MONTH(month_from) AS mnth,

    code,

    COUNT(*) AS cnt

    FROM cte_calendar

    LEFT OUTER JOIN #tempData ON month_from<=Subs_end AND month_to>Subs_end

    GROUP BY month_from, code

    )

    SELECT

    code,

    SUM(CASE WHEN mnth=1 THEN cnt ELSE 0 END ) AS [1],

    SUM(CASE WHEN mnth=2 THEN cnt ELSE 0 END ) AS [2],

    SUM(CASE WHEN mnth=3 THEN cnt ELSE 0 END ) AS [3],

    SUM(CASE WHEN mnth=4 THEN cnt ELSE 0 END ) AS [4],

    SUM(CASE WHEN mnth=5 THEN cnt ELSE 0 END ) AS [5],

    SUM(CASE WHEN mnth=6 THEN cnt ELSE 0 END ) AS [6],

    SUM(CASE WHEN mnth=7 THEN cnt ELSE 0 END ) AS [7]

    FROM cte_subresult

    WHERE code IS NOT NULL

    GROUP BY code



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • DECLARE @CurrentDate DATETIME

    SET @CurrentDate = GETUTCDATE()

    select @currentdate

    select YEAR(@CurrentDate) * 100 + MONTH(@CurrentDate)

    select YEAR(Dateadd(MONTH, 1, @CurrentDate)) * 100 + Dateadd(MONTH, 1, @CurrentDate)

    ________________________________________________________________
    you can lead a user to data....but you cannot make them think
    and remember....every day is a school day

  • Thanks Lutz, will play with that !

    Livingston SQL ; Dont understand your last post!

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

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