Oracle SQL uitdaging

Status
Niet open voor verdere reacties.

ottoioioi

Gebruiker
Lid geworden
1 nov 2006
Berichten
21
Beste Helpmij,

Ik heb een ingewikkelde uitdaging in oracle SQL!
Ik heb 2 querys die er als volgt uit zien:
__________________________________________________________________________
Query 1
Select tOrg.name AccountCode
,tOrg.alias_name AccountName
,tAddr.addr Address
,tAddr.zipcode || ' ' || tAddr.x_postalcode_detail Zipcode
,tAddr.city City
From :AppOwner.s_org_ext tOrg
,:AppOwner.s_con_addr tConad
,:AppOwner.s_addr_per tAddr
Where tAddr.row_id (+) = tConad.addr_per_id
and tOrg.row_id (+) = tConad.accnt_id
and tConad.addr_type_cd = 'Post'
and tOrg.cust_stat_cd = 'Active'
__________________________________________________________________________
Query 2
Select tOrg.name AccountCode
,tOrg.alias_name AccountName
,tAddr.addr Address
,tAddr.zipcode || ' ' || tAddr.x_postalcode_detail Zipcode
,tAddr.city City
From :AppOwner.s_org_ext tOrg
,:AppOwner.s_con_addr tConad
,:AppOwner.s_addr_per tAddr
Where tAddr.row_id (+) = tConad.addr_per_id
and tOrg.row_id (+) = tConad.accnt_id
and tConad.addr_type_cd = 'Visit'
and tOrg.cust_stat_cd = 'Active'
__________________________________________________________________________
Query 1 geeft alle postadressen in de database weer. Query 2 geeft alle bezoekadressen weer. Ik wil hier één query van maken. De kans bestaat dat iemand zowel een postadres als een bezoekadres heeft. In dat geval moet de query alleen het postadres weergeven.

Dus: 3 mogelijkheden:
alleen een postadres -->> postadres weergeven
alleen een bezoekadres -->> bezoekadres weergeven
postadres en bezoekadres -->> postadres weergeven

Heeft iemand enig idee hoe ik dit het beste zou moeten aanpakken??

Groet'n van Otto
 
Voor zover ik zie is alleen de een na laatste regel anders,

je zou ze kunnen combineren:

Select tOrg.name AccountCode
,tOrg.alias_name AccountName
,tAddr.addr Address
,tAddr.zipcode || ' ' || tAddr.x_postalcode_detail Zipcode
,tAddr.city City
From :AppOwner.s_org_ext tOrg
,:AppOwner.s_con_addr tConad
,:AppOwner.s_addr_per tAddr
Where (tAddr.row_id (+) = tConad.addr_per_id
and tOrg.row_id (+) = tConad.accnt_id
and tConad.addr_type_cd = 'Post'
and tOrg.cust_stat_cd = 'Active') or
(tAddr.row_id (+) = tConad.addr_per_id
and tOrg.row_id (+) = tConad.accnt_id
and tConad.addr_type_cd = 'Visit'
and tOrg.cust_stat_cd = 'Active')

dan alleen nog ervoor zorgen dat je geen dubbele personen hebt. (wellicht mogelijk met een query in query... maar das lastig, moet ik dan eens goed voor gaan zitten)

wellicht dat een reactie uit dit bericht je kan helpen:
http://www.helpmij.nl/forum/showthread.php?t=395683
 
Laatst bewerkt:
Wat je ook nog kunt doen is een UNION query. Eigenlijk heel eenvoudig, je maakt 2 query's en je zet er UNION tussen. Enige dat je dan aan je bezoekadres query moet toevoegen is een criterium dat het account geen postadres mag hebben.

Dus bijv:
Code:
Select tOrg.name AccountCode
,tOrg.alias_name AccountName
,tAddr.addr Address
,tAddr.zipcode || ' ' || tAddr.x_postalcode_detail Zipcode
,tAddr.city City
From :AppOwner.s_org_ext tOrg
,:AppOwner.s_con_addr tConad
,:AppOwner.s_addr_per tAddr
Where tAddr.row_id (+) = tConad.addr_per_id
and tOrg.row_id (+) = tConad.accnt_id 
and tConad.addr_type_cd = 'Post' 
and tOrg.cust_stat_cd = 'Active'
UNION
Select tOrg.name AccountCode
,tOrg.alias_name AccountName
,tAddr.addr Address
,tAddr.zipcode || ' ' || tAddr.x_postalcode_detail Zipcode
,tAddr.city City
From :AppOwner.s_org_ext tOrg
,:AppOwner.s_con_addr tConad
,:AppOwner.s_addr_per tAddr
Where tAddr.row_id (+) = tConad.addr_per_id
and tOrg.row_id (+) = tConad.accnt_id 
and tConad.addr_type_cd = 'Visit'
and tOrg.cust_stat_cd = 'Active'
AND tOrg.name NOT IN (
Select tOrg.name
From :AppOwner.s_org_ext tOrg
,:AppOwner.s_con_addr tConad
,:AppOwner.s_addr_per tAddr
Where tAddr.row_id (+) = tConad.addr_per_id
and tOrg.row_id (+) = tConad.accnt_id 
and tConad.addr_type_cd = 'Post' 
and tOrg.cust_stat_cd = 'Active')

De subquery kan vast efficiënter, maar dan moet ik beter naar het datamodel kijken...


Grtz,

Mark
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan