如何在一个请求中计算总金额的金额和百分比?

时间:2017-01-26 07:48:55

标签: mysql sql

我有两张桌子:

托运人 ShipperID ShipperName 列和

使用 订单ID ShipperID

订单

我需要选择高于平均水平(发件人“发件人”)的出货单,他们发货的订单数量(到“发送的商品”列)和整体发货的百分比金额(对于“配额”栏,值必须带有'%'符号)。

我有两个选择:

 select Shippers.ShipperName, Orders.OrderID, Orders.OrderID
 from (
       select 
              Shippers.ShipperName as "Sender",
              count(Orders.OrderID) as "Items Sent",
              (count(Orders.OrderID)*100)/sum(Orders.OrderID) as "Quota"
       from Shippers
       left join Orders
       on Orders.ShipperID = Shippers.ShipperID
       group by Shippers.ShipperName
      ) Shippers 
       group by Shippers.ShipperName
  having Orders.OrderID > avg(Orders.OrderID);

结果第5行的语法错误

第二个选项:

select Shippers.ShipperName as "Sender",
       count(Orders.OrderID) as "Items Sent",
       100*count(Orders.OrderID)/(select count(Orders.OrderID) 
                          from Shippers left join Orders
                          on Orders.ShipperID=Shippers.ShipperID) as "Quota"
from Shippers
left join Orders
on Orders.ShipperID = Shippers.ShipperID
group by Shippers.ShipperName
having count(Orders.OrderID)>(select count(Orders.OrderID)
            from Shippers left join Orders  
               on Orders.ShipperID = Shippers.ShipperID) /                 
               (select count(distinct ShipperID) from Shippers);

结果是第13行1064 语法错误;

数据“发货人”:数据“订单”:

ShippersID|ShipperName                        OrderID|ShipperID             
----------|----------------                 ---------|----------
       1  |Speedy Express                     10248  |    3
       2  |United Package                     10249  |    1
       3  |Federal Shipping                   10250  |    2
----------|----------------                   10251  |    1
                                              10252  |    2
                                              10253  |    2
                                              10254  |    2
                                              10255  |    3
                                              10256  |    2
                                            ---------|----------

2 个答案:

答案 0 :(得分:1)

应该使用过滤avg

 select t1.name, t1.count_order, t1.perc_order
 from (
  select 
      Shippers.ShipperName as name
    , count(Orders.OrderID) as  count_order
    , (count(Orders.OrderID)*100)/sum(Orders.OrderID) as  perc_order
  from Shippers
  left join Orders
  on Orders.ShipperID = Shippers.ShipperID
  Group by Shippers.ShipperName
  ) t1 
  group by t1.name 
  HAVING count_order > avg( count_order)

答案 1 :(得分:1)

我会按如下方式对您的查询进行短语:

SELECT s.ShipperName,
       COUNT(*) AS num_orders,
       100 * COUNT(*) / (SELECT COUNT(*) FROM Shippers t1 LEFT JOIN Orders t2
                         ON t2.ShipperID = t1.ShipperID) AS num_orders_percent
FROM Shippers s
LEFT JOIN Orders o
    ON o.ShipperID = s.ShipperID
GROUP BY s.ShipperName
HAVING COUNT(*) > (SELECT COUNT(*) FROM Shippers t1 LEFT JOIN Orders t2 -- total # orders
                   ON t2.ShipperID = t1.ShipperID) /                    -- divided by
                  (SELECT COUNT(DISTINCT ShipperID) FROM Shippers)      -- total # shippers

我的查询中的所有内容对您来说都应该很熟悉,除了两个子查询,我将在此解释:

SELECT COUNT(*) FROM Shippers t1 LEFT Orders t2 ON t2.ShipperID = t1.ShipperID

这会计算所有货运单的订单总数。它用于计算给定托运人持有的订单百分比:

% orders = # orders / total # orders

HAVING子句将结果集限制为仅超过平均订单数的发货人。这里需要另一个子查询:

SELECT COUNT(DISTINCT ShipperID) FROM Shippers

这会计算不同出货单的总数。以下等式可用于查找每个托运人的平均订单数量:

average # orders = total # orders / total # shippers

以下是使用您提供的示例数据的演示:

SQLFiddle