| Author |
Topic |
|
joemama
Posting Yak Master
113 Posts |
Posted - 2009-11-25 : 14:07:23
|
i have a working pivot table that gives me everything i need except one thing...i am hoping someone can give me a solutionthe output gives me totals by month that are added up to give me a yearly total ...however...what i want is not the yearly total but the TOTAL of the 8 highest monthshere is the part of the stored procedure that does the SUMIsNull([1], 0) + IsNull([2], 0) + IsNull([3], 0) + IsNull([4], 0) + IsNull([5], 0) + IsNull([6], 0) + IsNull([7], 0) + IsNull( , 0) + IsNull([9], 0) + IsNull([10], 0) + IsNull([11],0) + IsNull([12],0) AS 'TournPoints'here is the complete stored procedureALTER PROCEDURE [dbo].[WPRO_Get_TOURNAMENT_MATRIX] (@TournYear int)ASSELECT UserID, USERLASTNAME + ' ' + SUFFIX AS 'Angler', IsNull([1], 0) AS [Jan], IsNull([2], 0) AS [Feb], IsNull([3], 0) AS [Mar], IsNull([4], 0) AS [Apr], IsNull([5], 0) AS [May], IsNull([6], 0) AS [Jun], IsNull([7], 0) AS [Jul], IsNull( , 0) AS [Aug], IsNull([9], 0) AS [Sep], IsNull([10], 0) AS [Oct], IsNull([11], 0) AS [Nov], IsNull([12], 0) AS [Dec], IsNull([1], 0) + IsNull([2], 0) + IsNull([3], 0) + IsNull([4], 0) + IsNull([5], 0) + IsNull([6], 0) + IsNull([7], 0) + IsNull( , 0) + IsNull([9], 0) + IsNull([10], 0) + IsNull([11],0) + IsNull([12],0) AS 'TournPoints'FROM ccbass.dbo.Tournament_Results_for_PVT PIVOT (max(TRpoints) FOR tournmonth IN ([1], [2], [3], [4], [5], [6], [7], , [9], [10], [11], [12])) AS pvtwhere Tournyear = @TournYearorder by Tournpoints desc,Userlastname |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-11-27 : 02:40:10
|
| do u want individual month totals as well or just 8 month aggregate? |
 |
|
|
joemama
Posting Yak Master
113 Posts |
Posted - 2009-11-28 : 00:48:52
|
| I need both |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-11-28 : 01:50:41
|
| [code]select UserID,Angler,SUM(CASE WHEN Seq<=8 THEN TRpoints ELSE 0 END) AS 8mnthaggre,MAX(CASE WHEN tournmonth=1 THEN TRpoints ELSE NULL END) AS Jan,MAX(CASE WHEN tournmonth=2 THEN TRpoints ELSE NULL END) AS Feb,......MAX(CASE WHEN tournmonth=12 THEN TRpoints ELSE NULL END) AS Decfrom(select UserID, USERLASTNAME + ' ' + SUFFIX AS 'Angler',tournmonth,SUM(TRpoints) AS TRpoints,ROW_NUMBER() OVER(PARTITION BY UserID, USERLASTNAME + ' ' + SUFFIX ORDER BY SUM(TRpoints) DESC) AS Seqfrom ccbass.dbo.Tournament_Results_for_PVwhere Tournyear = @TournYeargroup by UserID, USERLASTNAME + ' ' + SUFFIX,tournmonth)tGROUP BY UserID, Angler[/code] |
 |
|
|
joemama
Posting Yak Master
113 Posts |
Posted - 2009-11-30 : 17:55:34
|
YOU ROCK!this is perfect...thanksquote: Originally posted by visakh16
select UserID,Angler,SUM(CASE WHEN Seq<=8 THEN TRpoints ELSE 0 END) AS 8mnthaggre,MAX(CASE WHEN tournmonth=1 THEN TRpoints ELSE NULL END) AS Jan,MAX(CASE WHEN tournmonth=2 THEN TRpoints ELSE NULL END) AS Feb,......MAX(CASE WHEN tournmonth=12 THEN TRpoints ELSE NULL END) AS Decfrom(select UserID, USERLASTNAME + ' ' + SUFFIX AS 'Angler',tournmonth,SUM(TRpoints) AS TRpoints,ROW_NUMBER() OVER(PARTITION BY UserID, USERLASTNAME + ' ' + SUFFIX ORDER BY SUM(TRpoints) DESC) AS Seqfrom ccbass.dbo.Tournament_Results_for_PVwhere Tournyear = @TournYeargroup by UserID, USERLASTNAME + ' ' + SUFFIX,tournmonth)tGROUP BY UserID, Angler
|
 |
|
|
|
|
|