rollup with 1stdayofmonth, lastdayofmonth for yearly report

  • It's hard to explain this:

    I have a table like this:

    value1 value2 date1 date2

    & I want to be able to go ...

    SELECT value1, count(value1)

    FROM table

    WHERE date1 <> null

    AND value2 <> 'blah'

    AND date2 BETWEEN (first_day_of_month AND last_day_of_month)

    GROUP BY value1

    & I need to be able to rollup the values so the final output is ...

    value1=1:count=42 for May: count=53 for June

    value1=2,count=32 for May: count=3 for June: ....

    Does that make any sense?

    Any clues gratefully accepted.

    πŸ™‚

  • snomad (12/20/2013)


    ...

    Any clues gratefully accepted.

    πŸ™‚

    A sample data set for folks here to run queries against would be gratefully accepted too! If you're not sure how to do this, click on the link in my signature below ("this article").

    β€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Looks to me like if you want to get your months as columns, you'll probably need to use a Cross tab query.

    Perhaps the clue that you seek is in one of these articles:

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns [/url]

    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs [/url]

    If that doesn't get you to that place you want to be, I suggest you follow Chris' advice.

    Merry Christmas!


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 3 posts - 1 through 2 (of 2 total)

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