![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi there I'm trying to come up with generic version of stored procedure to generate reports for financial application. The company is divided into 4 hierarchical levels, let's say A,B,C and D. So we can have an atomic division like 134,253,384,489 (like an IP number). Now, based on user input in presentation tier I'd like to be able to genarate costs report grouped by some subset of those hierarchical levels, eg. user clicks on B and C, and gets the report of costs generated in whole company (all atomic units) grouped by B and C values of each unit's costs. So, provided I have a table with four columns designating organization unit (A,B,C,D) and a column with a cost (COST), the query should be equivalent to: select B,C, SUM(Cost) from Costs GROUP BY B, C What I need is a parameterized version which would take four boolean parameters (@A,@B,@C,@D) and group results only by columns with its corresponding parameter being true. So to achieve functionality of previous example, I would make the following call: GenericReport(FALSE,TRUE,TRUE,FALSE) I know the trick allowing parameterized WHERE clause ( WHERE (@A IS NULL OR A=@A) but I have no idea whether any similar functionality is possible with GROUP BY. As a last resort, maybe some dirty CASE would be possible listing all 2^4 = 16 possible values combinations? |
|
I'm using sql server 2k standard ed. thank you very much for any insight! HP PS. On mssql ng I've been suggested to use WITH CUBE clause, but it seems to be an overkill (listing all group by combinations possible each time) and from what I've seen it doesn't support SUM() aggregate anyway. |
#3
| |||
| |||
|
|
PS. On mssql ng I've been suggested to use WITH CUBE clause, but it seems to be an overkill (listing all group by combinations possible each time) and from what I've seen it doesn't support SUM() aggregate anyway. |
#4
| |||
| |||
|
|
PS. On mssql ng I've been suggested to use WITH CUBE clause, but it seems to be an overkill (listing all group by combinations possible each time) and from what I've seen it doesn't support SUM() aggregate anyway.CUBE does support the SUM aggregate(). You can filter the results in the HAVING clause so that only the required subset of results gets returned. |
#5
| ||||
| ||||
|
|
|CASE WHEN MAX(@B) = 'TRUE' THEN ',B' ELSE '' END |CASE WHEN MAX(@C) = 'TRUE' THEN ',C' ELSE '' END |CASE WHEN MAX(@D) = 'TRUE' THEN ',D' ELSE '' END ,2) AS Columns |
|
|CASE WHEN MAX(@B) = 'TRUE' THEN ','||MAX(B) ELSE '' END |CASE WHEN MAX(@C) = 'TRUE' THEN ','||MAX(C) ELSE '' END |CASE WHEN MAX(@D) = 'TRUE' THEN ','||MAX(D) ELSE '' END ,2) AS Column_Value |
|
|CASE WHEN @B = 'TRUE' THEN ', B' ELSE '' END |CASE WHEN @C = 'TRUE' THEN ', C' ELSE '' END |CASE WHEN @D = 'TRUE' THEN ', D' ELSE '' END ,3) AS Columns |
|
|CASE WHEN MAX(@B) = 'TRUE' THEN ', '||MAX(B) ELSE '' END |CASE WHEN MAX(@C) = 'TRUE' THEN ', '||MAX(C) ELSE '' END |CASE WHEN MAX(@D) = 'TRUE' THEN ', '||MAX(D) ELSE '' END |
#6
| |||
| |||
|
|
The company is divided into 4 hierarchical levels, let's say A,B,C and D. |
![]() |
| Thread Tools | |
| Display Modes | |
| |