Author |
Topic |
vikas.wac
Starting Member
1 Post |
Posted - 2011-07-06 : 18:33:46
|
Hi,I am stuck in a SQL query. I have 2 tables say t1 and t2.I have to join these two tables using a key. This is very simple. I have done that. t1 structure :key, lat, long435, 29, -56 t2 structure :key, freqa, freqb435, 3000, 4000435, 2000, 1100435, 8000, 7500t2 has multiple number of records for the same keysimple inner join is giving me this:key, lat, long, freqa, freqb435, 29, -56 , 3000, 4000435, 29, -56 , 2000, 1100435, 29, -56 , 8000, 7500my desired output should have only unnique records and i want to update the table with additional columns from the same key records.my desired output structure :key, lat, long, freqa, freqb ,freqa, freqb, freqa, freqb435, 29, -56 , 3000, 4000 ,2000, 1100 8000, 7500Could you please help me out with this query. |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-07-06 : 19:31:07
|
with cte as(select *, seq = row_number() over (partition by key order by freqa) from t2)select t1.*, t2.freqa, t2.freqb, t3.freqa, t3.freqb, t4.freqa, t4.freqbfrom t1join cte t2on t1.key = t2.key and t2.seq = 1join cte t3on t1.key = t3.key and t3.seq = 2join cte t4on t1.key = t4.key and t4.seq = 3==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
|
|
|