oracle-databasesubqueryaggregatescalarscalar-subquery

scalar subquery has an aggregate operation


My oracle version is 10.2. It's very strange when a scalar subquery has an aggregate operation. my table named t_test looked like this;

t_id    t_name
 1       1
 2       1
 3       2
 4       2
 5       3
 6       3      

query string looked like this;

select t1.t_id,
       (select count(t_name)
         from (select t2.t_name 
                 from t_test t2 
                 where t2.t_id=t1.t_id 
                 group by t2.t_name)) a
from t_test t1

this query's result is,

t_id  a
 1    3
 2    3
 3    3
 4    3
 5    3
 6    3

which is very weird, take t1.t_id=1 for example,

select count(t_name)
 from (select t2.t_name 
         from t_test t2 
         where t2.t_id=1
         group by t2.t_name)

the result is 1, somehow,the 'where' operator doesn't work,the result is exactly the same as I put my query like this:

select t1.t_id,
       (select count(t_name)
         from (select t2.t_name 
                 from t_test t2 
                 group by t2.t_name)) a
from t_test t1

why?


Solution

  • Can you post a cut-and-paste from SQL*Plus showing exactly what query you're running? The query you posted does not appear to be valid-- the alias t1 is not going to be valid in the subquery where you're referencing it. That makes me suspect that you're simplifying the problem to post here but you've accidentally left something important out.

    SQL> ed
    Wrote file afiedt.buf
    
      1  with x as (
      2    select 1 id, 1 name from dual union all
      3    select 2,1 from dual union all
      4    select 3,2 from dual union all
      5    select 4,2 from dual union all
      6    select 5,3 from dual union all
      7    select 6,3 from dual
      8  )
      9  select t1.id
     10        ,(select count(b.name)
     11            from (select t2.name
     12                    from x t2
     13                   where t2.id = t1.id
     14                   group by t2.name) b) a
     15*   from x t1
    SQL> /
                     where t2.id = t1.id
                                   *
    ERROR at line 13:
    ORA-00904: "T1"."ID": invalid identifier
    

    Presumably, it would be much more natural to write the query like this (assuming you really want to use a scalar subquery) where t1 is going to be a valid alias in the scalar subquery.

    SQL> ed
    Wrote file afiedt.buf
    
      1  with x as (
      2    select 1 id, 1 name from dual union all
      3    select 2,1 from dual union all
      4    select 3,2 from dual union all
      5    select 4,2 from dual union all
      6    select 5,3 from dual union all
      7    select 6,3 from dual
      8  )
      9  select t1.id
     10        ,(select count(t2.name)
     11            from x t2
     12           where t2.id = t1.id) cnt
     13*   from x t1
    SQL> /
    
            ID        CNT
    ---------- ----------
             1          1
             2          1
             3          1
             4          1
             5          1
             6          1
    
    6 rows selected.