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 |
|
DaCheese
Starting Member
3 Posts |
Posted - 2002-11-20 : 06:33:26
|
| Ok this seams like an extremely simple SELECT query, but no amounts of TOPs and DISTINCTS and GROUP BYs seem to do it, so I'm wondering if you could put me out of my misery;I have 2 tables, lets sayTableOne---------------ONE_IDONE_NameTableTwo---------------TWO_IDTWO_OneIDTWO_NameTWO_RankSo basically each record in TableTwo has a foreign key link )TWO_OneID) thingamajig to TableOne. Multiple records in TableTwo can link to one record in TableOne (natch).What I want to do is SELECT all the records in TableOne that have at least one TableTwo record - but only return the top 1 table 2 record.See if I do;SELECT DISTINCT TableOne.ONE_ID, TableOne.ONE_Name, TableTwo.TWO_ FROM TableOne INNER JOIN TableTwo ON TableOne.ONE_ID = TableTwo.TWO_OneID ORDER BY TableOne.ONE_ID, TableTwo.TWO_RankI getONE_ID TWO_ID TWO_Rank-----------------------1 1 1001 2 971 3 912 4 992 6 813 5 99Where what I want to get is;ONE_ID TWO_ID TWO_Rank-----------------------1 1 1002 4 993 5 99Hope that makes sense. |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2002-11-20 : 07:17:56
|
| select ONE_ID, TWO_ID, TWO_Rankfrom TableOne , Table2where TWO_ID = (select top 1 TWO_ID from table2 where TWO_OneID = ONE_ID order by TWO_Rank desc) as aand TWO_OneID = ONE_ID==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
DaCheese
Starting Member
3 Posts |
Posted - 2002-11-20 : 08:36:10
|
| beautiful, thanks a lot :) |
 |
|
|
|
|
|