Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi Again!I have a simple select query which is returning 10 rows:select * from TABLE1Which returns the following:Col1 Col2 Col3----- ------------ ------Item1 Description1 100Item2 Description2 99Item3 Description3 98Item4 Description4 97Item1 Description1 96Item1 Description1 95Item2 Description2 94Item2 Description2 93Item5 Description5 92Item1 Description1 91Item1 Description1 90What I want to do is return the distinct items in Col1 but in that order - ordered by Col3.Any ideas?
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2008-02-08 : 12:18:30
What do you want to do with Col3 values? do you waqnt their min or max value for each Col1?
Gopher
Yak Posting Veteran
83 Posts
Posted - 2008-02-08 : 12:20:54
Max Value really!
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2008-02-08 : 12:22:51
Ok here it is:-
SELECT t.Col1,t.Col2,t.Col3FROM(SELECT ROW_NUMBER() OVER (PARTITION BY Col1 ORDER BY Col3 DESC) AS RowNo,Col1,Col2,Col3FROMTABLE1)tWHERE t.RowNo=1
Qualis
Posting Yak Master
145 Posts
Posted - 2008-02-08 : 14:04:28
Or
Select Col1, Col2, Max(Col3) As Col3From Table1Group By Col3
madhivanan
Premature Yak Congratulator
22864 Posts
Posted - 2008-02-09 : 01:26:55
quote:Originally posted by Qualis Or
Select Col1, Col2, Max(Col3) As Col3From Table1Group By Col3
That should beSelect Col1, Col2, Max(Col3) As Col3From Table1Group By Col1, Col2MadhivananFailing to plan is Planning to fail