我有一个查询,它返回有多个tetranumbers(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;
答案 0 :(得分: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;
答案 1 :(得分: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