Author |
Topic |
cplusplus
Aged Yak Warrior
567 Posts |
Posted - 2013-07-22 : 12:42:48
|
Is it possible, i want to present result in 3 columns only.by Dept, Period, totalperiod will show the month number based on value available in which period and the third column Total is the sum(per x) based on dept and period.when you run below query should present feb value as 3500 for dept 310. since per2 has two rows 2100 and 1400.Declare @Sample table (Dept int, per1 int, per2 int, per3 int,per4 int,per5 int,per6 int,per7 int,per8 int, per9 int, per10 int,per11 int,per12 int)insert @Sampleselect 310, 0,2100,0,0,0,0,0,0,0,0,0,0 union all -- feb month period value availableselect 310, 234,0,0,0,0,0,0,0,0,0,0,0 union all --jan month value availableselect 310, 0,0,765,0,0,0,0,0,0,0,0,0 union allselect 310, 0,0,0,5678,0,0,0,0,0,0,0,0 union allselect 310, 0,0,0,0,875,0,0,0,0,0,0,0 union allselect 310, 0,0,0,0,0,9800,0,0,0,0,0,0 union allselect 310, 0,0,0,0,0,0,9000,0,0,0,0,0 union allselect 310, 0,0,0,0,0,0,0,5600,0,0,0,0 union allselect 310, 0,0,0,0,0,0,0,0,770,0,0,0 union allselect 310, 0,0,0,0,0,0,0,0,0,8900,0,0 union allselect 310, 0,0,0,0,0,0,0,0,0,0,1400,0 union allselect 310, 0,1400,0,0,0,0,0,0,0,0,0,0 union allselect 310, 0,0,0,0,0,0,0,0,770,0,0,1950 select * from @Sample Thank you very much for the helpful info. |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-07-22 : 12:58:34
|
[code]-- SwePesoWITH cteSource(Dept, theMonth, theValue)AS ( SELECT u.Dept, --DATEADD(MONTH, CAST(SUBSTRING(u.theCol, 4, 2) AS TINYINT), '18991201') AS theMonth, CAST(SUBSTRING(u.theCol, 4, 2) AS TINYINT) AS theMonth, u.theValue FROM @Sample AS s UNPIVOT ( theValue FOR theCol IN (s.per1, s.per2, s.per3, s.per4, s.per5, s.per6, s.per7, s.per8, s.per9, s.per10, s.per11, s.per12) ) AS u)SELECT Dept, --CONVERT(CHAR(3), theMonth, 0) AS Period, theMonth AS Period, SUM(theValue) AS TotalFROM cteSourceGROUP BY Dept, theMonthORDER BY Dept, theMonth;[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-22 : 12:59:55
|
do mean this?Declare @Sample table (Dept int, per1 int, per2 int, per3 int,per4 int,per5 int,per6 int,per7 int,per8 int, per9 int, per10 int,per11 int,per12 int)insert @Sampleselect 310, 0,2100,0,0,0,0,0,0,0,0,0,0 union all -- feb month period value availableselect 310, 234,0,0,0,0,0,0,0,0,0,0,0 union all --jan month value availableselect 310, 0,0,765,0,0,0,0,0,0,0,0,0 union allselect 310, 0,0,0,5678,0,0,0,0,0,0,0,0 union allselect 310, 0,0,0,0,875,0,0,0,0,0,0,0 union allselect 310, 0,0,0,0,0,9800,0,0,0,0,0,0 union allselect 310, 0,0,0,0,0,0,9000,0,0,0,0,0 union allselect 310, 0,0,0,0,0,0,0,5600,0,0,0,0 union allselect 310, 0,0,0,0,0,0,0,0,770,0,0,0 union allselect 310, 0,0,0,0,0,0,0,0,0,8900,0,0 union allselect 310, 0,0,0,0,0,0,0,0,0,0,1400,0 union allselect 310, 0,1400,0,0,0,0,0,0,0,0,0,0 union allselect 310, 0,0,0,0,0,0,0,0,770,0,0,1950 select Dept,MONTH(DATEADD(mm,REPLACE(Period,'per','')-1,0)) AS [Month],SUM(Val) AS Totalfrom @Sampleunpivot (val for period in (per1,per2,per3,per4,per5,per6,per7,per8,per9,per10,per11,per12))uWHERE val >0GROUP BY Dept,MONTH(DATEADD(mm,REPLACE(Period,'per','')-1,0))ORDER BY Dept,[Month]output--------------------------------------------Dept Month Total310 1 234310 2 3500310 3 765310 4 5678310 5 875310 6 9800310 7 9000310 8 5600310 9 1540310 10 8900310 11 1400310 12 1950 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-07-22 : 13:02:09
|
[code]SELECT s.Dept, f.Period, SUM(f.theValue) AS TotalFROM @Sample AS sCROSS APPLY ( VALUES (1, 'Jan', s.per1), (2, 'Feb', s.per2), (3, 'Mar', s.per3), (4, 'Apr', s.per4), (5, 'May', s.per5), (6, 'Jun', s.per6), (7, 'Jul', s.per7), (8, 'Aug', s.per8), (9, 'Sep', s.per9), (10, 'Oct', s.per10), (11, 'Nov', s.per11), (12, 'Dec', s.per12) ) AS f(theSort, Period, theValue)GROUP BY s.Dept, f.theSort, f.PeriodORDER BY s.Dept, f.theSort;[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
cplusplus
Aged Yak Warrior
567 Posts |
Posted - 2013-07-22 : 13:05:06
|
Peso,Thanks a lot for the query, it is showing result wrong. Instead of feb it is showing 3500 for march.Declare @Sample table (Dept int, per1 int, per2 int, per3 int,per4 int,per5 int,per6 int,per7 int,per8 int, per9 int, per10 int,per11 int,per12 int)insert @Sampleselect 310, 0,2100,0,0,0,0,0,0,0,0,0,0 union all -- feb month period value availableselect 310, 234,0,0,0,0,0,0,0,0,0,0,0 union all --jan month value availableselect 310, 0,0,765,0,0,0,0,0,0,0,0,0 union allselect 310, 0,0,0,5678,0,0,0,0,0,0,0,0 union allselect 310, 0,0,0,0,875,0,0,0,0,0,0,0 union allselect 310, 0,0,0,0,0,9800,0,0,0,0,0,0 union allselect 310, 0,0,0,0,0,0,9000,0,0,0,0,0 union allselect 310, 0,0,0,0,0,0,0,5600,0,0,0,0 union allselect 310, 0,0,0,0,0,0,0,0,770,0,0,0 union allselect 310, 0,0,0,0,0,0,0,0,0,8900,0,0 union allselect 310, 0,0,0,0,0,0,0,0,0,0,1400,0 union allselect 310, 0,1400,0,0,0,0,0,0,0,0,0,0 union allselect 310, 0,0,0,0,0,0,0,0,770,0,0,1950;WITH cteSource(Dept, theMonth, theValue)AS ( SELECT u.Dept, DATEADD(MONTH, CAST(SUBSTRING(u.theCol, 4, 2) AS TINYINT), '19000101') AS theMonth, u.theValue FROM @Sample AS s UNPIVOT ( theValue FOR theCol IN (s.per1, s.per2, s.per3, s.per4, s.per5, s.per6, s.per7, s.per8, s.per9, s.per10, s.per11, s.per12) ) AS u)SELECT Dept, CONVERT(CHAR(3), theMonth, 0) AS Period, SUM(theValue) AS TotalFROM cteSourceGROUP BY Dept, theMonthORDER BY Dept, theMonth; THANK YOU VERY MUCH. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-07-22 : 13:06:24
|
Check my edited queries. Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
|
|