gpt4 book ai didi

sql - 简化嵌套的 case when 语句

转载 作者:行者123 更新时间:2023-11-29 11:33:37 25 4
gpt4 key购买 nike

下面是我当前的 SELECT CASE 语句:

SELECT CASE 
WHEN edition = 'STAN' AND has9 = 1 THEN '9'
WHEN edition = 'STAN' AND has8 = 1 THEN '8'
WHEN edition = 'STAN' AND has7 = 1 THEN '7'
WHEN edition = 'STAN' AND hasOLD = 1 THEN 'OLD'
WHEN edition = 'SUI' AND has_s9 = 1 THEN 'S9'
WHEN edition = 'SUI' AND has_s8 = 1 THEN 'S8' ELSE 'S7' END AS version

不想一直重复edition='xxx'的条件,比如

CASE WHEN edition = 'STAN' AND has9 = 1 THEN '9' ELSE WHEN has8 = 1 THEN '8' ELSE WHEN has7 = '7' ELSE WHEN edition 'SUI' AND has_s9 = 1 THEN 'S9' ELSE ...

在 Excel 中这很容易,但我如何在 PostgreSQL 中编译它?

最佳答案

试试这个

SELECT CASE 
WHEN edition = 'STAN' THEN
CASE
WHEN has9 = 1 THEN '9'
WHEN has8 = 1 THEN '8'
WHEN has7 = 1 THEN '7'
WHEN hasOLD = 1 THEN 'OLD'
END
WHEN edition = 'SUI' THEN
CASE
WHEN has9 = 1 THEN 'S9'
WHEN has8 = 1 THEN 'S8'
END
ELSE 'S7' END AS version

关于sql - 简化嵌套的 case when 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23823179/

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