|
Hi, I thought I posted this question last week but cannot find trace so apologies if I duplicate it: I have a requirement to total across a row but only columns in a user specified range. My results may require a grand total of multiple rows depending on the user parameters. The user wishes to run the report for example with the following criteria: Customer A ~ Customer A, Year/Period: 0810 ~ 0906 Therefore not only do I need only total across the row for year 08 for period 10 to 12 only but then add to it the total of period columns 1-6 from the row year 09 Eg dataset:
Result output = 20 I’m not much kop at in depth sql; a few static columns totalled across the row is my level; so I’m hoping some nice kind expert out there is able & willing to point me in the right direction. I will probably need apply any sql to a crystal report which I think is the end users main aim and from where the user parameters will be parsed from. Just seen the text formatting of the resultant question, sorry bout that but I'm sure you get the jist of my example data. Many thanks in advance
(comments are locked)
|
|
On SQL Server 2005 and above you can use UNPIVOT operator to rotate the columns into the rows and then you can simply select and sumarize the data. Here you have a complete solution.
For SQL 2000 you can achieve result using several UNIONS. Here you have the SQL 2000 solution Hi, many thanks Pavel, yes that works well on our sql2005 server but it's my own fault I didn't specify its our primary sql2000 server I have the issue with. Would have helped you wouldn't it. Can you assist? Thanks again for what was a super quick turnaround.
Mar 15 '10 at 10:14 AM
Richard 1
Added SQL server 2000 solution to the answer. Solution is simple by using several unions
Mar 15 '10 at 11:09 AM
Pavel Pawlowski
What a star, excellent, cheers :-)
Mar 15 '10 at 12:04 PM
Richard 1
(comments are locked)
|


Instead of 13 individual SELECTs being UNIONed together, there's a simple way to do this with a CROSS TAB but only if the data is properly normalized. You can have end of row totals, section subtotals, and grand totals by customer, year, etc, et.
So, let me ask... please post the CREATE TABLE statement along with some INSERT/SELECT's to populate the table and we'll give it a whirl. Or, you can use 13 SELECTs. ;-)