Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I display repeating feilds horizontal using pivot

Posted on 2010-08-23
5
Medium Priority
?
354 Views
Last Modified: 2012-05-10
I have this data

ID      Name      Code      Type
1      Sam      205      office
2      Sam      778      home
3      Sam      678      cell
4      Bob      789      office
5      Bob      167      home
6      Tim      556      cell

I want to create a view that will display the data this way

Name      Code1      Type1      Code2      Type2      Code3      Type3
Sam        205         office         778         home          678         cell
Bob        789         office         167         home
Tim                                                                              556         cell


How can I do this using pivot in SQL Server 2005
0
Comment
Question by:vraj20
[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
  • 3
  • 2
5 Comments
 
LVL 41

Expert Comment

by:ralmada
ID: 33503865
try the below:

DECLARE @strSQL varchar(8000) 
DECLARE @COLS VARCHAR(8000) 


SELECT @COLS = 	coalesce(@COLS+',', '') + 
		'max(case when [rn] = ' + rn + ' then [Code] end) as [Code' + cast(rn as varchar) + ']' 
		', max(case when [rn] = ' + rn + ' then [Type] end) as [Type' + cast(rn as varchar) + ']' 

from (
	select distinct row_number() over (partition by Name order by ID) rn from yourtable
) a 

set @strSQL = 'select 	Name, ' + @cols + ' from (select *, row_number() over (partition by Name order by ID) rn from yourtable ) t1 group by Name'

exec(@strSQL)

Open in new window

0
 

Author Comment

by:vraj20
ID: 33505352
'max(case when [rn] = ' + rn + ' then [Code] end) as [Code' + cast(rn as varchar) + ']'

What does rn represent?
0
 
LVL 41

Expert Comment

by:ralmada
ID: 33505418
rn is just a column that I'm creating using the row_number() function to number code and types column like code1, code2, etc....
 
Hope this helps
0
 

Author Comment

by:vraj20
ID: 33511530
Thank you for your assistance. When I run your script I get these error message

Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ', max(case when [rn] = '.
Msg 102, Level 15, State 1, Line 11
Incorrect syntax near 'a'.
0
 
LVL 41

Accepted Solution

by:
ralmada earned 2000 total points
ID: 33512072
yep, need a cast there and missed a plus sign.
DECLARE @strSQL varchar(8000) 
DECLARE @COLS VARCHAR(8000) 


SELECT @COLS = 	coalesce(@COLS+',', '') + 
		'max(case when [rn] = ' + rn + ' then [Code] end) as [Code' + cast(rn as varchar) + ']' +
		', max(case when [rn] = ' + rn + ' then [Type] end) as [Type' + cast(rn as varchar) + ']' 

from (
	select distinct cast(row_number() over (partition by Name order by ID) as varchar) rn from yourtable
) a 

set @strSQL = 'select 	Name, ' + @cols + ' from (select *, row_number() over (partition by Name order by ID) rn from yourtable ) t1 group by Name'

exec(@strSQL)

Open in new window

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

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

704 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