Solved

pivot fonction  in sql server 2005

Posted on 2011-09-09
16
182 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 41

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 41

Expert Comment

by:pcelba
ID: 36510891
Which one?
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 41

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 41

Expert Comment

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

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 41

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

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 I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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