Solved

pivot fonction  in sql server 2005

Posted on 2011-09-09
16
185 Views
Last Modified: 2012-05-12
Hi
I got the code below from ralmada and it works fine producing output as illustrated in TABLE A below.

I would now like to modify the code so that the returned column names for  the Dates are appended  with the text 'Hardware' as shown in TABLE B

I have concatenated  'Hardware' in the variable @colshw
like
select @colshw = stuff((select '],[' + right(convert(varchar, st, 106), 8)+' Hardware'

which compiles fine and runs   but  the results are as TABLE B, '0' for all cell entries concat
any help appreciated...thanks
/* pivot for hardware */
declare @strSQLhw varchar(max);
declare @colshw varchar(max);
;with CTE as (
	select 	dateadd(m, datediff(m, 0, @start_date_range), 0) as st, 
		dateadd(m, datediff(m, 0, @end_date_range), 0) as et
	union all
	select dateadd(m, 1, st),
			et
	from CTE 
	where dateadd(m, 1, st) <= et
)
select @colshw = stuff((select '],[' + right(convert(varchar, st, 106), 8)
 from CTE for xml path('')), 1, 2, '') + ']'


set @strSQLhw = '

select server_name, ' + @colshw + '  from (
select server_name, 
right(convert(varchar, dateadd(m, datediff(m, 0, [resolved_date]), 0), 106), 8)
 as eom, PROD_CAT1
		from #extranetHardware
		where [resolved_date] >= ''' + 
convert(varchar, @start_date_range,
 112) + ''' and [resolved_date] < ''' +convert(varchar, @end_date_range+1,112) + ''') o
		pivot (count(PROD_CAT1) for eom in (' + @colshw +' )) as p '

print (@strSQLhw)
execute (@strSQLhw)

Open in new window

0
Comment
Question by:blossompark
  • 9
  • 6
16 Comments
 
LVL 42

Expert Comment

by:pcelba
ID: 36510758
Try this:

/* pivot for hardware */
declare @strSQLhw varchar(max);
declare @colshw varchar(max);
;with CTE as (
	select 	dateadd(m, datediff(m, 0, @start_date_range), 0) as st, 
		dateadd(m, datediff(m, 0, @end_date_range), 0) as et
	union all
	select dateadd(m, 1, st),
			et
	from CTE 
	where dateadd(m, 1, st) <= et
)
select @colshw = stuff((select '],[' + right(convert(varchar, st, 106), 8)+' Hardware'
 from CTE for xml path('')), 1, 2, '') + ']'


set @strSQLhw = '

select server_name, ' + @colshw + '  from (
select server_name, 
right(convert(varchar, dateadd(m, datediff(m, 0, [resolved_date]), 0), 106), 8)+' Hardware'
 as eom, PROD_CAT1
		from #extranetHardware
		where [resolved_date] >= ''' + 
convert(varchar, @start_date_range,
 112) + ''' and [resolved_date] < ''' +convert(varchar, @end_date_range+1,112) + ''') o
		pivot (count(PROD_CAT1) for eom in (' + @colshw +' )) as p '

print (@strSQLhw)
execute (@strSQLhw)

Open in new window

0
 

Author Comment

by:blossompark
ID: 36510867
Hi pcelba,
get error when compiling
Incorrect syntax near 'Hardware'.
0
 
LVL 42

Expert Comment

by:pcelba
ID: 36510891
Which one?
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 42

Accepted Solution

by:
pcelba earned 250 total points
ID: 36510926
Of course, quotes must be doubled, sorry:
/* pivot for hardware */
declare @strSQLhw varchar(max);
declare @colshw varchar(max);
;with CTE as (
	select 	dateadd(m, datediff(m, 0, @start_date_range), 0) as st, 
		dateadd(m, datediff(m, 0, @end_date_range), 0) as et
	union all
	select dateadd(m, 1, st),
			et
	from CTE 
	where dateadd(m, 1, st) <= et
)
select @colshw = stuff((select '],[' + right(convert(varchar, st, 106), 8)+' Hardware'
 from CTE for xml path('')), 1, 2, '') + ']'


set @strSQLhw = '

select server_name, ' + @colshw + '  from (
select server_name, 
right(convert(varchar, dateadd(m, datediff(m, 0, [resolved_date]), 0), 106), 8)+'' Hardware''
 as eom, PROD_CAT1
		from #extranetHardware
		where [resolved_date] >= ''' + 
convert(varchar, @start_date_range,
 112) + ''' and [resolved_date] < ''' +convert(varchar, @end_date_range+1,112) + ''') o
		pivot (count(PROD_CAT1) for eom in (' + @colshw +' )) as p '

print (@strSQLhw)
execute (@strSQLhw)

Open in new window

0
 

Author Comment

by:blossompark
ID: 36510930
the one you added
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 250 total points
ID: 36510989
try this


/* pivot for hardware */
declare @strSQLhw varchar(max)
, @colshw varchar(max)
, @cols varchar(max);

;with CTE as (
	select 	dateadd(m, datediff(m, 0, @start_date_range), 0) as st, 
		dateadd(m, datediff(m, 0, @end_date_range), 0) as et
	union all
	select dateadd(m, 1, st),
			et
	from CTE 
	where dateadd(m, 1, st) <= et
)
select @colshw = stuff((select '],[' + right(convert(varchar, st, 106), 8)+'] as [' + right(convert(varchar, st, 106), 8)+' Hardware'
 from CTE for xml path('')), 1, 2, '') + ']'

;with CTE as (
	select 	dateadd(m, datediff(m, 0, @start_date_range), 0) as st, 
		dateadd(m, datediff(m, 0, @end_date_range), 0) as et
	union all
	select dateadd(m, 1, st),
			et
	from CTE 
	where dateadd(m, 1, st) <= et
)
select @cols = stuff((select '],[' + right(convert(varchar, st, 106), 8)
 from CTE for xml path('')), 1, 2, '') + ']'



set @strSQLhw = '

select server_name, ' + @colshw + '  from (
select server_name, 
right(convert(varchar, dateadd(m, datediff(m, 0, [resolved_date]), 0), 106), 8)
 as eom, PROD_CAT1
		from #extranetHardware
		where [resolved_date] >= ''' + 
convert(varchar, @start_date_range,
 112) + ''' and [resolved_date] < ''' +convert(varchar, @end_date_range+1,112) + ''') o
		pivot (count(PROD_CAT1) for eom in (' + @cols +' )) as p '

print (@strSQLhw)
execute (@strSQLhw)

Open in new window

0
 

Author Comment

by:blossompark
ID: 36510990
weird!! this now working...
all i did was add
print @colshw

/* pivot for hardware */
declare @strSQLhw varchar(max);
declare @colshw varchar(max);
;with CTE as (
	select 	dateadd(m, datediff(m, 0, @start_date_range), 0) as st, 
		dateadd(m, datediff(m, 0, @end_date_range), 0) as et
	union all
	select dateadd(m, 1, st),et
	from CTE 
	where dateadd(m, 1, st) <= et
)
select @colshw = stuff((select '],[' + right(convert(varchar, st, 106), 8) +' Hardware' 
 from CTE for xml path('')), 1, 2, '') + ']'

print @colshw


set @strSQLhw = '

select server_name, ' + @colshw + '  from (
select server_name, 
right(convert(varchar, dateadd(m, datediff(m, 0, [resolved_date]), 0), 106), 8)
 as eom, PROD_CAT1
		from #extranetHardware
		where [resolved_date] >= ''' + 
convert(varchar, @start_date_range,
 112) + ''' and [resolved_date] < ''' +convert(varchar, @end_date_range+1,112) + ''') o
		pivot (count(PROD_CAT1) for eom in (' + @colshw +' )) as p '

print (@strSQLhw)
execute (@strSQLhw)

Open in new window

0
 
LVL 42

Expert Comment

by:pcelba
ID: 36511076
This sound like a nonsense...
0
 
LVL 42

Expert Comment

by:pcelba
ID: 36511096
Does it stop working when you remove the PRINT command?

And what about the version with fixed quotes around Hardware?
0
 

Author Comment

by:blossompark
ID: 36511217
i commented out the PRINT command and again the result set returnes all 0 values
0
 

Author Comment

by:blossompark
ID: 36511228
i am running it again now with the print command uncommented...
0
 

Author Comment

by:blossompark
ID: 36511278
yes it is nonsense...just had a closer look at  the returned data...sorry
0
 

Author Comment

by:blossompark
ID: 36511297
Hi Lowfatspread...going to run your code now and will update
0
 

Author Comment

by:blossompark
ID: 36511345
hi pcelba,
i was looking at the data but didn't notice that the columns were not appended with 'Hardware'
0
 
LVL 42

Expert Comment

by:pcelba
ID: 36511415
This code works fine for me:

declare @start_date_range datetime
declare @end_date_range datetime
set @end_date_range = dateadd(m, 5, getdate())
set @start_date_range = getdate()

declare @strSQLhw varchar(max);
declare @colshw varchar(max);
;with CTE as (
	select 	dateadd(m, datediff(m, 0, @start_date_range), 0) as st, 
		dateadd(m, datediff(m, 0, @end_date_range), 0) as et
	union all
	select dateadd(m, 1, st),et
	from CTE 
	where dateadd(m, 1, st) <= et
)
select @colshw = stuff((select '],[' + right(convert(varchar, st, 106), 8) + ' Hardware' 
 from CTE for xml path('')), 1, 2, '') + ']'

print @colshw

set @strSQLhw = '

select server_name, ' + @colshw + '  from (
select server_name, 
right(convert(varchar, dateadd(m, datediff(m, 0, [resolved_date]), 0), 106), 8) + '' Hardware'' 
 as eom, PROD_CAT1
		from #extranetHardware
		where [resolved_date] >= ''' + 
convert(varchar, @start_date_range,
 112) + ''' and [resolved_date] < ''' +convert(varchar, @end_date_range+1,112) + ''') o
		pivot (count(PROD_CAT1) for eom in (' + @colshw +' )) as p '

print (@strSQLhw)

Open in new window

0
 

Author Closing Comment

by:blossompark
ID: 36511419
Hi Lowfatspread,
that produces the required output, thank you

Ho pcelba,
sorry for giving you the incorrect feedback, thank you for your code...

Thank you both...I learn so much from you guys....really appreciated it :-)
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question