简体   繁体   中英

Finding max of transaction_date for corresponding code

I'm trying to find out how to find max of transaction_date per EAN_code

My table looks like:

Transaction_Date    EAN_Code
09/04/2018       3029440000286
09/04/2018       3029440000286
08/04/2018       5000128221139
14/04/2018       5000128221139
08/04/2018       5000128221139
10/04/2018       5000128221108

Essentially what we need to do is for the list of items we want to pull out the latest date that it was sold across, eg one row per product, last date sold.

Both columns have non distinct values.

Simply do a GROUP BY . Use MAX() to get the latest date for each product.

select EAN_Code, max(Transaction_Date)
from tablename
group by EAN_Code

You could use ROW_NUMBER/RANK :

SELECT *
FROM (SELECT *,ROW_NUMBER() OVER(PARTITION BY Ean_Code 
                           ORDER BY Transaction_Date DESC) AS rn
      FROM table_name) s
WHERE s.rn = 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM