首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套case内的case语句

嵌套case内的case语句
EN

Stack Overflow用户
提问于 2020-04-25 03:12:00
回答 1查看 27关注 0票数 0

我会把这个问题说得更准确

我有这些数据

代码语言:javascript
复制
id   product  count  
1      a        10
1      b        20
1      c        10
1      d        30

我想要这样的结果

因为产品A和B都有值,所以我想把它们算作1,所以结果应该是3(不同的A,C,D

如果有任何乘积(A有值,但B没有,反之亦然),那么结果也必须是3

如果产品A和B都没有值,则结果应为2

如何在oracle plsql中使用case语句来实现这一点

EN

回答 1

Stack Overflow用户

发布于 2020-04-25 20:19:41

我不确定您是如何定义either count of a or either count of b not both的,但是如果您显式地定义了它,那么您可以尝试这个:

代码语言:javascript
复制
with t as (
    select 1 as id, 'a' as product from dual
    union all
    select 1 as id, 'b' as product from dual
    union all
    select 1 as id, 'c' as product from dual
    union all
    select 1 as id, 'd' as product from dual
) select id, 
        product, 
        count( case when product in ('c', 'd', 'a') then 1 end ) --change 'a' to 'b' to get the the result for 'b'
from t
group by id, product;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61415781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档