Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

dynamic pivot table, trouble with the quotes

$
0
0

Hello Naomi,

In relation to the other dynamic pivot table thread I've got an other question.

I would like to change the column names in the query. However I keep bumping into problems cause while the query is being inserted into a string it hold whenever i place a ' mark.

For instance this query works

create table DailyIncome(VendorId nvarchar(10), IncomeDay nvarchar(10), IncomeAmount int)
--drop table DailyIncome
insert into DailyIncome values ('SPIKE', 'FRI', 100)
insert into DailyIncome values ('SPIKE', 'MON', 300)
insert into DailyIncome values ('FREDS', 'SUN', 400)
insert into DailyIncome values ('SPIKE', 'WED', 500)
insert into DailyIncome values ('SPIKE', 'TUE', 200)
insert into DailyIncome values ('JOHNS', 'WED', 900)
insert into DailyIncome values ('SPIKE', 'FRI', 100)
insert into DailyIncome values ('JOHNS', 'MON', 300)
insert into DailyIncome values ('SPIKE', 'SUN', 400)
insert into DailyIncome values ('JOHNS', 'FRI', 300)
insert into DailyIncome values ('FREDS', 'TUE', 500)
insert into DailyIncome values ('FREDS', 'TUE', 200)
insert into DailyIncome values ('SPIKE', 'MON', 900)
insert into DailyIncome values ('FREDS', 'FRI', 900)
insert into DailyIncome values ('FREDS', 'MON', 500)
insert into DailyIncome values ('JOHNS', 'SUN', 600)
insert into DailyIncome values ('SPIKE', 'FRI', 300)
insert into DailyIncome values ('SPIKE', 'WED', 500)
insert into DailyIncome values ('SPIKE', 'FRI', 300)
insert into DailyIncome values ('JOHNS', 'THU', 800)
insert into DailyIncome values ('JOHNS', 'SAT', 800)
insert into DailyIncome values ('SPIKE', 'TUE', 100)
insert into DailyIncome values ('SPIKE', 'THU', 300)
insert into DailyIncome values ('FREDS', 'WED', 500)
insert into DailyIncome values ('SPIKE', 'SAT', 100)
insert into DailyIncome values ('FREDS', 'SAT', 500)
insert into DailyIncome values ('FREDS', 'THU', 800)
insert into DailyIncome values ('JOHNS', 'TUE', 600) 


select * from DailyIncome

DECLARE @cols AS NVARCHAR(MAX),
		@query  AS NVARCHAR(MAX)
;


--determine amount of columns and its name
SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(col.incomeday) 
            FROM (
				select incomeday from DailyIncome 
			) AS col
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)')	
                    ,1,1,'')       

--compile query in text and add variable column 
set @query = 'SELECT vendorID, ' + @cols + '
INTO ##a
 from 
            (
				select vendorid, incomeday  as incomeday, incomeamount from DailyIncome 
			   ) x
            pivot 
            (   max(incomeamount)
                for incomeday in (' + @cols + ')
            ) p '

--run query

execute(@query) 

select * from ##a


  if object_id('tempdb..#a') is not null
		begin
			drop table    ##a 
	end


and this one doens't (due to the extra ' ' )

create table DailyIncome(VendorId nvarchar(10), IncomeDay nvarchar(10), IncomeAmount int)
--drop table DailyIncome
insert into DailyIncome values ('SPIKE', 'FRI', 100)
insert into DailyIncome values ('SPIKE', 'MON', 300)
insert into DailyIncome values ('FREDS', 'SUN', 400)
insert into DailyIncome values ('SPIKE', 'WED', 500)
insert into DailyIncome values ('SPIKE', 'TUE', 200)
insert into DailyIncome values ('JOHNS', 'WED', 900)
insert into DailyIncome values ('SPIKE', 'FRI', 100)
insert into DailyIncome values ('JOHNS', 'MON', 300)
insert into DailyIncome values ('SPIKE', 'SUN', 400)
insert into DailyIncome values ('JOHNS', 'FRI', 300)
insert into DailyIncome values ('FREDS', 'TUE', 500)
insert into DailyIncome values ('FREDS', 'TUE', 200)
insert into DailyIncome values ('SPIKE', 'MON', 900)
insert into DailyIncome values ('FREDS', 'FRI', 900)
insert into DailyIncome values ('FREDS', 'MON', 500)
insert into DailyIncome values ('JOHNS', 'SUN', 600)
insert into DailyIncome values ('SPIKE', 'FRI', 300)
insert into DailyIncome values ('SPIKE', 'WED', 500)
insert into DailyIncome values ('SPIKE', 'FRI', 300)
insert into DailyIncome values ('JOHNS', 'THU', 800)
insert into DailyIncome values ('JOHNS', 'SAT', 800)
insert into DailyIncome values ('SPIKE', 'TUE', 100)
insert into DailyIncome values ('SPIKE', 'THU', 300)
insert into DailyIncome values ('FREDS', 'WED', 500)
insert into DailyIncome values ('SPIKE', 'SAT', 100)
insert into DailyIncome values ('FREDS', 'SAT', 500)
insert into DailyIncome values ('FREDS', 'THU', 800)
insert into DailyIncome values ('JOHNS', 'TUE', 600) 


select * from DailyIncome

DECLARE @cols AS NVARCHAR(MAX),
		@query  AS NVARCHAR(MAX)
;


--determine amount of columns and its name
SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(col.incomeday) 
            FROM (
				select incomeday from DailyIncome 
			) AS col
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)')	
                    ,1,1,'')       

--compile query in text and add variable column 
set @query = 'SELECT vendorID, ' + @cols + '
INTO ##a
 from 
            (
				select vendorid, incomeday + 'day' as incomeday, incomeamount from DailyIncome 
			   ) x
            pivot 
            (   max(incomeamount)
                for incomeday in (' + @cols + ')
            ) p '

--run query

execute(@query) 

select * from ##a


  if object_id('tempdb..#a') is not null
		begin
			drop table    ##a 
	end

What would you advice to change it's column names?

Thanks in advance!


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>