简体   繁体   中英

SQL - select rows greater/lesser than particular date

I have a table named edi_premise. I have four fields named customer_id, action_id, req_reason_code and trans_date in the same table.

I would like to choose all customers that have "673_03" in action_id field dated after the rows that have "ORV" in req_reason_code field.

In short, date ("ORV" in req_reason_code field) < date ( "673_03" in action_id field ).

How do I write a query to select these rows?

A pretty simple way of doing this is using conditional aggregation. It is a bit hard to tell what the data format is, but something like:

select customer_id
from edi_premise
group by customer_id
having max(case when action_id = '673_03' then dated end) >
       min(case when req_reason_code = 'ORV' then dated end);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM