Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Convert rows to column

Author  Topic 

PHUser
Starting Member

11 Posts

Posted - 2010-05-13 : 12:38:25
Person Table
ID Name
1 ABC

Phone Table
ID Ph
1 111111
1 222222

Result should look like this,
1 ABC 111111 222222

How can this be acheived? Can we use Pivot?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-05-13 : 12:41:55
you can. but will there be always two phone numbers?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

PHUser
Starting Member

11 Posts

Posted - 2010-05-13 : 12:45:15
No number of ph numbers varies.
quote:
Originally posted by visakh16

you can. but will there be always two phone numbers?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-05-13 : 12:48:04
then you need this

http://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

PHUser
Starting Member

11 Posts

Posted - 2010-05-13 : 12:49:22
What is the alternative if i don't want to use dynamic sql? it doesn't have to necessarily be with PIVOT.
quote:
Originally posted by visakh16

then you need this

http://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-05-13 : 12:50:39
no way without dynamic sql if you dont know number of values that can come in advance

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

PHUser
Starting Member

11 Posts

Posted - 2010-05-13 : 12:52:28
Like i mentioned it doesn't have to use pivot. can we use a subquery?
quote:
Originally posted by visakh16

no way without dynamic sql if you dont know number of values that can come in advance

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-05-13 : 12:54:16
nope. even then how will determine how many values to be crosstabbed? so it has to happen dynamically

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-05-14 : 03:02:43
quote:
Originally posted by PHUser

Like i mentioned it doesn't have to use pivot. can we use a subquery?
quote:
Originally posted by visakh16

no way without dynamic sql if you dont know number of values that can come in advance

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/






Why are you not using a pivot?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -