序列号 1 中的字段是序列号 2 中的 Null Return 字段

Field in Sequence number 1 is Null Return field in Sequence number 2

我最初在 where 语句中放置了一个 seq1(最近的物理地址)将是我想要的,但如果它是 LIKE ('SEE%') 则给出邮寄地址。但是,我注意到在某些情况下它会说 "See Mailing" 然后邮件中有一个空值。当发生这种情况时,我想 return 序列 2 的邮寄地址。我在下面提供了一个正在发生的事情以及需要什么的例子。

示例数据:

Cust_ID  SeqNum PhysicalAddress  MailingAddress
1          1        1 Main St     PO Box 1
1          2        1 Main St     PO Box 1
1          3        1 First Ave   PO Box 1
2          1        See Mailing     Null
2          2        See Mailing   2 Main St
2          3        See Mailing   2 Main St
3          1        See Mailing   3 Main St
3          2        3 Main St     3 Main St
3          3        3 Third St    3 Third St

期望的结果:

Cust_ID  SeqNum      Address
1           1       1 Main St
2           2       2 Main St
3           1       3 Main St

请帮我找出构建此查询的最佳方法。我应该在 SELECT 子句中构建一个 case 还是在 WHERE 子句中组合 AND 和 OR?

这个怎么样?

SELECT  "Cust_ID"
,       "SeqNum"
,       CASE WHEN "PhysicalAddress" = 'See Mailing' THEN "MailingAddress" ELSE "PhysicalAddress" END AS "Address"
FROM
(
    SELECT T.*, ROW_NUMBER() OVER(PARTITION BY "Cust_Id" ORDER BY "SeqNum") AS "RowNum"
    FROM   "Table" T
    WHERE  "MailingAddress" IS NOT NULL
    OR     "PhysicalAddress" <> 'See Mailing'
)
WHERE "RowNum" = 1

其他方法(如果只有一行地址为空,则不删除 ID)

select * from (
    select f1.*,  
    rownumber() over(partition by f1.Cust_ID 
    order by case when (case when f1.PhysicalAddress='See Mailing' then f1.MailingAddress else f1.PhysicalAddress end) is null then cast(null as integer) else SeqNum  end)  rang 
    from FormatedAdresse f1
) tmp where rang=1