gpt4 book ai didi

sql - 选择 'EXIST' 或 'NotExist' 作为列

转载 作者:行者123 更新时间:2023-12-02 07:28:52 24 4
gpt4 key购买 nike

我有以下 SQL,

SELECT SKU FROM Products WHERE SKU IN
(
'SKU1',
'SKU2',
'SKU3',
'SKU4',
......
......
......

'SKUN',
)

我需要选择的是

 SKU    Exist
----------
SKU1 Exist
SKU2 NotExist
SKU3 NotExist
................
................
................
SKUN NotExist

最佳答案

WITH SKUs AS (
SELECT SKU
FROM (VALUES ('SKU1'),('SKU2'),...) AS T(SKU)
)
SELECT SKUs.SKU
,CASE WHEN Products.SKU IS NULL THEN 'NotExists' ELSE 'Exists' END
FROM SKUs
LEFT JOIN Products
ON Products.SKU = SKUs.SKU

关于sql - 选择 'EXIST' 或 'NotExist' 作为列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24379955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com