Search code examples
selectplsqlwhere-clauseoracle12cselect-into

Selecting from Same Column into different variables


 Values                 Id's
    
    1                   120
    1                   120
    0                   120
    0                   120
    0                   120
    Not applicable      120
    Not applicable      120
    Empty               120

I wanted to select the count of values 1 into different variable and count of 0 into different variable with the single select statement. Is it possible ?


Solution

  • Possible? Sure, here's one option. Sample data in lines #1 - 10; query you might use begins at line #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>