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.
| Author |
Topic |
|
baburk
Posting Yak Master
108 Posts |
Posted - 2009-03-23 : 08:18:07
|
| Hi,I want the table to be PIVOT and get the sum both row wise and column wise.DECLARE @Sample table(VHWSCode VARCHAR(50),[Month] VARCHAR(20),TotalHours VARCHAR(12))INSERT @SampleSELECT 'WE04', 'March', '52:22:00' UNION ALLSELECT 'WE04', 'APRIL', '62:18:00' UNION ALLSELECT 'WE05', 'March', '45:40:00' UNION ALLSELECT 'WE05', 'APRIL', '70:16:00'This is my input dataI want Output in the format ofVHWSCode Jan Feb Mar Apr .Dec TotalWE04 52:22:00 62:18:00 114.40WE05 45:40:00 70:16:00 115.56Total 98.02. 132.34 |
|
|
darkdusky
Aged Yak Warrior
591 Posts |
Posted - 2009-03-24 : 12:49:31
|
| here is sample:SELECT VHWSCode, Jan = Max(case when [Month] ='Jan' then TotalHours end) ,Apr= Max(case when [Month] ='April' then TotalHours end), Mar= Max(case when [Month] ='March' then TotalHours end )FROM SampletableGROUP BY VHWSCode |
 |
|
|
Jurgen.Asselman
Starting Member
1 Post |
Posted - 2009-04-21 : 03:31:39
|
| SELECT VHWSCode , [March] as [MARCH] , [APRIL] as [APRIL]FROM ( SELECT VHWSCode , [Month], TotalHours FROM @Sample ) As TPIVOT( MAX(TotalHours) FOR [Month] IN ([March], [APRIL])) As pvtORDER BY pvt.VHWSCode |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|
|
|
|