Solved

pivot fonction  in sql server 2005

Posted on 2011-09-09
16
178 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
Comment Utility
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
Comment Utility
Hi pcelba,
get error when compiling
Incorrect syntax near 'Hardware'.
0
 
LVL 41

Expert Comment

by:pcelba
Comment Utility
Which one?
0
 
LVL 41

Accepted Solution

by:
pcelba earned 250 total points
Comment Utility
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
Comment Utility
the one you added
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
This sound like a nonsense...
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 41

Expert Comment

by:pcelba
Comment Utility
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
Comment Utility
i commented out the PRINT command and again the result set returnes all 0 values
0
 

Author Comment

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

Author Comment

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

Author Comment

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

Author Comment

by:blossompark
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now