0

複数のテトラナンバー (1 から n へのマッピング) を持つ custref を返すクエリがあります。

select *
  from cdsheader
 where custref in(select custref
                    from(select *
                           from cdsheader h,custandaddr c
                          where h.custref = c.cwdocid
                            and c.addresstype = 'C')
                   group by custref
                  having count(distinct( tetranumber )) > 1) 

5144 を数える

私の目的は、上記の結果と一致する住所の詳細を取得することですが、ここに何かが欠けていると思います。

何かのようなもの...

select a.cworderid,a.cwcreated,a.organisationtype,a.custref,a.tetranumber,
       b.buildingname,b.streetname,b.posttown,b.postcode,b.country
  from cdsheader a,custandaddr b
 where custref in (select custref
                     from cdsheader h,custandaddr c
                    where h.custref = c.cwdocid
                      and c.addresstype = 'C')
                    group by custref
                   having count(distinct( tetranumber )) > 1)
 order by a.custref,a.tetranumber,a.cworderid; 
4

2 に答える 2

0

クエリを変更する(もう1つの結合を含む)ことでうまくいきました...しかし、ガウラフの答えはより良い方法です

select 
     a.cworderid,a.cwcreated, a.organisationtype, a.custref, a.tetranumber, b.buildingname,     b.streetname, b.posttown, b.postcode, b.country
    from custandaddr b, cdsheader a where b.cwdocid = a.custref and custref in(
    select custref from(
    select * from cdsheader h, custandaddr c where h.custref=c.cwdocid and c.addresstype = 'C')
    group by custref having count(distinct(tetranumber))>1)
    order by a.custref, a.tetranumber, a.cworderid
于 2012-11-26T16:46:45.773 に答える
0

テーブルを 2 回スキャンするのではなく、インライン ビューを作成し、分析関数を使用して結果を取得します。テストしてください

SELECT     inner.cworderid,
           inner.cwcreated,
           inner.organisationtype,
           inner.custref,
           inner.tetranumber,
           inner.buildingname,
           inner.streetname,
           inner.posttown,
           inner.postcode,
           inner.country
FROM(           
  SELECT   h.cworderid,
           h.cwcreated,
           h.organisationtype,
           h.custref,
           h.tetranumber,
           c.buildingname,
           c.streetname,
           c.posttown,
           c.postcode,
           c.country,
           COUNT(DISTINCT tetranumber) OVER(PARTITION BY h.custref) cnt
    FROM   cdsheader h, custandaddr c
    WHERE  h.custref = c.cwdocid 
       AND c.addresstype = 'C'
      )inner
  WHERE inner.cnt>1 
 ORDER BY inner.custref, inner.tetranumber, inner.cworderid;  
于 2012-11-26T16:26:52.503 に答える