Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

sql - Select one row per index value with max column value

With a table setup with the following fields:

SKU, EVENTSTARTDATE, EVENTENDDATE, PRICE, (...etc...)

and housing thousands of rows here is example data (dates are YYMMDD, century code excluded):

1111111, 101224, 101231, 10.99
1111111, 110208, 110220, 9.99
1111111, 110301, 110331, 8.99
2222222, 101112, 101128, 15.99
2222222, 101201, 110102, 14.99
etc

I'd like to have a SELECT statement return one row per SKU with the maximum EVENTSTARTDATE without having a WHERE clause isolating a specific SKU or incomplete subset of SKUs (desired SELECT statement should return one row per SKU for all SKUs). I'd eventually like to add the criteria that start date is less than or equal to current date, and end date is greater than or equal to current date, but I have to start somewhere first.

Example results desired (for now just max date):

1111111, 110301, 110331, 8.99
2222222, 101201, 110102, 14.99
etc.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From recent versions of DB2, you can use the analytical function ROW_NUMBER()

SELECT * 
FROM (
    SELECT 
        tablename.*, 
        ROW_NUMBER() OVER (PARTITION BY sku 
                           ORDER BY eventstartdate DESC) As RowNum
        FROM tablename) X 
WHERE X.RowNum=1

For each Partition (group of SKU), the data is row numbered following the order by eventstartdate desc, so 1,2,3,...starting from 1 for the latest EventStartDate. The WHERE clause then picks up only the latest per SKU.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...