MySQL JOIN help
I've been struggling with this query for a few days now. I'm using PHP/MySQL.
It's for a system where customers can send freight for either collection or delivery by the company for which I am developing for. The query I am trying to write will gather the collections and deliveries carried out for a customer, and send them a summary of what they have spent that day.
I have two result sets below which are what I need to "join together" in order to send the report.
(Don't worry about the specifics, it's the join that is the issue :) )
Deliveries Result Set
This result is a list (grouped by day) of delivery totals that the company has delivered on behalf of the customers listed.
summary_day, num_cons, num_spaces, customer_code
2011-07-20 1 3 COB001P
2011-07-20 1 3 FAI001P
2011-07-20 2 2 FRE001P
2011-07-20 2 2 MIN001P
2011-07-20 17 29 NOR001P
2011-07-20 50 79 PAL001P
2011-07-20 1 1 PAR001P
2011-07-20 1 1 POT002P
2011-07-20 6 7 RHY001P
2011-07-20 9 13 TDG001P
2011-07-20 18 23 UPN001P
Collections Result Set
Similar to above, but in reverse, this result is a list of totals that the company has collected on behalf of each customer.
2011-07-20 9 15 ARR001P
2011-07-20 1 1 BAC002P
2011-07-20 1 1 BLA001P
2011-07-20 4 6 CAR003P
2011-07-20 2 2 DIS001P
2011-07-20 2 2 DOV001P
2011-07-20 1 1 DRY001P
2011-07-20 1 1 ECC001P
2011-07-20 3 5 FAI001P
2011-07-20 2 2 INV001P
2011-07-20 2 2 MIN001P
2011-07-20 3 3 PAL001P
2011-07-20 1 1 QUA002P
2011-07-20 1 开发者_Python百科1 TEC002P
2011-07-20 1 1 THE006P
2011-07-20 7 7 WIL005P
The Problem
I'm struggling with the JOIN to use to merge these two result sets.
Ideally the end result set should be pretty standard, with the columns of:
summary_day, customer_code, num_deliveries, num_collections
The number fields will be the num_spaces column from each result set. If a customer has both collection and delivery records, then show both the numbers. If they have one not the other, I have used COALESCE to expect one column to be NULL, and to set it to 0 accordingly.
I have tried to use a RIGHT OUTER JOIN, ON the customer_code field, in the hope that this would produce the results expected, but the only results I get are:
2011-07-20, ARR001P, 0, 15
2011-07-20, BAC002P, 0, 1
2011-07-20, BLA001P, 0, 1
2011-07-20, CAR003P, 0, 6
2011-07-20, DIS001P, 0, 2
2011-07-20, DOV001P, 0, 2
2011-07-20, DRY001P, 0, 1
2011-07-20, FAI001P, 3, 5
2011-07-20, INV001P, 0, 2
2011-07-20, MIN001P, 2, 2
2011-07-20, PAL001P, 79, 3
2011-07-20, QUA002P, 0, 1
2011-07-20, TEC002P, 0, 1
2011-07-20, THE006P, 0, 1
2011-07-20, WIL005P, 0, 7
As you can see, the result set only seems to be returning the customers who have collections, which is OK but I also need to see the customers who have deliveries and no collections.
For example, an example would be the customer NOR001P, who is in the deliveries result set but not the collections resultset...
Is a FULL OUTER JOIN required in this scenario? If so, how could I work round this considering MySQL does not support FULL OUTER JOINs?
Thanks for taking the time to read.
FULL SOLUTION
Thanks to the answer from CResults, the full solution was as follows... as you can see the delivery and collection result sets were actually made up from sub queries, so this was a bit of a head ache!
set @summary_day = '2011-07-20';
select summary_day, customer_code, sum(num_deliveries) as pallet_deliveries, sum(num_collections) as pallet_collections
from
(
select d.summary_day, d.customer_code, d.num_spaces as num_deliveries, 0 as num_collections from
(select
@summary_day as summary_day, /* change variable */
count(*) as num_cons,
sum( coalesce(micro_qty,0) + coalesce(quarter_qty,0) + coalesce(half_qty,0) + coalesce(full_qty,0) + coalesce(ceil(vlu_qty),0) ) as num_spaces,
pc.customer_code
from pallet_routes pr
inner join pallet_consignments pc on pc.route_id = pr.route_id
where pr.date = @summary_day /* today */
and pc.type = 'D'
group by customer_code
) as d
union all
select c.summary_day, c.customer_code, 0 as num_deliveries, num_spaces as num_collections from
(select
@summary_day as summary_day, /* change variable */
count(*) as num_cons,
sum(coalesce(micro_qty,0) + coalesce(quarter_qty,0) + coalesce(half_qty,0) + coalesce(full_qty,0) + coalesce(ceil(vlu_qty),0)) as num_spaces,
pc.customer_code
from pallet_routes pr
inner join pallet_consignments pc on pc.route_id = pr.route_id
where pr.date = DATE_SUB(@summary_day, INTERVAL 1 DAY) /* Yesterday */
and pc.type = 'C'
group by customer_code
) as c
) as pallet_summaries
group by summary_day, customer_code
Totally untested, but give this a try
Select
Date, CustCode, Sum(Num_Cons) as Num_Cons, Sum(Num_Cols) as Num_Cols
from
( Select Date, CustCode, Num_Cons, 0 as Num_Cols From Consignments
UNION ALL
Select Date, CustCode, 0 as Num_Cons, Num_Cols From Collections
) parcels
group by Date, CustCode
SELECT D.summary_day
, D.customer_code
, D.num_deliveries
, COALESCE(C.num_collections, 0) AS num_collections
FROM Deliveries AS D
LEFT JOIN Collections AS C
ON D.customer_code = C.customer_code
AND D.summary_day = C.summary_day
UNION ALL
SELECT C.summary_day
, C.customer_code
, 0
, C.num_collections
FROM Deliveries AS D
RIGHT JOIN Collections AS C
ON D.customer_code = C.customer_code
AND D.summary_day = C.summary_day
WHERE D.summary_day IS NULL
ORDER BY summary_day
, customer_code
精彩评论