Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

rollup with 1stdayofmonth, lastdayofmonth for yearly report Expand / Collapse
Posted Friday, December 20, 2013 4:53 AM


Group: General Forum Members
Last Login: 2 days ago @ 3:31 AM
Points: 154, Visits: 610
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.

Post #1524933
Posted Friday, December 20, 2013 6:48 AM

SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 9:41 AM
Points: 8,605, Visits: 18,775
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1524967
Posted Monday, December 23, 2013 5:42 PM

Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Wednesday, February 24, 2016 6:28 AM
Points: 3,977, Visits: 6,431
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
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs

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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1525660
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse