Solved

pivot fonction  in sql server 2005

Posted on 2011-09-09
16
191 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard 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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

707 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