Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1065709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:52:27+00:00 2026-05-16T19:52:27+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T19:52:29+00:00Added an answer on May 16, 2026 at 7:52 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My scenario is that I have some payment transaction data in MySQL and some
I have some CSV data files that I want to import into mySQL. I
I have some PHP script for export MYSQL table into Excel format, but I
I have a php script that makes some MySQL queries that just writes some
I have a PHP/mySQL search script that for some reason, after the latest update
Greetings, I have some mysql tables that are currently using an md5 hash as
I have some problem with MySQL Workbench in that I sometimes can't set foreign
I have some data, yes, data. This data came from a MySQL query and
I have a simple php/mysql script that stores in mysql database id, link, and
I have some software that is heavily dependent on MySQL, and is written in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.