I have a follow up question to a question I posted yesterday. Today there are changes to the logic needed. SQL Multi Conditional CASE WHEN issues
Example data table is at the bottom of this post. The original code was written around the following logic:
Credit to user Gordon Linoff who provided the following code which worked perfectly:
select o.*
from (select o.*,
row_number() over (partition by order_number
order by (case when payment_method = 'Credit Card' then 1 else 2 end),
order_date desc
) as seqnum
from orders o
) o
where seqnum = 1;
There have been updates to the requirements in the logic which have been expanded to the following:
Can this be accomplished by modifying the CASE WHEN in Gordon's code? Or does it require more than that simple of a change.
Original Data Table in question:
Order Number | Payment Method | Order Date |
---|---|---|
120 | Cash | 01/01/2021 |
175 | Credit Card | 01/02/2021 |
209 | Cash | 01/03/2021 |
209 | Credit Card | 01/04/2021 |
209 | Personal Check | 01/05/2021 |
209 | Credit Card | 01/06/2021 |
209 | Cash | 01/07/2021 |
209 | Personal Check | 01/08/2021 |
277 | Credit Card | 01/09/2021 |
301 | Cash | 01/10/2021 |
333 | Personal Check | 01/11/2021 |
333 | Cash | 01/12/2021 |
333 | Cash | 01/13/2021 |
333 | Personal Check | 01/14/2021 |
400 | Credit Card | 01/15/2021 |
551 | Credit Card | 01/16/2021 |
551 | Cash | 01/17/2021 |
680 | Personal Check | 01/18/2021 |
If I get this logic correctly, it is:
You can get the auxiliary information using another subquery and window functions:
select o.*
from (select o.*,
row_number() over (partition by order_number
order by (case when cnt = 2 and num_cc = 0 then order_date end) desc,
order_date asc
) as seqnum
from (select o.*,
count(*) over (partition by order_number) as cnt,
sum(case when payment_method = 'Credit Card' then 1 else 0 end) over (partition by order_number) as num_cc
from orders o
) o
) o
where seqnum = 1;