We use a database to capture transactions / deals and I need to find out which one is the most up-to-date deal.
Action
is set to "Insert" and the column Modified
is set to "No".Action
= "Delete"; Modified
= "Yes" AND Action
= "Insert"; Modified
= "Yes".Action
= "Delete"; Modified
= "No"Here are some examples:
Record
1.Record
4.Record
11 to be returned.Thanks, Daniel
If I followed you correctly, you can filter on the latest record per deal with a subquery, and then ignore deals whose last status is a deletion:
select t.*
from mytable t
where
t.record = (select max(t1.record) from mytable t1 where t1.dealID = t.dealID)
and t.action <> 'Delete'
As far as concerns, you don't need to use column modified
to get the result that you want.
For performance with this query, consider an index on (dealID, record, action)
.
You can also express this with row_number()
, if your database supports window functions:
select record, action, modified, dealID
from (
select t.*, row_number() over(partition by dealID order by record desc) rn
from mytable t
) t
where action <> 'Deleted'