With a table setup with the following fields:
SKU, EVENTSTARTDATE, EVENTENDDATE, PRICE, (...etc...)
and housing thousands of rows here is e
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
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
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.