如何统计MySQL中每一列的数据?
我有一些用于数据透视表的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这让我很感兴趣,因此我在此处的在线 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.)
使用
WITH ROLLUP
修饰符。http://dev.mysql.com/doc/ refman/5.0/en/group-by-modifiers.html
您必须修改外部 select 语句才能
使修饰符生效。如果您需要将其标记为这样,请将
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
for the modifier to take effect. Change
A.Line
toIFNULL(A.Line, "SUM") as Line
instead if you need it to be labelled as such.