Author |
Topic |
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2012-07-19 : 02:13:31
|
I am having a table like thisId--Name---Source---CreateDate1---Wilson--WebSite-- 7/7/2012 2---jafry--Referal-- 7/7/2012 3---Bryce--WebSite-- 6/10/20124---Jim---WebSite--- 5/7/2012 5---Peter--WebSite-- 7/7/20116---John--Direct-- 7/7/2011My req o/p for year 2012Source--May-- June-- July Website-1-----1-------1Referal-0-----0-------1Direct-0-----0-------1for year 2011Source--May-- June-- July Website-0-----0-------1Referal-0-----0-------0Direct-0-----0-------0 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-07-19 : 02:26:19
|
use PIVOT operator. Please refer to BOL http://msdn.microsoft.com/en-us/library/ms177410%28v=sql.105%29.aspx KH[spoiler]Time is always against us[/spoiler] |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2012-07-19 : 02:45:19
|
Thnak you Khtan.. I tried to create Pivot table like thisSELECT UtilityValue,[Jan], [Feb], [Mar], [Apr], [May], [June], [July], [Aug], [Sep], [Oct], [Nov], [Dec]FROM(SELECT DATEADD(mm,DATEDIFF(mm,0,create_date),0) AS FirstDayofCreatedMonth, UtilityValue,marketing_source,count(isnull(marketing_source,0)) cnt FROM student join Utility on Utility.UID=student.marketing_sourceGROUP BY marketing_source,DATEADD(mm,DATEDIFF(mm,0,create_date),0),UtilityValue ) AS SourceTablePIVOT(cntFOR month(FirstDayofCreatedMonth) IN ([Jan], [Feb], [Mar], [Apr], [May], [June], [July], [Aug], [Sep], [Oct], [Nov], [Dec])) AS PivotTable;but i got error asMsg 156, Level 15, State 1, Line 15Incorrect syntax near the keyword 'FOR'. |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2012-07-19 : 02:56:03
|
Thank you for your help.. i got itSELECT *FROM(SELECT DATEADD(mm,DATEDIFF(mm,0,create_date),0) AS FirstDayofCreatedMonth, month(create_date) AS Mth, UtilityValue,marketing_source FROM student join Utility on Utility.UID=student.marketing_sourceGROUP BY marketing_source,DATEADD(mm,DATEDIFF(mm,0,create_date),0),UtilityValue,create_date) AS SourceTablePIVOT(count(marketing_source)FOR Mth IN ([1], [2], [3],[4],[5],[6],[7],,[9],[10],[11] ,[12])) AS PivotTable; |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-07-19 : 02:56:27
|
[code]PIVOT(cntFOR month(FirstDayofCreatedMonth) IN ([Jan], [Feb], [Mar], [Apr], [May], [June], [July], [Aug], [Sep], [Oct], [Nov], [Dec])) AS PivotTable;[/code]1. the cnt should be a aggregate function2. this does not return month(FirstDayofCreatedMonth) the value Jan, Feb etc, it returns integer value 1 to 123. you can't do this month(FirstDayofCreatedMonth) in the PIVOT part. Do it in the SourceTable query[code]SELECT UtilityValue, [Jan], [Feb], [Mar], [Apr], [May], [June], [July], [Aug], [Sep], [Oct], [Nov], [Dec]FROM( SELECT left(datename(month, create_date), 3) AS FirstDayofCreatedMonth, UtilityValue,marketing_source FROM student join Utility on Utility.UID=student.marketing_source) AS SourceTablePIVOT( count(marketing_source) FOR FirstDayofCreatedMonth IN ([Jan], [Feb], [Mar], [Apr], [May], [June], [July], [Aug], [Sep], [Oct], [Nov], [Dec])) AS PivotTable;[/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
|
|
|