Reputation: 4664
I have two selection queries and i want to select values from both and have a new query. I tried to join them but I don't get the information i want to.
The first query that I have is the one below:
SELECT InWarehouse.expiry_date,
drugs.active_substance,
drugs.strength,
drugs.strength_type,
drugs.dosage_form,
drugs.minimum_quantity,
NZ(SUM(InWarehouse.quantity),0) AS quant
FROM drugs
LEFT JOIN (SELECT drug_id,
expiry_date,
SUM(in_quant - out_quant) AS quantity
FROM warehouse GROUP BY drug_id, expiry_date)
AS InWarehouse ON
InWarehouse.drug_id = drugs.id
GROUP BY InWarehouse.expiry_date,
drugs.active_substance,
drugs.strength,
drugs.strength_type,
drugs.dosage_form,
drugs.minimum_quantity;
And the second one is:
SELECT InPharmacy.expiry_date,
drugs.active_substance,
drugs.strength,
drugs.strength_type,
drugs.dosage_form,
drugs.minimum_quantity,
NZ(SUM(InPharmacy.quantity), 0) as quant
FROM (drugs
LEFT JOIN (SELECT drug_id, expiry_date, SUM(in_quant - out_quant) as quantity
FROM pharmacy
GROUP BY drug_id, expiry_date) as InPharmacy
ON InPharmacy.drug_id = drugs.id)
GROUP BY InPharmacy.expiry_date,
drugs.active_substance,
drugs.strength,
drugs.strength_type,
drugs.dosage_form,
drugs.minimum_quantity;
I want to get the:
InWarehouse.expiry_date,
drugs.active_substance,
drugs.strength,
drugs.strength_type,
drugs.dosage_form,
drugs.minimum_quantity,
InWarehouse.quantity + InPharmacy.quantity,
InPharmacy.expiry_date
GROUP BY InWarehouse.expiry_date = InPharmacy.expiry_date
Any help please?
Upvotes: 1
Views: 340
Reputation: 72636
You can just put the two queries as subquery of a main query :
SELECT InWarehouse.expiry_date,
t1.active_substance,
drugs.strength,
drugs.strength_type,
drugs.dosage_form,
drugs.minimum_quantity,
InWarehouse.quantity + InPharmacy.quantity,
InPharmacy.expiry_date
FROM (SELECT InWarehouse.expiry_date,
drugs.active_substance,
drugs.strength,
drugs.strength_type,
drugs.dosage_form,
drugs.minimum_quantity,
NZ(SUM(InWarehouse.quantity),0) AS quant
FROM drugs
LEFT JOIN (SELECT drug_id,
expiry_date,
SUM(in_quant - out_quant) AS quantity
FROM warehouse GROUP BY drug_id, expiry_date)
AS InWarehouse ON
InWarehouse.drug_id = drugs.id
GROUP BY InWarehouse.expiry_date,
drugs.active_substance,
drugs.strength,
drugs.strength_type,
drugs.dosage_form,
drugs.minimum_quantity
) t1
JOIN ( SELECT InPharmacy.expiry_date,
drugs.active_substance,
drugs.strength,
drugs.strength_type,
drugs.dosage_form,
drugs.minimum_quantity,
NZ(SUM(InPharmacy.quantity), 0) as quant
FROM (drugs
LEFT JOIN (SELECT drug_id, expiry_date, SUM(in_quant - out_quant) as quantity
FROM pharmacy
GROUP BY drug_id, expiry_date) as InPharmacy
ON InPharmacy.drug_id = drugs.id)
GROUP BY InPharmacy.expiry_date,
drugs.active_substance,
drugs.strength,
drugs.strength_type,
drugs.dosage_form,
drugs.minimum_quantity ) t2
ON t1.active_substance = t2.active_substance
I have used the active_substance
field as a key to join the two table, if not appropriate you can choose the more appropriate set of columns , however the technique is two put the two dinstinct query as subquery of a main one and JOIN them in the proper way ...
Upvotes: 1