【问题标题】:case statement inside nested case嵌套 case 中的 case 语句
【发布时间】:2020-04-24 19:11:59
【问题描述】:

我会让这个问题更准确

我有这个数据

id   product  count  
1      a        10
1      b        20
1      c        10
1      d        30

我想要这样的结果

由于产品 A 和 B 都有值,我想将它们计为 1,因此结果应该计数(不同的 A、C、D)为 3

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

如果产品 A 和 B 都没有值,那么结果应该是 2

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

【问题讨论】:

    标签: plsql oracle-sqldeveloper


    【解决方案1】:

    我不确定你如何定义either count of a or either count of b not both,但如果你明确定义了它,那么你可以试试这个:

    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;
    

    【讨论】:

      猜你喜欢
      • 2011-03-12
      • 2016-12-10
      • 1970-01-01
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多