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)
 Insert Query - best solution?

Author  Topic 

Looper
Yak Posting Veteran

68 Posts

Posted - 2010-04-19 : 04:43:05
I have a table(Table1) with the following fields:

PayNo
PayCode
Code1
Amount1
Code2
Amount2
.
.
.etc upto Code50, Amount50

I want to insert them into another table(Table2) which is structured as follows:

PayId int- this is a foreign key to another table(I would get this from table3 where payNo and paycode match)
Code (varchar)
Amount (decimal)

What is the best way of doing this without having an insert query repeated 50 times for each code1, code2 ... etc?

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-19 : 04:45:49
Unpivot:

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


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-19 : 04:46:18
use UNPIVOT operator and get Code1,...Code50 as well as Amounts as rows. then its just a matter of simple join with Table3 to get PayId



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

Go to Top of Page

Looper
Yak Posting Veteran

68 Posts

Posted - 2010-04-19 : 05:14:08
Thanks - I am new to this and am a little confused by unpivot, could you show me an example for my scenerio?

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-19 : 05:24:00
quote:
Originally posted by Looper

Thanks - I am new to this and am a little confused by unpivot, could you show me an example for my scenerio?





--sample data preparation dont worry about this
DECLARE @Test table
(
PayNo int,
PayCode varchar(10),
Code1 varchar(10),
Amount1 int,
Code2 varchar(10),
Amount2 int
)


INSERT INTO @Test
VALUES (101,'A001','UGKI',100,'kjh',123),
(102,'A002','UGKI',124,'sadg',345),
(104,'A004','few',678,'savd',563),
(105,'A005','sadv',658,'sav',142),
(106,'A006','awv',547,'wer',547),
(109,'A009','svvs',326,'wrwre',457)


--actual solution
SELECT a.PayNo,a.PayCode,a.value,b.val1
FROM
(
select PayNo,PayCode,value,
ROW_NUMBER() OVER (PARTITION BY PayNo,PayCode ORDER BY PayNo) AS Seq
from @Test
unpivot(value for cat in (Code1,Code2))u
)a
JOIN
(

select PayNo,PayCode,val1,
ROW_NUMBER() OVER (PARTITION BY PayNo,PayCode ORDER BY PayNo) AS Seq
from @Test
unpivot(val1 for cat1 in (Amount1,Amount2))v
)b
ON a.PayNo=b.PayNo
AND a.PayCode=b.PayCode
AND a.Seq=b.Seq

output
----------------
101 A001 UGKI 100
101 A001 kjh 123
102 A002 UGKI 124
102 A002 sadg 345
104 A004 few 678
104 A004 savd 563
105 A005 sadv 658
105 A005 sav 142
106 A006 awv 547
106 A006 wer 547
109 A009 svvs 326
109 A009 wrwre 457




and remember you need one more join in your case with table3 to get payid


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

Go to Top of Page

Looper
Yak Posting Veteran

68 Posts

Posted - 2010-04-19 : 05:46:45
Cheers for your help - I am getting there slowly but surely. One more question - The PayNo and PayCode are only used to get the payid from Table3 and will not be inserted into Table2 so do this need to be in the select queries for a and b?
Go to Top of Page
   

- Advertisement -