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 |
pizzazzz
Yak Posting Veteran
55 Posts |
Posted - 2010-10-21 : 17:02:55
|
I have been pulling my hair out today trying to create a pivot from the following data, below is the sql script I used; any help would be really appreciated -thx'sEventDate Description Count9/1/2010 Special Events 119/2/2010 Roadshow (Food) 289/2/2010 Special Events 249/3/2010 Special Events 549/3/2010 Roadshow (Food) 429/4/2010 Special Events 259/4/2010 Roadshow (Food) 229/5/2010 Roadshow (Food) 229/5/2010 Special Events 279/7/2010 Special Events 329/8/2010 Special Events 449/9/2010 Roadshow (Food) 409/9/2010 Special Events 279/10/2010 Roadshow (Food) 369/10/2010 Special Events 319/11/2010 Special Events 199/11/2010 Roadshow (Food) 279/12/2010 Special Events 539/12/2010 Roadshow (Food) 229/13/2010 Special Events 29/14/2010 Special Events 19/15/2010 Special Events 19/15/2010 Roadshow (Food) 10This is the sql script I used - SELECT Convert(Varchar(10),tblDemos.DemoDate, 101) as EventTime, tblServiceType.Description, Count(tblBreakers.EmployeeInfoID) AS CountOfEmployee FROM ((((tblDemos INNER JOIN tblLocations ON tblDemos.LocationsID = tblLocations.LocationsID) INNER JOIN tblBillingHeader ON tblDemos.DemoID = tblBillingHeader.DemoID) INNER JOIN tblBillingDetail ON tblBillingHeader.BillingHeaderID = tblBillingDetail.BillingHeaderID) INNER JOIN (tblServiceType INNER JOIN tblItems ON tblServiceType.ServiceTypeID = tblItems.ServiceTypeID) ON tblBillingDetail.ItemsID = tblItems.ItemsID) INNER JOIN tblBreakers ON tblDemos.DemoID = tblBreakers.DemoID WHERE (((tblDemos.DemoDate) Between '9/1/2010' And '9/15/2010') AND ((tblDemos.StatusCodeID) In (3,5,6)) AND ((tblLocations.DivisionID)=781) AND ((tblServiceType.ServiceTypeID) In (3,9))) GROUP BY tblDemos.DemoDate, tblServiceType.Description, tblDemos.StatusCodeID, tblLocations.DivisionID, tblServiceType.ServiceTypeID ORDER BY tblDemos.DemoDate |
|
sql-programmers
Posting Yak Master
190 Posts |
|
pizzazzz
Yak Posting Veteran
55 Posts |
Posted - 2010-10-22 : 13:13:04
|
Thanks! I'll take a look at it! |
 |
|
pizzazzz
Yak Posting Veteran
55 Posts |
Posted - 2010-10-22 : 13:29:52
|
Thanks! I've read a lot of articles regarding PIVOT and UNPIVOT, but I still get invalid errors. Any other suggestions? |
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-10-22 : 14:59:59
|
What is the error & what query are you using?PBUH |
 |
|
pizzazzz
Yak Posting Veteran
55 Posts |
Posted - 2010-10-22 : 15:23:45
|
The query is in the post above - plus the dataset it produces. |
 |
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2010-10-22 : 17:42:21
|
what is the final result you want to see? it helps for next time if you have something like followingDECLARE @hihi TABLE(EventDate DATETIME, DESCRIPTION NVARCHAR(255), COUNT int)INSERT INTO @hihi ( EventDate, DESCRIPTION, COUNT )SELECT '9/1/2010', ' Special Events', 11UNION ALLSELECT '9/2/2010', ' Roadshow (Food)', 28UNION ALLSELECT '9/2/2010', ' Special Events', 24UNION ALLSELECT '9/3/2010', ' Special Events', 54UNION ALLSELECT '9/3/2010', ' Roadshow (Food)', 42UNION ALLSELECT '9/4/2010', ' Special Events', 25UNION ALLSELECT '9/4/2010', ' Roadshow (Food)', 22UNION ALLSELECT '9/5/2010', ' Roadshow (Food)', 22UNION ALLSELECT '9/5/2010', ' Special Events', 27UNION ALLSELECT '9/7/2010', ' Special Events', 32UNION ALLSELECT '9/8/2010', ' Special Events', 44UNION ALLSELECT '9/9/2010', ' Roadshow (Food)', 40UNION ALLSELECT '9/9/2010', ' Special Events', 27UNION ALLSELECT '9/10/2010', ' Roadshow (Food)', 36UNION ALLSELECT '9/10/2010', ' Special Events', 31UNION ALLSELECT '9/11/2010', ' Special Events', 19UNION ALLSELECT '9/11/2010', ' Roadshow (Food)', 27UNION ALLSELECT '9/12/2010', ' Special Events', 53UNION ALLSELECT '9/12/2010', ' Roadshow (Food)', 22UNION ALLSELECT '9/13/2010', ' Special Events', 2UNION ALLSELECT '9/14/2010', ' Special Events', 1UNION ALLSELECT '9/15/2010', ' Special Events', 1UNION ALLSELECT '9/15/2010', ' Roadshow (Food)', 10SELECT * FROM @hihi If you don't have the passion to help people, you have no passion |
 |
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2010-10-22 : 18:11:13
|
[code]create procedure [dbo].[dynamic_pivot](@select varchar(2000),@PivotCol varchar(100), @Summaries varchar(100)) as declare @pivot varchar(max), @sql varchar(max)select @select =replace(@select,'select ','select '+@PivotCol+' as pivot_col,') create table #pivot_columns (pivot_column varchar(100))Select @sql='select distinct pivot_col from ('+@select+') as t'insert into #pivot_columnsexec(@sql)select @pivot=coalesce(@pivot+',','')+'['+pivot_column+']'from #pivot_columnsselect @sql=' select * from ( '+@select+' ) as t pivot ( '+@Summaries+' for pivot_col in ('+@pivot+') ) as p' exec(@sql) GOCREATE TABLE hihi (EventDate DATETIME, DESCRIPTION NVARCHAR(255), iCOUNT int)INSERT INTO hihi ( EventDate, DESCRIPTION, iCOUNT )SELECT '9/1/2010', ' Special Events', 11UNION ALLSELECT '9/2/2010', ' Roadshow (Food)', 28UNION ALLSELECT '9/2/2010', ' Special Events', 24UNION ALLSELECT '9/3/2010', ' Special Events', 54UNION ALLSELECT '9/3/2010', ' Roadshow (Food)', 42UNION ALLSELECT '9/4/2010', ' Special Events', 25UNION ALLSELECT '9/4/2010', ' Roadshow (Food)', 22UNION ALLSELECT '9/5/2010', ' Roadshow (Food)', 22UNION ALLSELECT '9/5/2010', ' Special Events', 27UNION ALLSELECT '9/7/2010', ' Special Events', 32UNION ALLSELECT '9/8/2010', ' Special Events', 44UNION ALLSELECT '9/9/2010', ' Roadshow (Food)', 40UNION ALLSELECT '9/9/2010', ' Special Events', 27UNION ALLSELECT '9/10/2010', ' Roadshow (Food)', 36UNION ALLSELECT '9/10/2010', ' Special Events', 31UNION ALLSELECT '9/11/2010', ' Special Events', 19UNION ALLSELECT '9/11/2010', ' Roadshow (Food)', 27UNION ALLSELECT '9/12/2010', ' Special Events', 53UNION ALLSELECT '9/12/2010', ' Roadshow (Food)', 22UNION ALLSELECT '9/13/2010', ' Special Events', 2UNION ALLSELECT '9/14/2010', ' Special Events', 1UNION ALLSELECT '9/15/2010', ' Special Events', 1UNION ALLSELECT '9/15/2010', ' Roadshow (Food)', 10EXEC dynamic_pivot 'SELECT EventDate, iCOUNT FROM hihi','DESCRIPTION','SUM (iCOUNT)' DROP TABLE hihi[/code]If you don't have the passion to help people, you have no passion |
 |
|
pizzazzz
Yak Posting Veteran
55 Posts |
Posted - 2010-10-22 : 19:33:46
|
Thanks for the script to create my pivot!! |
 |
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
|
|
|
|
|
|