I have got next SQL query
select * from ( select * from prices as p order by p.productId, p.googlePrice ) as t
It returns next result
I want to get the same table for each productId with min googlePrices
I have tried to use group by, having... something like this:
select t.productId, MIN(t.googlePrice), t.priceRaw, t.id, t.title from prices t group by t.productId
But it isn't a result that I need.
I need a table with records for each productId min googlePrice. I've marked records that I need with green line.
I can't solve the task. Help please!
You can use a windows function, partitioning by productId. Based on your question, not sure if you want a new column with the min_price, or rather, just the records with the min price. Both options below.
Query #1 (if you just want another column and display all rows)
Query #2 (if you want to display only the min rows)
View on DB Fiddle
UPDATE If you have ties for lowest price by productId, and want to display ALL with the lowest value, then use
RANK()
instead ofROW_NUMBER()
.