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 |
|
BaggaDonuts
Yak Posting Veteran
52 Posts |
Posted - 2007-10-18 : 12:59:38
|
| If I have a table of values:Key Type Priority7 1 68 2 411 3 415 4 816 3 3I want to select all rows that have a unique Type. if the Type is the same as another row, i want to return the one with the lower Priority value. In the case above, Keys 11 and 16 have the same Type. In that case, I'd want to return 16 since it has a lower Priority value.how would i do that? |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-10-18 : 13:07:10
|
SELECT Type, MIN(Priority) AS PriorityFROM YourTableGROUP BY TypeIf you also want the key column, then you'll need a derived table:SELECT t1.Key, t2.Type, t2.PriorityFROM YourTable t1INNER JOIN( SELECT Type, MIN(Priority) AS Priority FROM YourTable GROUP BY Type) t2ON t1.Type = t2.Type AND t1.Priority = t2.Priority Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-10-18 : 13:09:48
|
| [code]Declare @T Table ([Key] int, Type int, Priority int)Insert into @T SELECT 7, 1 ,6 union allSELECT 8, 2 ,4 union allSELECT 11, 3 ,4 union allSELECT 15, 4 ,8 union allSELECT 16, 3 ,3Select T3.*From @T T3Join ( Select distinct T.Type , T2.Priority from @T T join (Select Type, Min(Priority) Priority FROM @T Group by Type ) T2 ON T.Type = T2.Type and T.Priority = T2.Priority ) T4 on T3.Type = T4.Type And T3.Priority = T4.Priority[/code]Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
BaggaDonuts
Yak Posting Veteran
52 Posts |
Posted - 2007-10-18 : 13:12:34
|
| awesome works perfectly. Thanks Tara |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-19 : 01:23:19
|
And with the new ROW_NUMBER() function in SQL Server 2005SELECT Key, Type, Priority FROM (SELECT Key, Type, Priority, ROW_NUMBER() OVER (PARTITION BY Type ORDER BY Priority) AS RecID FROM Table1) AS d WHERE RecID = 1 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|
|
|
|
|