我正确地分割了一个复杂的CASE WHEN查询,以显示逻辑每一位的列。但是,我不知道如何仅创建一列来显示case语句的ELSE部分的结果。有没有办法将复杂的CASE WHEN语句限制为仅ELSE结果?
when (bool_or(description = 'bears')
and bool_or(description = 'giraffes')
and bool_or(description = 'gorillas')
and bool_or(description = 'deer')
and bool_and(description in ('baboons', 'lions','cats', 'dogs')))
then 'mammal'
else 'insect'
我将其细分为:
when (bool_or(description = 'bears')
and bool_or(description = 'giraffes')
and bool_or(description = 'gorillas')
and bool_or(description = 'deer')
and bool_and(description in ('baboons', 'lions','cats', 'dogs')))
then 'mammal' END AS mammal
但是我想为其他创建一个列,以便显示昆虫的数量。
更新:为澄清起见,其他语句都排在一长串其他都链接在一起的CASE WHEN语句之后。
case when bool_or(description = 'xxx')
and bool_and(description in ('xxx','xxx', 'xxx', 'xxx'))
then 'xxx'
when bool_or(description = 'xxx')
and bool_and(description in ('xxx'))
then 'xxx'
when bool_or(description = 'xxx)
and bool_or(description = 'xxx')
and bool_and(description in ('xxx))
then 'xxx' END AS xxx
这种情况一直持续到ELSE。
答案 0 :(得分:0)
由于您没有与我们分享完整的查询-我只能建议您对布尔逻辑进行布尔简化,例如使用https://www.dcode.fr/boolean-expressions-calculator或http://www.32x8.com/,也可以在YouTube上搜索“卡诺映射”,以了解该技术的工作原理。
另一个想法是创建一个表,其中包含您的CASE值,每个要区分的值组合均包含1列。例如
case_value | combine_1 | combine_2
-----------+-----------+-----------
bears | true | false
-----------+-----------+-----------
giraffes | false | false
-----------+-----------+-----------
gorillas | true | true
-----------+-----------+-----------
deer | true | false
-----------+-----------+-----------
lions | false | true
-----------+-----------+-----------
cats | false | false
-----------+-----------+-----------
然后您可以使用类似的
SELECT table.* FROM table LEFT JOIN cases ON description = case_value WHERE NOT combine_2
您可以尝试
SELECT * FROM table WHERE description NOT IN (
'bears', 'giraffes', 'gorillas', 'deer', 'baboons', 'lions', 'cats', 'dogs'
)