Please I have the below table, I need to filter the data based on the DATE column, In the Sus_ID level if the DATE column has NULL get this row or if the hasn't NULL record will get the row for the newest DATE
|Sub_ID |Cust_ID |C_Date |
|-------+-----------+----------|
|123456 |2233 |2021-02-21|
|123456 |2211 |2021-01-21|
|123456 |3432 |NULL |
|987654 |1122 |2021-02-21|
|987654 |3322 |2021-01-21|
the desired result should be the below
|Sub_ID |Cust_ID |C_Date |
|-------+-----------+----------|
|123456 |3432 |NULL |
|987654 |1122 |2021-02-21|
I tried the below code but it didn't work
Subs_ID,
CASE
WHEN C_Date IS NULL THEN Cust_ID
ELSE (FIRST_VALUE(Cust_ID) OVER (PARTITION BY Subs_ID ORDER BY C_Date DESC )) END AS Cust_ID_N
FROM
tbl
You can use ROW_NUMBER()
window function with a CASE
expression in the ORDER BY
clause:
SELECT Subs_ID, Cust_ID, C_Date
FROM (
SELECT *,
ROW_NUMBER() OVER (
PARTITION BY Subs_ID
ORDER BY CASE WHEN C_Date IS NULL THEN 1 ELSE 2 END, C_Date DESC
) rn
FROM tablename
) t
WHERE rn = 1
The ORDER BY
clause can be also simplified to:
ORDER BY C_Date IS NULL DESC, C_Date DESC
See the demo (for MySql but it is standard SQL).
Results:
Subs_ID | Cust_ID | C_Date |
---|---|---|
123456 | 3432 | null |
987654 | 1122 | 2021-02-21 |
Netezza supports the SQL Standard NULLS FIRST
syntax. I would recommend that you use that:
select t.*
from (select t.*,
row_number() over (partition by sub_id order by c_date desc nulls first) as seqnum
from t
) t
where seqnum = 1;
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.