【问题标题】:Selecting from Same Column into different variables从同一列中选择不同的变量
【发布时间】:2021-09-14 04:16:57
【问题描述】:
 Values                 Id's
    
    1                   120
    1                   120
    0                   120
    0                   120
    0                   120
    Not applicable      120
    Not applicable      120
    Empty               120

我想使用单个 select 语句将值 1 的计数选择到不同的变量中,并将 0 的计数选择到不同的变量中。有可能吗?

【问题讨论】:

    标签: select plsql where-clause oracle12c select-into


    【解决方案1】:

    可能吗?当然,这是一种选择。第 1 - 10 行中的样本数据;您可能使用的查询从第 11 行开始。

    SQL> with test (value, id) as
      2    (select 'n/a'  , 120 from dual union all
      3     select 'n/a'  , 120 from dual union all
      4     select 'empty', 120 from dual union all
      5     select '1'    , 120 from dual union all
      6     select '1'    , 120 from dual union all
      7     select '0'    , 120 from dual union all
      8     select '0'    , 120 from dual union all
      9     select '0'    , 120 from dual
     10    )
     11  select sum(case when value = '1' then 1 else 0 end) cnt_1,
     12         sum(case when value = '0' then 1 else 0 end) cnt_0
     13  from test;
    
         CNT_1      CNT_0
    ---------- ----------
             2          3
    
    SQL>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多