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 |
|
NewMedia42
Starting Member
35 Posts |
Posted - 2001-10-20 : 15:00:28
|
| I have a pretty straightforward database setup in the following way: ID (int) Name (vchar) Rating (int) ---------- -------------- ------------- 1 CompanyA 12 2 CompanyB 55 3 CompanyA 32 4 CompanyC 99 5 CompanyD 5 6 CompanyA 22Now in the past I've returned the top x rows using the following SELECT string:SELECT TOP 4 [Table].ID,[Table].Name,[Table].Rating FROM [Table] ORDER BY [Table].Rating DESCWhich returns: 4,CompanyC,99 2,CompanyB,55 3,CompanyA,32 6,CompanyA,22When I need it to return: 4,CompanyC,99 2,CompanyB,55 3,CompanyA,32 5,CompanyD,5I've tried adding distinct to the query:SELECT DISTINCT TOP 4 [Table].ID,[Table].Name,[Table].Rating FROM [Table] ORDER BY [Table].Rating DESCBut it just returns the same thing - I'm assuming it's considering ID, Name, and Rating in the DISTINCT evaluation.I've also attempted to resolve this with the GROUP BY instruction, but then I'm unable to get the ID that corresponds with the largest rating (even though they share the same name, they are different making any sort of min/max evaluation useless).Any help would be greatly appreciated! |
|
|
|
|
|
|
|