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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Help with OVER PARTITION

Author  Topic 

Mondeo
Constraint Violating Yak Guru

287 Posts

Posted - 2009-05-14 : 12:38:36
Hi,

Got this table

Model Price Column 3 Column 4 Column 5 etc
A3 123 x x x
A3 198 x x x
A3 225 x x x
A4 234 x x x
A4 227 x x x
A5 121 x x x
A5 234 x x x
A5 232 x x x

I want to return 1 row for each distinct model with the lowest price. I believe I can user OVER PARTITION but not sure how. Thanks

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-05-14 : 12:41:11
select model, price, col3, col4, col5
from (
select model, price, col3, col4, col5, row_number() over (partition by model order by price desc) AS recID
from table) as f
where recid = 1



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-14 : 14:15:39
[code]SELECT *
FROM
(
SELECT Model, Price, [Column 3], [Column 4], [Column 5],MIN(Price) OVER (PARTITION BY Model) AS MinPrice
FROM Table
)t
WHERE Price=MinPrice
[/code]
Go to Top of Page
   

- Advertisement -