ACCESS 2010:如何从两个查询中选择值?

时间:2012-06-08 12:02:00

标签: sql ms-access-2010

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

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

 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

请帮忙吗?

1 个答案:

答案 0 :(得分: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查询作为一个主的子查询一个并以正确的方式加入他们......