1

我有两个选择查询,我想从两者中选择值并有一个新查询。我试图加入他们,但我没有得到我想要的信息。

我的第一个查询是以下查询:

 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;

第二个是:

 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;

我想得到:

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

请问有什么帮助吗?

4

1 回答 1

1

您可以将这两个查询作为主查询的子查询:

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

我已经使用active_substance字段作为键来连接两个表,如果不合适,您可以选择更合适的列集,但是技术是两个将两个 dinstinct 查询作为主查询的子查询并以正确的方式加入它们...

于 2012-06-08T12:07:17.073 回答