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?
Use the
WITH ROLLUPmodifier.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.LinetoIFNULL(A.Line, "SUM") as Lineinstead if you need it to be labelled as such.