0

我有这个要执行的查询。

 SELECT warehouse.expiry_date, pharmacy.expiry_date,
   drugs.active_substance, 
   drugs.strength, 
   drugs.strength_type, 
   drugs.dosage_form,  
   drugs.minimum_quantity,
   SUM(QTY-QTYQ) as QTYU
 FROM
 (SELECT warehouse.expiry_date, 
   drugs.active_substance, 
   drugs.strength, 
   drugs.strength_type, 
   drugs.dosage_form,  
   drugs.minimum_quantity,
   SUM(warehouse.in_quant)-  SUM(warehouse.out_quant) as QTY
 FROM drugs INNER JOIN warehouse ON drugs.ID = warehouse.drug_id  
 GROUP BY warehouse.expiry_date,
   drugs.active_substance, 
   drugs.strength, 
   drugs.strength_type, 
   drugs.dosage_form, 
   drugs.minimum_quantity

 UNION ALL

 SELECT pharmacy.expiry_date, 
   drugs.active_substance, 
   drugs.strength, 
   drugs.strength_type, 
   drugs.dosage_form,  
   drugs.minimum_quantity,
   SUM(pharmacy.out_quant)-  SUM(pharmacy.out_quant) as QTYQ
 FROM drugs INNER JOIN pharmacy ON drugs.ID = pharmacy.drug_id  
 GROUP BY pharmacy.expiry_date,
   drugs.active_substance, 
   drugs.strength, 
   drugs.strength_type, 
   drugs.dosage_form, 
   drugs.minimum_quantity)
 GROUP BY 
 warehouse.expiry_date,
   drugs.active_substance, 
   drugs.strength, 
   drugs.strength_type, 
   drugs.dosage_form, 
   drugs.minimum_quantity,
  pharmacy.expiry_date;

当我尝试执行它时,程序想要输入 pharmacy.expiry_date 和 QTYQ 的参数值。但是这些值在我的嵌套选择中。

请问有什么帮助吗?

4

2 回答 2

0

由于您似乎没有任何主机变量,我的心灵感应告诉我这与您在语句中的语法错误有关。具体来说,QTYQ似乎不存在(该名称的列QTY被 前面部分中的列所掩盖UNION ALL),并且在给定您选择列的顺序的情况下pharmacy.expiry_date位于 中的错误位置。GROUP BY最后的减法语句 ( SUM(QTY - QTYQ)) 试图减去两个不同的,但你没有给它任何相关性(你真的想要列)。还有一个错字会给你不正确的结果(SUM(pharmacy.out_quant) - SUM(pharmacy.out_quant)应该返回 0,可能不是你想要的。优化器甚至可能不会打扰调用函数,如果它足够聪明的话)。

这是您的查询,改写成更好的形式,我认为这对您有更好的帮助。请注意,这未经测试,有责任验证它是否返回满足您需求的正确结果。您还需要一个日历文件,无论如何您都应该拥有它。

SELECT Calendar.date, 
       Drugs.active_substance,
       Drugs.strength,
       Drugs.strength_type,
       Drugs.dosage_form,
       Drugs.minimum_quantity,
       COALESCE(SUM(InWarehouse.quantity), 0) 
          - COALESCE(SUM(InPharmacy.quantity), 0) as quant
FROM Drugs 
CROSS JOIN Calendar
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
AND InWarehouse.expiry_date = Calendar.date
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
AND InPharmacy.expiry_date = Calendar.date
GROUP BY Calendar.date, 
         Drugs.active_substance,
         Drugs.strength,
         Drugs.strength_type,
         Drugs.dosage_form,
         Drugs.minimum_quantity
于 2012-06-06T16:49:08.343 回答
0
 SELECT InWarehouse.expiry_date, 
        drugs.active_substance,
        drugs.strength,
        drugs.strength_type,
        drugs.dosage_form,
        drugs.minimum_quantity,
        COALESCE(SUM(InWarehouse.quantity), 0) 
           - COALESCE(SUM(InPharmacy.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
 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
 AND InPharmacy.expiry_date = InWarehouse.expiry_date
 GROUP BY InWarehouse.expiry_date, 
          drugs.active_substance,
          drugs.strength,
          drugs.strength_type,
          drugs.dosage_form,
          drugs.minimum_quantity
于 2012-06-18T12:54:13.973 回答