mysql在单个查询中比较销售/购买

时间:2014-12-12 20:39:03

标签: mysql join subquery

我们有一个专有的销售系统,我们已经使用了一段时间。 最近我们添加了“购买”方面,以便我们可以比较匹配产品的平均购买/销售价格以及查看库存位置。

在MySQL中,我有2个表:tblPurchases和tblSalesDetail。

每个都有productID,数量,价格和货运字段(所有数字)。

我想查询每个产品ID:

  

[商品ID] [总和购买数量] [平均购买价格]   [平均购买运费] [总销售数量] [平均销售价格] [平均   销售运费]

我当前的sql语句看起来像这样

select 
    tblPurchases.productID,
    tblSalesDetail.productNo,
    sum(tblPurchases.quantity) as pQuantity,
    avg(tblPurchases.price) as pPrice,
    avg(tblPurchases.freight) as pFreight,
    sum(tblSalesDetail.quantity) as sQuantity,
    format(avg(tblSalesDetail.unitPrice),2) as sPrice,
    format(avg(tblSalesDetail.freight),2) as sFreight
from
    tblPurchases
left join tblSalesDetail
    on tblPurchases.productID=tblSalesDetail.productNo
group by tblPurchases.productID

但只有在双方都有匹配的产品ID时它才能正常工作。 我希望它还显示没有匹配购买产品ID的产品ID的销售。

单个查询可以实现吗?

=============

编辑

新查询如下所示

select 
  ps.productID, 
  ps.productNo,
  sum(ps.pQuantity) as pQuantity, 
  avg(ps.pPrice) as pPrice, 
  avg(ps.pFreight) as pFreight,
  sum(sQuantity) as sQuantity, 
  avg(sPrice) as sPrice, 
  avg(sFreight) as sFreight
from (
        (select productID, 
                null as productNo,
                quantity as pQuantity, 
                price as pPrice, 
                freight as pFreight,
                NULL as sQuantity, 
                NULL as sPrice, 
                NULL as sFreight
         from tblPurchases)  

      union all

      (select null as productID, 
              productNo, 
              NULL as pQuantity,
              NULL as pPrice,
              null as pFreight,
              quantity as sQuantity, 
              unitPrice as sPrice, 
              freight as sFreight
       from tblSalesDetail
      )
     ) as ps
group by ps.productID

但是我获得了2行,其中无与伦比的销售额在一行中被平均排在一起。

result set

如果我运行每个单独的查询,例如

select null as productID, 
                  productNo, 
                  NULL as pQuantity,
                  NULL as pPrice,
                  null as pFreight,
                  quantity as sQuantity, 
                  unitPrice as sPrice, 
                  freight as sFreight
           from tblSalesDetail

我得到了我期望得到的东西(约5000行)。

===============

解决

select 
*
from (
      (select   tblPurchases.productID as pProduct, 
                tblSalesDetail.productNo as sProduct,
                sum(tblPurchases.quantity) as pQuantity, 
                avg(tblPurchases.price) as pPrice, 
                avg(tblPurchases.freight) as pFreight,
                tblSalesDetail.quantity as sQuantity, 
                tblSalesDetail.unitPrice as sPrice, 
                tblSalesDetail.freight as sFreight
      from tblPurchases
      left join tblSalesDetail on tblPurchases.productID=tblSalesDetail.productNo
      group by tblPurchases.productID)   

      union all

      (select tblPurchases.productID as pProduct, 
              tblSalesDetail.productNo, 
              tblPurchases.quantity as pQuantity,
              tblPurchases.price as pPrice,
              tblPurchases.freight as pFreight,
              sum(tblSalesDetail.quantity) as sQuantity, 
              avg(tblSalesDetail.unitPrice) as sPrice, 
              avg(tblSalesDetail.freight) as sFreight
       from tblSalesDetail
       left join tblPurchases on tblPurchases.productID=tblSalesDetail.productNo
       group by tblSalesDetail.productNo)

     ) as ps

2 个答案:

答案 0 :(得分:1)

这是因为您正在执行LEFT OUTER JOIN TO tblSalesDetail,它会提供双方所有匹配的实例,以及没有销售明细的购买实例(不是相反)。两个选项:

向我提供所有销售详情,但仅限购买

SELECT Purchases.productID AS productID
    ,Sales.productNo AS productNo
    ,SUM(Purchases.quantity) AS pQuantity
    ,AVG(Purchases.price) AS pPrice
    ,AVG(Purchases.freight) AS pFreight
    ,SUM(Sales.quantity) AS sQuantity
    ,FORMAT(AVG(Sales.unitPrice),2) AS sPrice
    ,FORMAT(AVG(Sales.freight),2) AS sFreight
FROM tblSalesDetail AS Sales
LEFT OUTER JOIN tblPurchases AS Purchases
    ON Purchases.productID = Sales.productNo
GROUP BY Purchases.productID

只需切换表格,tblSalesDetail即成为驱动程序。

向我提供所有销售详情和所有购买,无论是否匹配

注意:已编辑,因为MySQL不支持FULL OUTER JOIN s。

SELECT productID
    ,productNo
    ,SUM(pQuantity) AS pQuantity
    ,AVG(pPrice) AS pPrice
    ,AVG(pFreight) AS pFreight
    ,SUM(sQuantity) AS sQuantity
    ,FORMAT(AVG(sPrice),2) AS sPrice
    ,FORMAT(AVG(sFreight),2) AS sFreight
FROM (
    SELECT Purchases.productID AS productID
        ,Sales.productNo AS productNo
        ,Purchases.quantity AS pQuantity
        ,Purchases.price AS pPrice
        ,Purchases.freight AS pFreight
        ,Sales.quantity AS sQuantity
        ,Sales.unitPrice AS sPrice
        ,Sales.freight AS sFreight
    FROM tblPurchases AS Purchases
    LEFT OUTER JOIN tblSalesDetail AS Sales
        ON Purchases.productID = Sales.productNo

    UNION

    SELECT Purchases.productID AS productID
        ,Sales.productNo AS productNo
        ,Purchases.quantity AS pQuantity
        ,Purchases.price AS pPrice
        ,Purchases.freight AS pFreight
        ,Sales.quantity AS sQuantity
        ,Sales.unitPrice AS sPrice
        ,Sales.freight AS sFreight
    FROM tblSalesDetail AS Sales
    LEFT OUTER JOIN tblPurchases AS Purchases
        ON Purchases.productID = Sales.productNo
) AS FullOuterJoin
GROUP BY productID
    ,productNo

FULL OUTER JOIN(通过UNION ALL完成,有两个LEFT OUTER JOIN S,然后组合在一起)会在匹配时将它们关联起来,但即使一方提供,也要提供两个表中的所有现有记录与另一个不匹配,反之亦然。

希望这有帮助。

答案 1 :(得分:1)

问题是你需要full outer join,但MySQL并不支持它。但是,您可以使用union all和聚合执行基本相同的操作。这基本上就是查询的样子:

select ps.productID, 
       sum(pquantity) as pQuantity, avg(pPrice) as pPrice, avg(pFreight) as pFreight,
       sum(squantity) as sQuantity, avg(sPrice) as sPrice, avg(sFreight) as sFreight
from ((select productid,
              quantity as pQuantity, price as pPrice, freight as pFreight,
              NULL as sQuantity, NULL as sPrice, NULL as sfreight
       from tblPurchases
      ) union all
      (select productno, NULL as pQuantity
              quantity as sQuantity, price as sPrice, freight as sFreight,
       from tblSalesDetail
      )
     ) ps
group by ps.productID;