Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Advanced Pivot with totals rows X Columns

Author  Topic 

jooorj
Posting Yak Master

126 Posts

Posted - 2011-04-24 : 13:00:31
empno month1 month2 month3 all months
1001 162.333650 250.000000 450.000000 g
1002 300.000000 350.000000 500.000000 g
1003 200.000000 450.000000 600.000000 g
-----------------------------------------------------------
Total: 662.333650 1050.000000 1550.000000 g
------------------------------------------------------------
I want to replace all months (g) with total month1+month2+month3
I used the following pivot Script

select emp_no,[1] ,[2],[3],'g'
from(

select case when emp_no is null then
'Total:>' else
CAST (emp_no as nvarchar) end as emp_no
,months,sum(salary) as salary from AAA
group by cube( months, emp_no)

) sourced
pivot
(
sum(salary) for months
in([1],[2],[3])
)pvt

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-04-24 : 13:09:05
Duplicate
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=159834

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

jooorj
Posting Yak Master

126 Posts

Posted - 2011-04-24 : 13:10:56
sorry for duplicate it is not intended post.
I want to use a simple way, to generate a view then connect it with crystal report 2008.
I want to do it using Pivot,Compute By,grouping sets,cube,rollup methods.
thank you;
Go to Top of Page
   

- Advertisement -