我有两列表1中的数据,它们有SKU编号和新SKU编号。我的目标是将这些数据作为单列进行查询,如"所需"表中所示
+-------+--------+ +---------+
| SKU | NEW_SKU| | Desired |
+-------+--------+ |---------+
| null | 1 | | 1 |
| null | 1 | | 1 |
| 2 | null | | 2 |
| 2 | 3 | | 3 |
| 2 | 5 | | 5 |
+-------+--------+ +---------+
目前我有:
SELECT
SKU
CASE WHEN (SKU IS NOT NULL AND NEW_SKU IS NOT NULL) THEN new_sku
WHEN (SKU IS NULL AND NEW_SKU IS NOT NULL) THEN new_sku
ELSE SKU END AS Product_number
FROM Table1
此处使用COALESCE
:
SELECT SKU, NEW_SKU, COALESCE(NEW_SKU, SKU) AS Product_number
FROM Table1;
如上所述,如果COALESCE
函数不是NULL
,则它将使用NEW_SKU
值,否则将回退到使用SKU
值。