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.
| Author |
Topic |
|
grimmus
Yak Posting Veteran
53 Posts |
Posted - 2011-12-09 : 07:43:01
|
| Hi,I have 3 temp tables displaying the following results1.FG_PriceListID TW_FG_Line_ID31617-----------1006212731617-----------1006212831617-----------1006212931617-----------1006213031617-----------1006213131617-----------1006213231617-----------1006213331617-----------100621342.FG_PriceListID T5_FG_Line_ID31617-----------1006213531617-----------1006213631617-----------1006213731617-----------1006213831617-----------1006213931617-----------1006214031617-----------1006214131617-----------100621423.FG_PriceListID T9_FG_Line_ID31617-----------1006214331617-----------1006214431617-----------1006214531617-----------1006214631617-----------1006214731617-----------1006214831617-----------1006214931617-----------10062150I would like to insert them into a table along side each other so the resulting table would look like :FG_PriceListID TW_FG_Line_ID T5_FG_Line_ID T9_FG_Line_ID31617-----------10062127-----------10062135-----------1006214331617-----------10062128-----------10062136-----------1006214431617-----------10062129-----------10062137-----------1006214531617-----------10062130-----------10062138-----------1006214631617-----------10062131-----------10062139-----------1006214731617-----------10062132-----------10062140-----------1006214831617-----------10062133-----------10062141-----------1006214931617-----------10062134-----------10062142-----------10062150I was hoping to do some sort of outer join but the result set is not coming back how I need it to be.Can anyone offer any advice on how to achieve this. I realize it seems a bit awkward but the data i am working with is not structured in the best way.Thanks |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-12-09 : 07:49:32
|
select t1.FG_PriceListID, t1.TW_FG_Line_ID, t2.T5_FG_Line_ID, t3.T9_FG_Line_IDfrom table_1 as t1left join table_2 as t2 on t1.FG_PriceListID = t2.FG_PriceListIDleft join table_3 as t3 on t1.FG_PriceListID = t3.FG_PriceListID No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
grimmus
Yak Posting Veteran
53 Posts |
Posted - 2011-12-09 : 07:53:38
|
| Thank you webfred.This is creating the results alright but they are getting multiplied, so the result set is 512 rows (8*8*8)I would just like the 8 results with the 4 columns of data - FG_PriceListID TW_FG_Line_ID T5_FG_Line_ID T9_FG_Line_IDThanks |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-12-09 : 08:16:02
|
select DISTINCT ... No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-12-09 : 08:19:03
|
Ah - I understand now...Sorry.Wait a moment... No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-12-09 : 08:30:20
|
[code]declare @sample1 table(FG_PriceListID int, TW_FG_Line_ID int)insert @sample1select 31617,10062127 union allselect 31617,10062128 union allselect 31617,10062129 union allselect 31617,10062130 union allselect 31617,10062131 union allselect 31617,10062132 union allselect 31617,10062133 union allselect 31617,10062134declare @sample2 table(FG_PriceListID int, T5_FG_Line_ID int)insert @sample2select 31617,10062135 union allselect 31617,10062136 union allselect 31617,10062137 union allselect 31617,10062138 union allselect 31617,10062139 union allselect 31617,10062140 union allselect 31617,10062141 union allselect 31617,10062142declare @sample3 table(FG_PriceListID int, T9_FG_Line_ID int)insert @sample3select 31617,10062143 union allselect 31617,10062144 union allselect 31617,10062145 union allselect 31617,10062146 union allselect 31617,10062147 union allselect 31617,10062148 union allselect 31617,10062149 union allselect 31617,10062150select t1.FG_PriceListID, t1.TW_FG_Line_ID, t2.T5_FG_Line_ID, t3.T9_FG_Line_IDfrom(select row_number() over (partition by FG_PriceListID order by TW_FG_Line_ID) as rnum,* from @sample1)t1left join(select row_number() over (partition by FG_PriceListID order by T5_FG_Line_ID) as rnum,* from @sample2)t2on t1.FG_PriceListID = t2.FG_PriceListID and t1.rnum = t2.rnumleft join(select row_number() over (partition by FG_PriceListID order by T9_FG_Line_ID) as rnum,* from @sample3)t3on t1.FG_PriceListID = t3.FG_PriceListID and t1.rnum = t3.rnum[/code] No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
grimmus
Yak Posting Veteran
53 Posts |
Posted - 2011-12-09 : 09:03:41
|
Thanks very much webfred, works perfect |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-12-09 : 09:07:54
|
welcome  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|
|
|