hello guys,
I am trying to create a dynamic reporting system and not sure about how to go about it.
I have a table of departments (which vary for different clients) and the clients have no general naming conventions or lenghts and any number e.g. ABC1, DBR4, AKJ9, akdjafdk etc The last one I did had 47 columns
As they want reports generated and exported to excel files
So when I run select distinct departments from reports where IsEnabled = 1 I get the list of departments
e.g.
ABC1
DBR4
AKJ9
akdjafdk
Now the client wants these as column names. Here is my HARD CODED script of column names. I created a loop to update the columns
How can I achieve dynamic columns ?
thanks
update final_reports
set IAAFDHN = (select (count()/1000.000) from .[dbo].[##Temp_Reports] WHERE USERNAME = @each and DeptID =‘IAAFDHN’ ),
IAafd2M = (select (count()/1000.000) from .[dbo].[##Temp_Reports] WHERE USERNAME = @each and DeptID =‘IAafd2M’ ),
IFasdA2A = (select (count()/1000.000) from .[dbo].[##Temp_Reports] WHERE USERNAME = @each and DeptID =‘IFasdA2A’ ),
IA4AFDM = (select (count()/1000.000) from .[dbo].[##Temp_Reports] WHERE USERNAME = @each and DeptID =‘IA4AFDM’ ),
IAAFDA4A = (select (count()/1000.000) from .[dbo].[##Temp_Reports] WHERE USERNAME = @each and DeptID =‘IAAFDA4A’ ),
RDAAI3A = (select (count()/1000.000) from .[dbo].[##Temp_Reports] WHERE USERNAME = @each and DeptID =‘RDAAI3A’ ),
RAADFAI1A = (select (count()/1000.000) from .[dbo].[##Temp_Reports] WHERE USERNAME = @each and DeptID =‘RAADFAI1A’ ),
RAAFDAI2A = (select (count()/1000.000) from .[dbo].[##Temp_Reports] WHERE USERNAME = @each and DeptID =‘RAAFDAI2A’ ),
NGEAFD4M = (select (count()/1000.000) from .[dbo].[##Temp_Reports] WHERE USERNAME = @each and DeptID =‘NGEAFD4M’ ),
ZAFDAAL1M = (select (count()/1000.000) from .[dbo].[##Temp_Reports] WHERE USERNAME = @each and DeptID =‘ZAFDAAL1M’ ),
HFDAPN2M = (select (count()/1000.000) from .[dbo].[##Temp_Reports] WHERE USERNAME = @each and DeptID =‘HFDAPN2M’ ),
HAFDPN2A = (select (count()/1000.000) from .[dbo].[##Temp_Reports] WHERE USERNAME = @each and DeptID =‘HAFDPN2A’ ),
HPAFDAN3M = (select (count(*)/1000.000) from .[dbo].[##Temp_Reports] WHERE USERNAME = @each and DeptID =‘HPAFDAN3M’ ),where enabled = 1