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 |
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2008-11-21 : 12:46:05
|
I'm back and got the cross tabs to work thanks again!Could you help me generate my year columns dynamically?Thanks!SELECT Sort,Reg, SUM(CASE WHEN MONTH(DowrDt)=9 AND YEAR(DowrDt)=2008 THEN PctInt Else 0 END) AS [Sep 08], SUM(CASE WHEN MONTH(DowrDt)=10 AND YEAR(DowrDt)=2008 THEN PctInt Else 0 END) AS [Oct 08], SUM(CASE WHEN MONTH(DowrDt)=11 AND YEAR(DowrDt)=2008 THEN PctInt Else 0 END) AS [Nov 08], SUM(CASE WHEN MONTH(DowrDt)=12 AND YEAR(DowrDt)=2008 THEN PctInt Else 0 END) AS [Dec 08], SUM(CASE WHEN MONTH(DowrDt)=1 AND YEAR(DowrDt)=2009 THEN PctInt Else 0 END) AS [Jan 09], SUM(CASE WHEN MONTH(DowrDt)=2 AND YEAR(DowrDt)=2009 THEN PctInt Else 0 END) AS [Feb 09], SUM(CASE WHEN MONTH(DowrDt)=3 AND YEAR(DowrDt)=2009 THEN PctInt Else 0 END) AS [Mar 09], SUM(CASE WHEN MONTH(DowrDt)=4 AND YEAR(DowrDt)=2009 THEN PctInt Else 0 END) AS [Apr 09], SUM(CASE WHEN MONTH(DowrDt)=5 AND YEAR(DowrDt)=2009 THEN PctInt Else 0 END) AS [May 09], SUM(CASE WHEN MONTH(DowrDt)=6 AND YEAR(DowrDt)=2009 THEN PctInt Else 0 END) AS [Jun 09], SUM(CASE WHEN MONTH(DowrDt)=7 AND YEAR(DowrDt)=2009 THEN PctInt Else 0 END) AS [Jul 09], SUM(CASE WHEN MONTH(DowrDt)=8 AND YEAR(DowrDt)=2009 THEN PctInt Else 0 END) AS [Aug 09], SUM(CASE WHEN MONTH(DowrDt)=9 AND YEAR(DowrDt)=2009 THEN PctInt Else 0 END) AS [Sep 09], SUM(CASE WHEN MONTH(DowrDt)=10 AND YEAR(DowrDt)=2009 THEN PctInt Else 0 END) AS [Oct 09], SUM(CASE WHEN MONTH(DowrDt)=11 AND YEAR(DowrDt)=2009 THEN PctInt Else 0 END) AS [Nov 09], SUM(CASE WHEN MONTH(DowrDt)=12 AND YEAR(DowrDt)=2009 THEN PctInt Else 0 END) AS [Dec 09]FROM #tempintncGROUP BY sort,RegOrder by sort, reg |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2008-11-21 : 13:39:35
|
If you have a Date table you could do something like:DECLARE @Date TABLE (FullDate SMALLDATETIME)INSERT @DateSELECT '20080101' UNION ALLSELECT '20080201' UNION ALLSELECT '20080301' UNION ALLSELECT '20080401' UNION ALLSELECT '20080501' UNION ALLSELECT '20080601' UNION ALLSELECT '20080701' UNION ALLSELECT '20080801' UNION ALLSELECT '20080901' UNION ALLSELECT '20081001' UNION ALLSELECT '20081101' UNION ALLSELECT '20081201' UNION ALLSELECT '20090101' UNION ALLSELECT '20090201' UNION ALLSELECT '20090301' UNION ALLSELECT '20090401' UNION ALLSELECT '20090501' UNION ALLSELECT '20090601' UNION ALLSELECT '20090701' UNION ALLSELECT '20090801' UNION ALLSELECT '20090901' UNION ALLSELECT '20091001' UNION ALLSELECT '20091101' UNION ALLSELECT '20091201' DECLARE @String VARCHAR(8000)SET @String = 'SELECT Sort,Reg, ' + CHAR(13) + CHAR(10)SELECT @String = @String + STUFF( (SELECT ',SUM(CASE WHEN MONTH(DowrDt)= ' + CAST(MONTH(FullDate) AS VARCHAR(2)) + ' AND YEAR(DowrDt)= ' + CAST(YEAR(FullDate) AS VARCHAR(4)) + ' THEN PctInt Else 0 END) AS [' + CONVERT(VARCHAR(3), FullDate, 9) + ' ' + RIGHT(CAST(YEAR(FullDate) AS VARCHAR(4)), 2) + '] ' + CHAR(10) FROM @Date WHERE FulLDate BETWEEN '20080901' AND '20091201' ORDER BY FullDate FOR XML PATH('')), 1, 1, '') SET @String = @String + 'FROM #tempintnc ' + CHAR(13) + CHAR(10) + 'GROUP BY sort,Reg ' + CHAR(13) + CHAR(10) + 'Order by sort, reg' + CHAR(13) + CHAR(10) SELECT @String |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2008-11-21 : 13:52:54
|
| PERFECT!!!!! Thanks so much this is the best Forum to come to because you all are experts! I've learned so much.Would you mind writing some comments next to the select statement so I can understand what you had done.Thanks! |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2008-11-21 : 14:11:09
|
| One thing I am running a 13 month trend so I would want 9/08 - 10/08 to appear on the report then next month I would want 10/08 - 11/08, does that make sense?So instead of hardcoding those dates and having to update each year is there a way to use parameters? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-22 : 01:29:31
|
then i think you need to do a dynamic pivot. something like belowDECLARE @PeriodList varchar(max)SELECT @PeriodList=LEFT(pl.PeriodList,LEN(pl.PeriodList)-1)FROM (SELECT DISTINCT DATENAME(yyyy,DATEADD(mm,-1 * number,GETDATE()))+DATENAME(mm,DATEADD(mm,-1 * number,GETDATE()))+','FROM master..spt_valuesWHERE type='p'AND number<=12FOR XML PATH(''))pl(PeriodList)SELECT @Sql='SELECT *FROM(SELECT Sort,Reg,PctInt,DATENAME(yyyy,DowrDt)+ DATENAME(mm,DowrDt) AS PeriodFROM YourTable)mPIVOT (SUM(PctInt) FOR Period IN ([' + REPLACE(@PeriodList,',','],[') + ']))p'EXEC (@Sql) |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2008-11-24 : 13:23:14
|
Thanks! Sorry for just getting to this. I tried it but I'm getting the error:Must declare the scalar variable "@Sql".This is my stored procedure with the pivot info added to it:USE [iClaims]GO/****** Object: StoredProcedure [dbo].[iClaimsNat] Script Date: 11/24/2008 13:19:30 ******/SET ANSI_NULLS OFFGOSET QUOTED_IDENTIFIER OFFGOALTER PROCEDURE [dbo].[iClaimsNat]@Rpt char(1),@Period char(1) AsSET NOCOUNT ONselect ZipCd = Case when RSDNC_ZIP5 is Null then MAILG_ADDR_ZIP5else RSDNC_ZIP5 end, convert(char,MonthDt,1) as DowrDt, INET_IND, COSSNinto #tempzipfrom iClaims If @Rpt = '1' and @Period = 'M'beginSelect Sort='1',Reg='NAT', a.DowrDt, iCnt, tCnt into #tempintFrom (select DowrDt, Count(COSSN) as iCntfrom #tempzipwhere INET_IND <> 'N'Group by DowrDt)a inner join (select DowrDt, Count(COSSN) as tCntfrom #tempzipGroup by DowrDt)bon a.DowrDt = b.DowrDt insert #tempintSelect Sort =c.Region,Reg=c.Reg, c.DowrDt, iCnt, tCntFrom(select Region, Reg, DowrDt, Count(COSSN) as iCnt from #tempzip inner join ZipCodes on zipcd = zipwhere INET_IND <> 'N'Group by Region, Reg, DowrDt )c inner join(select Region, Reg, DowrDt, Count(COSSN) as tCnt from #tempzip inner join ZipCodes on zipcd = zipGroup by Region, Reg, DowrDt )d on c.DowrDt = d.DowrDt and c.Region = d.Regionend DECLARE @PeriodList varchar(max)SELECT @PeriodList=LEFT(pl.PeriodList,LEN(pl.PeriodList)-1)FROM (SELECT DISTINCT DATENAME(yyyy,DATEADD(mm,-1 * number,GETDATE()))+DATENAME(mm,DATEADD(mm,-1 * number,GETDATE()))+','FROM master..spt_valuesWHERE type='p'AND number<=12FOR XML PATH(''))pl(PeriodList)SELECT @Sql='SELECT *FROM(SELECT Sort,Reg,PctInt,DATENAME(yyyy,DowrDt)+ DATENAME(mm,DowrDt) AS PeriodFROM #tempzip)mPIVOT (SUM(PctInt) FOR Period IN ([' + REPLACE(@PeriodList,',','],[') + ']))p'EXEC (@Sql)drop table #tempzipdrop table #tempint |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2008-11-26 : 07:23:39
|
Thanks but having a hard time setting this up...How can I get my table to look like this with the dynamic pivot table with the months being generated across the top? Sorry don't understand the info you sent.The fields that I need are Sort, Reg and DowrDt which is the month field.Sort Reg Sept08 Oct08 Nov081 NAT 16.22 17.24 18.22A BOS 14.60 14.50 15.14 |
 |
|
|
|
|
|
|
|