0

我有一个查询,它返回具有多个四数的 custref(1 到 n 映射)

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

而不是扫描你的表两次,创建一个内联视图并使用分析函数获取结果。请测试

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 回答
0

通过修改查询(包括另外一个连接)它对我有用......但 Gaurav 答案是一种更好的方法

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 回答