SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Group a column into different subtotals


Group a column into different subtotals

Author
Message
Trybbe
Trybbe
SSC Veteran
SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)

Group: General Forum Members
Points: 283 Visits: 493
Hi

I know this might be a pretty obvious thing to many. I need help to split data in a column into two categories with subtotals before grouping them into one grand total.

I have data like this :

EmpLevel NumberOfstaff

Snr Mngmnt 6
Jnr Mngmnt 13
Non Mngmnt 30
Non Pmnt 10
Exclusions 5

I need to display it like this:

Snr Mngmnt 6
Jnr Mngmnt 13
Non Mngmnt 30

Sub Total : 49 ----- Sum of all three on top

Non Pmnt 10
Exclusions 5

Grant Total : 64 ----- Sum of subtotal + the two after it.

Please assist
David Burrows
David Burrows
SSCrazy Eights
SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)

Group: General Forum Members
Points: 9586 Visits: 9754

WITH cte (MainGroup,EmpLevel,NumberOfstaff) AS (SELECT CASE WHEN EmpLevel LIKE '%Mngmnt' THEN 1 ELSE 2 END,EmpLevel,NumberOfstaff FROM [table])
SELECT COALESCE(EmpLevel,CASE WHEN GROUPING(MainGroup) = 0 THEN 'Sub Total' ELSE 'Grand Total' END),SUM(NumberOfstaff) AS [NumberOfstaff]
FROM cte
GROUP BY MainGroup,EmpLevel WITH ROLLUP
HAVING NOT (GROUPING(MainGroup) = 0 AND GROUPING(EmpLevel) = 1 AND MainGroup = 2)




Far away is close at hand in the images of elsewhere.

Anon.


Tee Time
Tee Time
Say Hey Kid
Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)

Group: General Forum Members
Points: 671 Visits: 465
I would do this with a CASE statment in the SQL query, something like:
CASE WHEN EmpLevel IN ('Non Pmnt', 'Exclusions') THEN 'Temp' ELSE 'Regular' END AS EmployeeType
and then create a group in your report, using the EmployeeType field.
Trybbe
Trybbe
SSC Veteran
SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)

Group: General Forum Members
Points: 283 Visits: 493
Thanks a mil guys for your help. both your answers helped me a lot both on reporting services and on query side as well. I can even use this method on other reporting tools.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search