根据组 SQL 过滤数据 Netezza

Filter data based on group SQL Netezza

请给我以下 table,我需要根据 DATE 列过滤数据, 在 Sus_ID 级别,如果 DATE 列有 NULL 获取此行,或者如果没有 NULL 记录将获取最新 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|

想要的结果应该是下面的

|Sub_ID |Cust_ID    |C_Date    |
|-------+-----------+----------|
|123456 |3432       |NULL      |
|987654 |1122       |2021-02-21|

我尝试了下面的代码,但没有用

    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

您可以在 ORDER BY 子句中使用 ROW_NUMBER() window 函数和 CASE 表达式:

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

ORDER BY子句也可以简化为:

ORDER BY C_Date IS NULL DESC, C_Date DESC

参见 demo(对于 MySql 但它是标准的 SQL)。
结果:

Subs_ID Cust_ID C_Date
123456 3432 null
987654 1122 2021-02-21

Netezza 支持 SQL 标准 NULLS FIRST 语法。我建议您使用:

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;