Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Performing calculations on subtotals


Performing calculations on subtotals

Author
Message
adam.berman
adam.berman
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 3
Hello: I'm a new user to SQL Server 2012 Standard. I have a table with information similar to what's found in the spreadsheet attached. I would like to get the total Pay calculated not just by department (e.g. total of CNA is 70.31) but then also have a total of the subtotals of some departments (but not all). For example, I'd like the total of LPN and RN together (both of which are types of nurses, so I want a 'nurse total') but not CNA. Is this possible to do through a SQL query, or another way in SSMS? I've read something about Calculated Members and cubes, is that the direction I have to head in, or can it be handled in SSMS? For the record, I'm using Yellowfin to run reports off of the data in SS 2012 and I can't find a way to create these calculations in YF, so I'm hoping to do it on the server end. I hope this is clear. thank you in advance.
Attachments
Payroll Record Example.xlsx (4 views, 9.00 KB)
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16550 Visits: 17004
Hi and welcome to the forums. The best chance you have of people helping is if you first post ddl and sample data in a consumable format. Most people won't even bother with your post if you post an excel doc. I took the risk this time. I have formatted your data into something consumable to demonstrate what I mean.


if object_id('tempdb..#PayData') is not null
drop table #PayData

create table #PayData
(
Dept int,
DeptDesc varchar(5),
PayDate datetime,
Pay numeric(7,2)
)

insert #PayData
select 006051, 'CNA', '2013-01-04', 18.53 union all
select 006051, 'CNA', '2013-01-04', 19.16 union all
select 003000, 'RN', '2013-01-04', 3.86 union all
select 003100, 'LPN', '2013-01-04', 11.54 union all
select 006051, 'CNA', '2013-01-04', 3.76 union all
select 005410, 'RN', '2013-01-04', 16.44 union all
select 003100, 'RN', '2013-01-04', 6.08 union all
select 005410, 'RN', '2013-01-04', 12.81 union all
select 805410, 'RN', '2013-01-04', 12.35 union all
select 006051, 'CNA', '2013-01-04', 15.02 union all
select 003000, 'LPN', '2013-01-04', 2.25 union all
select 003100, 'LPN', '2013-01-04', 10.03 union all
select 006051, 'CNA', '2013-01-04', 10.33 union all
select 006051, 'CNA', '2013-01-04', 3.51 union all
select 003000, 'RN', '2013-01-04', 11.91 union all
select 006050, 'LPN', '2013-01-04', 3.53 union all
select 806050, 'RN', '2013-01-04', 12.81 union all
select 006031, 'R.N.', '2013-01-04', 49.86 union all
select 006020, 'LPN', '2013-01-04', 5.67



Now I think that this query will produce the results you are looking for but from the brief description it is hard to say for sure.


select * from
(
select case when DeptDesc in ('LPN', 'RN', 'R.N.') then 'Nurse' else DeptDesc end as DeptDesc,
sum(Pay) over(partition by case when DeptDesc in ('LPN', 'RN', 'R.N.') then 'Nurse' else DeptDesc end ) as TotalPay
from #PayData
group by case when DeptDesc in ('LPN', 'RN', 'R.N.') then 'Nurse' else DeptDesc end, Pay
)x
group by DeptDesc, TotalPay



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
adam.berman
adam.berman
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 3
Thank you very much for "taking the risk" Wink My apologies for not posting the data is a different format.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16550 Visits: 17004
adam.berman (3/5/2013)
Thank you very much for "taking the risk" Wink My apologies for not posting the data is a different format.


Did that get you the desired results?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
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