gpt4 book ai didi

sql - SQL:修改具有唯一值的查询结果行

转载 作者:行者123 更新时间:2023-12-03 17:45:09 26 4
gpt4 key购买 nike

我有一个sqlite查询,返回带有列[字母,数字]的以下内容:

("a", 1) 
("a", 2)
("b", 3)
("c", 3)


如果字母是不同的,我想将数字列检索为0。怎么做?

预期产量:

("a", 1) 
("a", 2)
("b", 0)
("c", 0)

最佳答案

SELECT tba.mychar
-- if tbu.mychar is null, then the letter is not unique
-- when it happens, the letter is not "unique" thus use the number column
-- else use zero for "unique" letters
, CASE WHEN tbu.mychar IS NULL THEN tba.mynum ELSE 0 END AS newnum
FROM mytab tba
LEFT JOIN (
-- this subquery only returns the letters that don't repeat
SELECT mychar
FROM mytab
GROUP BY mychar
HAVING COUNT(*) = 1
) AS tbu ON tba.mychar=tbu.mychar

关于sql - SQL:修改具有唯一值的查询结果行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12677133/

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