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 2008 Forums
 Transact-SQL (2008)
 Need to create a new table using rows from existin

Author  Topic 

steveg
Starting Member

2 Posts

Posted - 2011-12-09 : 12:21:15
Identifier priority accountnumber

123456 1 9999999999
123456 1 8888888888
123456 5 0000000001
123456 47 1111111111

678901 1 2222222222
678901 2 0101010101

012344 1 9998887767
987659 1 0890970707
876501 1 7645387076

Above represents sample data from my table, in total it has 1.2mil records. As you can see the Identifier field can have multiple identical records, each with a Priority number and a unique account number. It's possible for the same Priority number to exist for
the same Identifier.

I need create a new table, transforming (my word) the accountnumber values from rows to columns, by Priority in ascending order. In the event there are duplicate Priority value, just take the first accountnumber. So, my new table would look like this:

Identifier account1 account2 account3 account4 ....... up to 56 account fields
123456 9999999999 8888888888 0000000001 1111111111
678901 2222222222 0101010101
012344 9998887767
987659 0890970707
876501 7645387076

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-09 : 12:39:13
have a look at PIVOT in bol

http://msdn.microsoft.com/en-us/library/ms177410.aspx

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

Go to Top of Page

steveg
Starting Member

2 Posts

Posted - 2011-12-09 : 12:48:58
I've seen that, but I'm pretty much a newbie and can't get close. Can you provide SPECIFIC T-SQL? Thank you.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-09 : 13:02:29
can you post what you tried?

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

Go to Top of Page
   

- Advertisement -