1

Oracleでこれを行う方法はありますか?

select a.field1 || '_' || 
       b.field2 || '_' || 
       sum(c.field3)
  from table1 a, 
       table2 b, 
       table3 c
 where a.field1 || '_' || 
       b.field2 || '_' || 
       sum(c.field3) not in (select d.field1 || '_' || 
                                    e.field2 || '_' || 
                                    sum(f.field3)  
                               from table4 d, 
                                    table5 e, 
                                    table6 f
                              where conditional_info_to_join_the_tables 
                              group by d.field1, e.field2)
   and conditional_info_to_join_the_tables  
 group by a.field1, b.field2

私が得るエラーは、where句で合計を使用できないことです

使ってみました

select a.field1 || '_' || 
       b.field2 || '_' || 
       sum(c.field3), 
       sum(c.field2) foo
  from table1 a, 
       table2 b, 
       table3 c
 where a.field1 || '_' || 
       b.field2 || '_' || 
       foo not in (select d.field1 || '_' || 
                          e.field2 || '_' || 
                          sum(f.field3)  
                     from table4 d, 
                          table5 e, 
                          table6 f
                    where conditional_info_to_join_the_tables 
                    group by d.field1, e.field2)
   and conditional_info_to_join_the_tables 
 group by a.field1, b.field2

しかし、そのfooは識別された変数ではありませんでした。

4

2 に答える 2

3

集約はフィルタリング(句)の後に発生するため、 sumandエイリアス( )を指定するだけでfooは役に立ちません。where

このhaving句は、集計後に適用されるフィルターのようなものです。

select a.field1 || '_' || b.field2 || '_' || sum(c.field3)
from table1 a, table2 b, table3 c
where conditional_info_to_join_the_tables
group by a.field1, b.field2
having a.field1 || '_' || b.field2 || '_' || sum(c.field3)
           not in ( select d.field1 || '_' || e.field2 || '_' || sum(f.field3) 
                    from table4 d, table5 e, table6 f
                    where conditional_info_to_join_the_tables 
                    group by d.field1, e.field2 )

エイリアシングではなく、アグリゲートを完全に書き出す必要があります。この背後にある理由は少しわかりにくいため、dba.seのこの回答で説明されています:https ://dba.stackexchange.com/a/21982/ 1396

もちろん、サブクエリでも同じことができます。

select foo
from( select a.field1 || '_' || b.field2 || '_' || sum(c.field3) as foo
      from table1 a, table2 b, table3 c
      where conditional_info_to_join_the_tables
      group by a.field1, b.field2 )
where foo not in ( select d.field1 || '_' || e.field2 || '_' || sum(f.field3) 
                   from table4 d, table5 e, table6 f
                   where conditional_info_to_join_the_tables 
                   group by d.field1, e.field2 )
于 2012-12-03T17:55:35.853 に答える
0

これは、すべての連結なしで実行できます。

select abc.field1 || '_' || abc.field2 || '_' || sumf, 
       sum(c.field2) foo
from (select a.field1, b.field2, sum(c.field3) as sumf
      from a, b, c
      where conditional_info_to_join_the_tables
      group by a.field1, b.field2
     ) abc left outer join
     (select d.field1, e.field2, sum(f.field3) as sumf
      from d, e, f
      where conditional_info_to_join_the_tables
      group by d.field1, 3.field2
     ) def
     on abc.field1 = def.field1 and
        abc.field2 = def.field2 and
        abc.sumf = def.sumf
where def.field1 = NULL

join一般に、より新しい構文を使用する必要があります。これは、left outer joinが非常に適切な場合です。

于 2012-12-03T18:58:43.340 に答える