I have customers with contract at various sites which must be charged.
There are two types of billing address for the sites: sites charged at the customer's address and sites charged to another address. The kind of site is handled by the field 'status': if the site is charged at the customer's address, the field is equal to 'C'. If it is charged to a different site's address, it is equal to 'A' and the field 'bill_site_id' is filled with the 'site_id' used for billing. I want to retrieve, in one query, the address where I have to send the bill...
Let's say a customer have two sites (the A refers to the B for billing); the request obviously shows the two sites, but I only want the one used for billing (the B). How to retrieve this one only?
Here is my query (anonymized, so please be gentle :-) )
SELECT CASE wp.status
WHEN 'A'
THEN wp2.name
ELSE c.NAME
END AS NAME,
CASE wp.status
WHEN 'A'
THEN wp2.adress
ELSE c.street
END AS street,
CASE wp.status
WHEN 'A'
THEN wp2.city
ELSE c.city
END AS city,
CASE wp.status
WHEN 'A'
THEN wp2.postcode
ELSE c.postcode
END AS postcode
FROM customer c, site wp, site wp2, customer_site cwp
WHERE c.idcompany = cwp.idcompany
AND cwp.site_id = wp.site_id
AND wp2.site_id(+) = wp.bill_site_id
AND c.idcompany = :someId
GROUP BY wp.status,
wp2.name,
wp2.adress,
wp2.city,
wp2.postcode