2

この特定のクエリについて誰か助けてもらえますか? これを実行しようとすると、ORA-00979 が発生します。

select   t0.title, count (1) as count0, (select   count (1)
      from contract c1, se se1
      where c1.c_id = se1.c_id
      and se1.svc_id = 3
      and se1.deleted = 0
      and c1.deleted = 0
      and c1.c_date between to_date ('07.10.2000', 'dd.mm.yyyy') 
                        and to_date ('22.11.2010', 'dd.mm.yyyy')
      and c1.company = 0
      and c1.tdata.tariff = c0.tdata.tariff
    ) as count1
  from contract c0, se se0, tariff t0
  where c0.c_id = se0.c_id
  and se0.svc_id = 3
  and se0.deleted = 0
  and c0.deleted = 0
  and c0.c_date between to_date ('21.11.2000', 'dd.mm.yyyy') 
                and to_date ('06.01.2011', 'dd.mm.yyyy')
  and c0.company = 0
  and t0.tariff_id = c0.tdata.tariff
  group by t0.title
4

4 に答える 4

3

問題は、パーツを使用したサブクエリselect count(1)です。カウントがあるからといって、実際には集計にはなりません。これは依然としてすべての行に適用されるサブクエリであり、ご覧のとおりc0.tdata.tariff、グループの一部ではない値を使用しています。

于 2011-01-05T15:54:34.573 に答える
1

これは同じクエリの 2 つのインスタンスが異なる日付にあるように見えることを考慮すると (ここで間違っている可能性があります... 長い 1 日でした)、単純化して次のように書き直すことができます。

select
  t0.title, 
  count (case when c0.c_date between to_date ('21.11.2000', 'dd.mm.yyyy') 
                and to_date ('06.01.2011', 'dd.mm.yyyy') then 1 end) as count0,
  count (case when c0.c_date between to_date ('07.10.2000', 'dd.mm.yyyy') 
                and to_date ('22.11.2011', 'dd.mm.yyyy') then 1 end) as count1
from 
  contract c0, 
  se se0, 
  tariff t0
where 
  c0.c_id = se0.c_id
  and se0.svc_id = 3
  and se0.deleted = 0
  and c0.deleted = 0
  and (c0.c_date between to_date ('21.11.2000', 'dd.mm.yyyy') 
                and to_date ('06.01.2011', 'dd.mm.yyyy')
    or c0.c_date between to_date ('07.10.2000', 'dd.mm.yyyy') 
                        and to_date ('22.11.2010', 'dd.mm.yyyy'))
  and c0.company = 0
  and t0.tariff_id = c0.tdata.tariff
group by t0.title
于 2011-01-05T19:54:36.383 に答える
1

そのスカラー サブクエリが問題を引き起こしているようです。これはグループ関数でも、GROUP BY リストにも含まれていません。

おそらく、次のような方法で回避できます。

select   t0.title, count (1) as count0, SUM(select   count (1) ...) AS count1
 ...
于 2011-01-05T15:55:00.503 に答える
0

group by には、選択リストの非集計列をすべて含める必要があります。この場合、count1サブクエリによって返されたが group by にありません。

于 2011-01-05T15:54:12.207 に答える