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

  • Home
  • SEARCH
  • 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 137579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:07:42+00:00 2026-05-11T07:07:42+00:00

I need to query this DB to get each row, but also the SUM

  • 0

I need to query this DB to get each row, but also the SUM of one of the column values of the results. I could use php to get the total value, but then I’d need to run two loops, one to get the total (which goes at the top above the results). So I’d prefer the query to catch it and just make a ‘total’ row, but the only way I’ve gotten it to work is with a subquery that is essentially a repeat of the original query. Is there a better way?

SELECT  CONCAT(u.firstname, ' ', u.lastname ) name, u.id, s.description, s.shiftstart, s.shiftend,      (SELECT      SUM( TIME_TO_SEC( TIMEDIFF( shiftend, shiftstart ) ) ) /3600     FROM shifts     WHERE id =  '$user'     AND DATE( shiftstart )     BETWEEN '$start'     AND '$end') total FROM shifts s INNER JOIN users u ON ( s.id = u.id ) WHERE s.id = '$user' AND DATE( shiftstart ) BETWEEN '$start' AND '$end' ORDER BY shiftstart 

The above works and outputs:

name        id     description  shiftstart             shiftend               total Joe User    joeuser    Stuff    2009-01-05 07:45:00    2009-01-05 12:15:00    39.5000 Joe User    joeuser    Stuff    2009-01-05 13:00:00    2009-01-05 17:00:00    39.5000 Joe User    joeuser    Stuff    2009-01-06 07:45:00    2009-01-06 10:45:00    39.5000 Joe User    joeuser    Stuff    2009-01-06 10:45:00    2009-01-06 12:45:00    39.5000 Joe User    joeuser    Stuff    2009-01-06 13:30:00    2009-01-06 14:30:00    39.5000 Joe User    joeuser    Stuff    2009-01-06 14:30:00    2009-01-06 17:00:00    39.5000 Joe User    joeuser    Stuff    2009-01-07 09:45:00    2009-01-07 14:00:00    39.5000 Joe User    joeuser    Stuff    2009-01-07 15:00:00    2009-01-07 17:00:00    39.5000 Joe User    joeuser    Stuff    2009-01-08 08:00:00    2009-01-08 12:15:00    39.5000 Joe User    joeuser    Stuff    2009-01-08 13:15:00    2009-01-08 17:00:00    39.5000 Joe User    joeuser    Stuff    2009-01-09 07:45:00    2009-01-09 10:45:00    39.5000 Joe User    joeuser    Stuff    2009-01-09 11:45:00    2009-01-09 15:15:00    39.5000 Joe User    joeuser    Stuff    2009-01-09 15:15:00    2009-01-09 17:00:00    39.5000 

Which is what I need, but probably not the best way to get it.

  • 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. 2026-05-11T07:07:43+00:00Added an answer on May 11, 2026 at 7:07 am

    MySQL supports a special group-by modifier called ROLLUP.

    SELECT CONCAT(u.firstname, ' ', u.lastname ) name, u.id,    s.description, s.shiftstart, s.shiftend,    SUM( TIME_TO_SEC( TIMEDIFF( shiftend, shiftstart ) ) ) /3600 total FROM shifts s INNER JOIN users u ON ( s.id = u.id ) WHERE s.id = ? AND DATE( shiftstart ) BETWEEN ? AND ? GROUP BY u.id, s.shiftstart WITH ROLLUP ORDER BY shiftstart; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need query to get data between two days.I tried with this.But it gives
I need this query - update last but one record. UPDATE changes SET checked=''
I need to get UserCarId(int) from this query and assign to int type variable.
I need to rewrite this query and I'm not allowed to use a subquery.
I need to have MySQL query like this one: UPDATE table_name SET 1 =
what wrong with this Query ??? i need this Query. UPDATE user_preferences SET user_preferences_value
i'm building this little function, what i need is this query to print me
I need to do this query with Java + Hibernate. SELECT table2.id, COUNT(table2.id) AS
I need to do a query like this: SELECT wposts.ID FROM posts wposts WHERE
I need some help putting together this query in Django. I've simplified the example

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.