Select one row per index value with max column value

自闭症网瘾萝莉.ら 提交于 2019-12-17 14:55:06

问题


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.

回答1:


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.




回答2:


Have a look at GROUP BY and HAVING clauses.

select sku, max(eventstartdate)
FROM TABLE
group by sku
having eventstartdate <= sysdate

Edit: added HAVING statement




回答3:


other solution

 select distinct f3.* 
 from  yourtable f1
 inner join lateral
       (
        select * from yourtable f2
        where f1.SKU=f2.SKU
        order by EVENTSTARTDATE desc, EVENTENDDATE desc
        fetch first rows only
        ) f3 on 1=1


来源:https://stackoverflow.com/questions/4937046/select-one-row-per-index-value-with-max-column-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!