I'm trying to rank accounts of a customer by their first payment date. Sometimes the first account they open is never funded and thus has a date '1900-01-01' for "First_pay_date". I want to keep that row of info but do not want to include it in the rank.
Current outcome with code:
| CUST | ACCT | FIRST_PAY_DATE | RANK_BY_FIRST_PAY |
|---|---|---|---|
| JOHN H | JOHNH1 | 1900-01-01 | NULL |
| JOHN H | JOHNH2 | 2000-02-25 | 2 |
| JOHN H | JOHNH3 | 2001-03-21 | 3 |
| JOHN H | JOHNH4 | 2002-12-01 | 4 |
Desired Result:
| CUST | ACCT | FIRST_PAY_DATE | RANK_BY_FIRST_PAY |
|---|---|---|---|
| JOHN H | JOHNH1 | 1900-01-01 | 0 |
| JOHN H | JOHNH2 | 2000-02-25 | 1 |
| JOHN H | JOHNH3 | 2001-03-21 | 2 |
| JOHN H | JOHNH4 | 2002-12-01 | 3 |
SELECT
cust,
acct,
first_pay_date,
CASE
WHEN first_pay_date <> '1900-01-01' THEN RANK() OVER(PARTITION BY cust
ORDER BY
first_pay_date)
END rank_by_first_pay
FROM
acct_table
ORDER BY
first_pay_date ASC
