复杂的MySQL查询,我很接近,我可以闻它

时间:2012-05-22 04:05:37

标签: mysql

我用我的架构和一些测试数据创建了SQLFiddle(谢谢,Amadan!)。计算列在SQLFiddle中由于某种原因不起作用,但它们在我的MYSQL Workbench中表现良好。假设它们正常工作。

我的查询得到了这么多:

SELECT DISTINCT o.orderid,i.invoiceid,i.subinvoicenumber,stockingorderid AS oistockingorderid,

IF(stockingorderid > 0, 0, po.purchaseorderid) AS purchaseorderid,subponumber,po.paidvia,dropshipfee,i.taxstate,

COALESCE(
    (SELECT mfrname 
        FROM cs_products.tblstockingorders so 
        WHERE so.stockingorderid=oistockingorderid),
    po.mfrname)
AS source,

(i.shipping + i.surcharge + 
    (SELECT SUM(additionalshipping * quantity) AS additionalshipping 
        FROM cs_products.tblorderitems 
        WHERE invoiceid=i.invoiceid)) 
AS shipping,

CAST(IF(o.paymentmethod=2, CONCAT('Check: ', i.checknumber),o.paymentmethod) AS CHAR) AS invoicepaidvia,

COALESCE(
    (SELECT (SUM(cost * quantity) * IF(so.mfrdiscount > 0, 1 - so.mfrdiscount, 1)) AS cost 
        FROM cs_products.tblorderitems oi 
        JOIN cs_products.tblallocations a ON oi.orderitemid=a.orderitemid 
        JOIN cs_products.tblstockingorders so ON a.stockingorderid=so.stockingorderid
        WHERE a.stockingorderid=oistockingorderid),
    (SELECT (SUM(cost * quantity) * IF(po.mfrdiscount > 0, 1 - po.mfrdiscount, 1)) AS cost 
        FROM cs_products.tblorderitems 
        WHERE purchaseorderid=po.purchaseorderid)) 
    AS grosscost,

(SELECT SUM(price * quantity)
    FROM cs_products.tblorderitems 
    WHERE invoiceid=i.invoiceid) 
AS grossprice

FROM cs_products.tblorders o 
    JOIN cs_products.tblinvoices i ON o.orderid=i.orderid
    #ordertype of 0 means the order came from the website
    LEFT JOIN cs_products.tblpurchaseorders po ON o.orderid=po.orderid AND IF(o.ordertype<>0, subinvoicenumber=subponumber, subinvoicenumber=0 AND subponumber>=0)
    LEFT JOIN cs_products.tblallocations a ON a.orderid=o.orderid

这让我有95%的路要走。正如您所看到的,对于OrderId 1287,它显示了两个stockingorderid s的记录,而没有purchaseorderid的记录,即使采购订单已归因于1287.我期待的是看到缺失的行是这样的:

1287|276|0|NULL|194|0||0|0|'Quality Fabricators'|357.53|0|781.43|11917.70

再次,不要担心计算列,我只想要显示记录。我觉得我错过了最小的东西,但我不知道它是什么。

谢谢!

更新计算列无效的原因是因为使用了旧订单商品数据。我有机会的时候会更新它。

1 个答案:

答案 0 :(得分:2)

我建议您使用NULL(而不是0)分别代表PO和SO记录的stockingorderidpurchaseorderid

ALTER TABLE tblorderitems MODIFY purchaseorderid int(10) unsigned NULL;
UPDATE tblorderitems SET purchaseorderid = NULL WHERE purchaseorderid = 0;

然后,您可以使用UNION子查询加入查询,该子查询提供一组所有(订单 stockingorder )和(订单 purchaseorder )对:

  SELECT orderid, stockingorderid, NULL AS purchaseorderid FROM tblallocations
UNION ALL
  SELECT orderid, NULL AS stockingorderid, purchaseorderid FROM tblpurchaseorders

我认为您的查询可以简化为:

SELECT   orderid, invoiceid, subinvoicenumber, purchaseorderid, subponumber,
         tblpurchaseorders.paidvia, dropshipfee, tblinvoices.taxstate,
         invoicedate, datepaid, stockingorderid,

         COALESCE(tblstockingorders.mfrname, tblpurchaseorders.mfrname) AS source,

         shipping + surcharge + SUM(quantity * additionalshipping) AS shipping,

         CASE paymentmethod
           WHEN 2 THEN CONCAT('Check: ', checknumber)
           ELSE CAST(paymentmethod AS CHAR)
         END AS InvoicePaidVia,

         (1-COALESCE(tblstockingorders.mfrdiscount, tblpurchaseorders.mfrdiscount))
           * SUM(quantity * cost) AS grosscost,

         SUM(quantity * price) AS grossprice

FROM     tblorders
  JOIN   tblinvoices          USING (orderid)
  JOIN (
          SELECT orderid, orderitemid, stockingorderid, NULL AS purchaseorderid
          FROM   tblallocations
        UNION ALL
          SELECT orderid, NULL, NULL, purchaseorderid
          FROM   tblpurchaseorders
       ) AS t                 USING (orderid)
  LEFT JOIN tblpurchaseorders USING (orderid,purchaseorderid)
  LEFT JOIN tblstockingorders USING (stockingorderid)
  LEFT JOIN tblorderitems     USING (orderid,orderitemid,invoiceid,purchaseorderid)
WHERE    ordertype = 0 OR subinvoicenumber = subponumber
GROUP BY orderid, invoiceid, subinvoicenumber, purchaseorderid, subponumber,
         tblpurchaseorders.paidvia, dropshipfee, tblinvoices.taxstate, datepaid,
         invoicedate, stockingorderid, shipping, surcharge, paymentmethod,
         tblstockingorders.mfrname, tblpurchaseorders.mfrname, checknumber,
         tblstockingorders.mfrdiscount, tblpurchaseorders.mfrdiscount

sqlfiddle上查看,删除了对invoicedate列的引用(因为它不在您的示例模式中)。