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
 General SQL Server Forums
 New to SQL Server Programming
 Insert to a table from the data from another data

Author  Topic 

shiyam198
Yak Posting Veteran

94 Posts

Posted - 2011-05-30 : 14:42:05
Hi,

I have a table called temp which has two colummns.

Account No | Data
1 | Apple
1 | Orange
1 | Pear
2 | Apple
2 | Pear

I have another table "RealData" that has

AccountNo | Fruit01 | Fruit02 | Fruit03

AccountNo is unique here.

I need to get the data from "temp" and load it to "ReadData".

for e.g. "RealData" will become:

AccountNo | Fruit01 | Fruit02 | Fruit03
1 | Apple | Orange | Pear


How can I do this? I appreciate your time.

Regards,
Shiyam


jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-05-30 : 15:54:47
You need to UNPIVOT the data

DECLARE @Table Table (AccountNo int ,Fruit01 varchar(10),Fruit02 varchar(10))

INSERT INTO @Table
SELECT 1223,'banana','apple'

SELECT AccountNO,Fruit
FROM
(select accountNo,Fruit01,Fruit02
from @table
) p
UNPIVOT
( Fruit For Fruits in ([Fruit01],[Fruit02])


) upvt


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page
   

- Advertisement -