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 |
|
sneupane
Starting Member
28 Posts |
Posted - 2010-05-07 : 09:45:53
|
| I have two tables with no common key.Table1P Qt Rta 15 48b 16 50c 20 39a 15 32c 21 31Table 2Numb UtNum1 26Num2 27Num3 28Num2 62Result I needP Qt Rt Numb Uta 30 80 Num1 26b 16 50 Num2 89c 41 70 Num3 28I tried to join it even though it does not have common key in the following way.select P,sum(Qt),sum(Rt),Numb,sum(Ut)From Table1,Table2Group By P,NumbThe query was run but gave me very wrong results.I tried union too it did not work.Could anyone help me in this error?SAROJ |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-05-07 : 10:31:42
|
| I don't know what purpose it will serve you.I just did it for fun.Declare @Table1 table(p varchar(10), QT int, RT int)Declare @Table2 table(Numb varchar(10),Ut int)Insert into @Table1Select 'a', 15 ,48 unionSelect 'b', 16 ,50 unionSelect 'c', 20 ,39 unionSelect 'a', 15 ,32 unionSelect 'c', 21 ,31 Insert into @Table2 Select 'Num1', 26 unionSelect 'Num2', 27 unionSelect 'Num3', 28 unionSelect 'Num2', 62 Select T1.p, T1.qt, T1.rt, T2.Numb, T2.Utfrom(Select Rank() over (order by Numb) as Srno, Numb , sum(Ut) Ut from @Table2 group by Numb) As T2Join(Select Rank() over (order by P) as Srno, P , sum(qt) QT , sum(rt) Rt from @Table1 group by P) As T1on T1.Srno = T2.SrnoRegards,BohraI am here to learn from Masters and help new bees in learning. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-07 : 13:35:40
|
| i cant understand how you make sure the columns get joined based on order in which they're given above. I think you're assuming that values will always be in sequential order as per sample data which i dont think will always be true. please note that unless you dont have a column with which you specify order you cant guarantee the order of retrieval from each table and joining.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-05-07 : 13:41:55
|
quote: Originally posted by visakh16 I think you're assuming that values will always be in sequential order as per sample data which i dont think will always be true. please note that unless you dont have a column with which you specify order you cant guarantee the order of retrieval from each table and joining.
Hi Visakh,I totally agree with you that unless you a proper joining column the data may be incorrect..As i said in my earlier post, I just did it for fun..Regards,BohraI am here to learn from Masters and help new bees in learning. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-07 : 13:44:13
|
oh ok. just told that it wont work always ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|