db2 - SQL query for the given select operation -
consider table t, columns
- id = e.g customer id
- expense = amount spent on buying item
- date = date of transaction
- item = item bought
i want perform following select operation on t.
i want find each id, expensive item bought on earliest date.
for example if table had 3 records follows
id expense date item 1 1000 10/20/2015 1 1000 10/21/2015 b 1 200 10/15/2015 c it should pick first row.
i wrote following not seem work
select t.id. t.expense, t.date, t.item t inner join (select id, max(expense), min(date) t group id) w on t.id = w.id , t.expense=w.expense , t.date=w.date; please give suggestions. thanks
try:
select * (select row_number() on (partition id order expense desc, date asc) rn, id, expense, date, item t)t rn=1
Comments
Post a Comment