Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

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

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, December 18, 2014 2:34 AM
Points: 96, Visits: 455
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


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, December 18, 2014 1:43 AM
Points: 6,890, Visits: 14,254
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: Today @ 4:42 AM
Points: 3,440, Visits: 5,398
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!
Post #1525660
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse