如何在1 mysql中求和、乘和除

bf1o4zei  于 2021-06-17  发布在  Mysql
关注(0)|答案(3)|浏览(360)

这已经困扰我一段时间了
表:库存

|   customerID   | Item  |  Quantity  |  Price  |
|     cus1       |  A    |     3      |    4    |
|     cus1       |  B    |     2      |    3    |
|     cus1       |  C    |     3      |    3    |
|     cus2       |  A    |     3      |    3.50 |
|     cus2       |  C    |     3      |    2    |
|     cus3       |  D    |     6      |    4    |

如何让我的mysql查询生成如下视图
视图:库存

| cusID  | Item |Quan |Price |Tot_Qua | Tot_pri | sumtot_pri/tot_qua|
|  cus1  |  A   |  3  | 4    |   8    | 12      | 3.375             | 
|  cus1  |  B   |  2  | 3    |   8    | 6       | 3.375             | 
|  cus1  |  C   |  3  | 3    |   8    | 9       | 3.375             |
|  cus2  |  A   |  3  | 3.50 |   6    | 10.05   | 2.675             |
|  cus2  |  C   |  3  | 2    |   6    | 6       | 2.675             | 
|  cus3  |  D   |  6  | 4    |   6    | 24      | 4.00              |

cus1示例。cus1有3个项目,即a、b和c。所以我想要一些公式

Tot_Qua = 3+2+3 = 8
Tot_pri = price x quan

tot\u pri必须为cus1记录求和,

sumtot_pri = (quan x price) + (quan x price) + (quan x price)
sumtot_pri = (12) + (6) + (9)

因为cus1有3项,最后一项

sumtot_pri / Tot_qua = 27 / 8 = 3.375

我想需要分组,因为我想看看他们的物品。我不在乎是否有专栏 tot_qua 和列 sumtot_pri/tot_qua 将为每个CU复制相同的数据。

ne5o7dgx

ne5o7dgx1#

此查询将提供所需的结果。它在子查询中执行所有需要的聚合 JOIN 将其发送到主表以提供所需的输出:

SELECT s.customerID
     , Item
     , Quantity
     , Price
     , Tot_Qua
     , Quantity * Price AS Tot_pri
     , Avg_Pri
FROM stock s
JOIN (SELECT customerID
           , SUM(Quantity) AS Tot_Qua
           , SUM(Quantity * Price) / SUM(Quantity) AS Avg_Pri
      FROM stock
      GROUP BY customerID) s1
    ON s1.customerID = s.customerID

输出:

customerID  Item    Quantity    Price   Tot_Qua     Tot_pri     Avg_Pri
cus1        A       3           4       8           12          3.375
cus1        B       2           3       8           6           3.375
cus1        C       3           3       8           9           3.375
cus2        A       3           3.5     6           10.5        2.75
cus2        C       3           2       6           6           2.75
cus3        D       6           4       6           24          4

在dbfiddle上演示

tjjdgumg

tjjdgumg2#

select  customerID, item, quantity, price,
            (select sum(quantity) from stock s2 where stock.customerID = s2.customerID ) as tot_qua,
            price * quantity as tot_pri,
            (select sum(quantity * price) / sum(quantity) from stock s2 where stock.customerID = s2.customerID) as `sumtotpri/tot_qua`
from stock
order by stock.customerID, stock.item;
mhd8tkvw

mhd8tkvw3#

在mysql 8+中,您只需使用窗口函数:

select s.*,
       sum(quan) over (partition by cusid) as tot_quan,
       (quan * price) as tot_price,
       sum(quan * price) over (partition by cusid) / sum(quan) over (partition by cusid) as ratio
from stock s;

在早期版本中,您将使用子查询或类似的机制。可能 join 以及 group by 最简单的方法是:

select s.*, s2.tot_quan, (s.quan * s.price) as tot_price,
       tot_tot_price / tot_quan as ratio
from stock s join
     (select cusid, sum(quan) as tot_quan,
            sum(quan * price) as tot_tot_price
      from stock s2
      group by cusid
     ) s2
     using (cusid);

相关问题