SQL QUERY - NO DUPLICATE RESULTS

52 Views Asked by At

I have the below table, named shop:

enter image description here

Can you suggest the query to have no duplicate "product" results associated with the most recent delivery date.

Thanks

3

There are 3 best solutions below

0
Marko On
select product, max(delivery_date) from table
group by product
0
Krzysztof K On
SELECT
product, 
MAX(delivery_date) as most_recent_delivery_date 
FROM table_name
GROUP BY product;
0
Gordon Linoff On

If you want just the product, you can use aggregation as in the other answers. If you want the complete row, then one method is:

select t.*
from (select t.*,
             row_number() over (partition by product order by delivery_date desc) as seqnum
      from t
     ) t
where seqnum = 1;