简体   繁体   English

MySQL:获取每行的总数,然后获取每个“分组依据”的总数

[英]MySQL: Get total each row then get total each 'group by'

Sorry about the title, I don't know what's the proper term to use.对不起,标题,我不知道什么是正确的术语使用。

I have this data:我有这个数据:

在此处输入图片说明

I want to produce a table something like this:我想制作一个这样的表格:

在此处输入图片说明

I want to get the total of each 'group by' and I'm doing this code but I'm getting wrong total.我想得到每个'group by'的总数,我正在做这个代码,但我得到的总数是错误的。

SELECT code, (a * b)
AS total_each_code
FROM  table1   
GROUP BY code 

UPDATE: updated photos of sample data, sorry for typos.更新:更新了样本数据的照片,抱歉打字错误。

you can use sub query for this您可以为此使用子查询

select code, sum(a.total_each_code)
         from (
            SELECT code, (a * b)
                AS total_each_code
                FROM  table1   order by code
                )a 
        group by a.total_each_code

or just simple as或者只是简单的

select code,sum(a*b) as total from table1 group by code.

Use sum() aggregation使用sum()聚合

SELECT code, sum(a * b)
AS total_each_code
FROM  table1   
GROUP BY code

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM