I have a query which returns the custref having multiple tetranumbers(1 to n mapping)
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)
count 5144
My objective is to pull the matching address details along with the above result, but I guess I am missing something here.
Something like…
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;
Rather than scanning your table twice ,create a inline view and fetch the result using analytical function .Please test