如何统计MySQL中每一列的数据?

发布于 2024-09-19 18:41:23 字数 1175 浏览 3 评论 0原文

我有一些用于数据透视表的 mysql 脚本,然后计算其中的一些数据:

SELECT A.Line,
       week1.1stweek, week2.2ndweek,
       IFNULL(week1.1stweek,0) + IFNULL(week2.2ndweek,0) AS TOTAL
FROM inspection_report AS A
LEFT JOIN(
           SELECT Line, (SUM(S) + SUM(A) + SUM(B)*0.4 + SUM(C)*0.1)/COUNT(Serial_number) AS 1stweek
           FROM inspection_report
           WHERE DAY(Inspection_datetime) BETWEEN 1 AND 7
           GROUP BY Line, WEEK(Inspection_datetime), YEAR(Inspection_datetime)
          ) AS week1 USING (Line)
LEFT JOIN(
           SELECT Line, (SUM(S) + SUM(A) + SUM(B)*0.4 + SUM(C)*0.1)/COUNT(Serial_number) AS 2ndweek
           FROM inspection_report
           WHERE DAY(Inspection_datetime) BETWEEN 8 AND 14
           GROUP BY Line, WEEK(Inspection_datetime), YEAR(Inspection_datetime)
          ) AS week2 USING (Line)
GROUP BY Line

它使表头显示为按钮。 之后我想在每列下面做一个“SUM”,例如:

Line          1stweek            2ndweek             total
1               12                 2                   14
2               3                  0                    3
SUM             15                 2                   17

我该怎么做?

i have some mysql script for pivot table and then counting some data inside that:

SELECT A.Line,
       week1.1stweek, week2.2ndweek,
       IFNULL(week1.1stweek,0) + IFNULL(week2.2ndweek,0) AS TOTAL
FROM inspection_report AS A
LEFT JOIN(
           SELECT Line, (SUM(S) + SUM(A) + SUM(B)*0.4 + SUM(C)*0.1)/COUNT(Serial_number) AS 1stweek
           FROM inspection_report
           WHERE DAY(Inspection_datetime) BETWEEN 1 AND 7
           GROUP BY Line, WEEK(Inspection_datetime), YEAR(Inspection_datetime)
          ) AS week1 USING (Line)
LEFT JOIN(
           SELECT Line, (SUM(S) + SUM(A) + SUM(B)*0.4 + SUM(C)*0.1)/COUNT(Serial_number) AS 2ndweek
           FROM inspection_report
           WHERE DAY(Inspection_datetime) BETWEEN 8 AND 14
           GROUP BY Line, WEEK(Inspection_datetime), YEAR(Inspection_datetime)
          ) AS week2 USING (Line)
GROUP BY Line

it makes the table head show like button.
after that i want to make a "SUM" below each column like:

Line          1stweek            2ndweek             total
1               12                 2                   14
2               3                  0                    3
SUM             15                 2                   17

how do i do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮邮递员 2024-09-26 18:41:39

这让我很感兴趣,因此我在此处的在线 SQL 沙箱中实现了等效项。

选择 ifnull(toy, "SUM") 作为玩具,sum(s1.store1) 作为 store2,sum(s2.store2) 作为 store1,sum(ifnull(store1,0)+ifnull(store2,0)) as Total from (select toy_id, sum(inventory) as store1 from store_inventory where store_id=1 group by toy_id) as s1 left join (select toy_id, sum(inventory) as store2 from store_inventory where store_id=2 group by toy_id) as s2 using ( toy_id) left join toy_info using (toy_id) group by toy with rollup;

(请原谅我缺乏 Markdown 技能,这是我的第一篇文章。)

This interested me, so I implemented a equivalent in an online SQL sandbox, here.

select ifnull(toy, "SUM") as toy, sum(s1.store1) as store2, sum(s2.store2) as store1, sum(ifnull(store1,0)+ifnull(store2,0)) as total from (select toy_id, sum(inventory) as store1 from store_inventory where store_id=1 group by toy_id) as s1 left join (select toy_id, sum(inventory) as store2 from store_inventory where store_id=2 group by toy_id) as s2 using (toy_id) left join toy_info using (toy_id) group by toy with rollup;

(Pardon my lack of markdown skills, this is my first post.)

难得心□动 2024-09-26 18:41:35

使用 WITH ROLLUP 修饰符。

http://dev.mysql.com/doc/ refman/5.0/en/group-by-modifiers.html

您必须修改外部 select 语句才能

SELECT A.Line,
       sum(week1.1stweek) as 1stweek, sum(week2.2ndweek) 2ndweek,
       sum(IFNULL(week1.1stweek,0) + IFNULL(week2.2ndweek,0)) AS TOTAL

使修饰符生效。如果您需要将其标记为这样,请将 A.Line 更改为 IFNULL(A.Line, "SUM") as Line

Use the WITH ROLLUP modifier.

http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html

You'd have to modify the outer select statement though to

SELECT A.Line,
       sum(week1.1stweek) as 1stweek, sum(week2.2ndweek) 2ndweek,
       sum(IFNULL(week1.1stweek,0) + IFNULL(week2.2ndweek,0)) AS TOTAL

for the modifier to take effect. Change A.Line to IFNULL(A.Line, "SUM") as Line instead if you need it to be labelled as such.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文