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 |
|
klondono
Starting Member
6 Posts |
Posted - 2011-07-13 : 09:03:28
|
Select * from #temp The statement above yields the following records:ShopName-----LicenseNo----Officer------TypeOfInsp - Result - DateABC Shop LIC1 Alfredo Diaz MVR Insp Approved 6/1/2011ABC Shop LIC1 Suresh Bedasie MVR Insp Approved 6/10/2011GEH Shop LIC3 Suresh Bedasie MVR Insp Approved 7/1/2011IJK Shop LIC4 Alfredo Diaz MVR Insp Approved 8/1/2011LMN Shop LIC5 Alfredo Diaz MVR Insp Approved 6/1/2011LMN Shop LIC5 Suresh Bedasie MVR Insp Approved 7/1/2011RST Shop LIC7 Alfredo Diaz MVR Insp Approved 2/1/2011UVW Shop LIC8 Alfredo Diaz MVR Insp Approved 6/1/2011From the table above, I extract the total inspections done for each officer per shop with the following statement. This is the Total Inspections by Officer table :select [Shop] = T.ShopName, [LicenseNo] = T.LicenseNo, [Alfredo] = (SUM(case when T.Officer = 'Alfredo Diaz' then 1 else 0 end)), [Suresh] = SUM(case when T.Officer = 'Suresh Bedasie' then 1 else 0 end), [Total_Insp] = count(*)from #temp Tgroup by T.ShopName, T.LicenseNoORDER BY [Total_Insp] descThis is the result:Shop-----LicenseNo----Alfredo------Suresh ---Total_InspABC Shop LIC1 1 1 2GEH Shop LIC3 0 1 1IJK Shop LIC4 1 0 1LMN Shop LIC5 1 1 2UVW Shop LIC8 1 0 1Instead of explicitly creating the columns for each officer (this will then require that I change the query each time an officer is hired or fired), how can I create the Total Inspections by Officer table to dynamically create a column depending on the total number of officers that appear on the first table? Any help will be greatly appreciated. Thank you!! |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
klondono
Starting Member
6 Posts |
Posted - 2011-07-14 : 21:27:44
|
| Thank you so much! I used the stored procedure that dynamically creates my columns and provides the totals for each officer. I would like to create a totals column but cant figure out how to do it. I'll provide the sql statement i used to get the totals & create the columns tomorrow.Seeking Analyst/Programming Job - Career changer |
 |
|
|
klondono
Starting Member
6 Posts |
Posted - 2011-07-15 : 09:52:35
|
| Thank you very much Madhivanan!! It worked using the code you posted. I created the procedure "dynamic_pivot" and then executed it with the parameters below. This is the entire SQL statement:Select * from #temp--Create the procedure that will dynamically create your columnscreate procedure 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) --Execute dynamic_pivot with SQL select statement, column requiring pivoting and specific column to countEXEC dynamic_pivot'select [ShopName] = T.ShopName, T.LicenseNo, T.Officer from #temp T','(T.Officer)','Count(T.Officer)'Seeking Analyst/Programming Job - Career changer |
 |
|
|
klondono
Starting Member
6 Posts |
Posted - 2011-07-15 : 09:57:08
|
| The code above dynamically creates the Officer columns (Alfredo Suresh), How do I add a totals column to the end of each row? Also, how do I add totals for the Alfredo and Suresh columns using the stored procedure in the previous post? I know how to do it when I know in advance what the name of Officer columns will be but that is not the case here. Any help will be appreciated.Shop-----LicenseNo----Alfredo------SureshABC Shop LIC1 1 1 GEH Shop LIC3 0 1 IJK Shop LIC4 1 0 LMN Shop LIC5 1 1 UVW Shop LIC8 1 0 1Seeking Analyst/Programming Job - Career changer |
 |
|
|
|
|
|
|
|