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 7894909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:30:49+00:00 2026-06-03T07:30:49+00:00

I’ve tried everything to figure this out but I can’t get the correct total.

  • 0

I’ve tried everything to figure this out but I can’t get the correct total. My attempts either add all the records and not just the latest ones or I only get the first record.

My first table: hubs

hubID    hubName
1           hub1
2           hub2

My second table: hub_reports

reportID    hubID          date          health         school 
1               1          2012-04-27    467            322 
2               2          2012-04-23    267            22 
3               1          2012-01-20    176            623 

So what you see is 2 tables, one with the organizations name and other info and the second with the reports that each organization submits quarterly. I want to list all the organizations and their latest report. At the bottom of the table I want to add all the available health kits and school kits that are currently available.

Here’s the code I’m using right now to display all the organizations and their latest reports.

SELECT * FROM (SELECT hubName, date, health, school FROM hub_reports,
hubs WHERE hub_reports.hubID = hubs.hubID ORDER BY date DESC) AS Total
GROUP BY hubName

This seems to work but when I try the same tactic to get the SUM of the health and school columns I don’t get the right answer.

SELECT SUM(health) FROM (SELECT hubName, date, health FROM
hub_reports, hubs WHERE hub_reports.hubID = hubs.hubID ORDER BY date
DESC) AS Total GROUP BY hubName

I tried other using a LEFT JOIN approach that I found on another forum but it didn’t seem to work any better. But I maybe I wasn’t doing it right.

Please help!

  • 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-06-03T07:30:51+00:00Added an answer on June 3, 2026 at 7:30 am

    I just encountered a similar problem in a project of mine. A variation of this query worked for me. Hope it is helpful to you.

    SELECT hubs.hubName, hub_reports.*, 
    SUM(hub_reports.health) AS ttl_health, 
    SUM(hub_reports.school) AS ttl_school
    FROM hubs, hub_reports 
    WHERE hub_reports.hubID = hubs.hubID 
    GROUP BY hub_reports.hubID
    ORDER BY hub_reports.date DESC
    

    Here’s the PHP:

    $rs = mysql_query( 'SELECT hubs.hubName, hub_reports.*, 
        SUM(hub_reports.health) AS ttl_health, 
        SUM(hub_reports.school) AS ttl_school
        FROM hubs, hub_reports 
        WHERE hub_reports.hubID = hubs.hubID 
        GROUP BY hub_reports.hubID
        ORDER BY hub_reports.date DESC' );
    
    $grand_total['school']=0;
    $grand_total['health']=0;
    
    while ( $row = mysql_fetch_assoc( $rs ) ){ // Step through each hub
        echo "{$row['hubName']} shows {$row['ttl_school']} total school, {$row['ttl_health']} total health";
        $grand_total['school'] += $row['ttl_school'];
        $grand_total['health'] += $row['ttl_health'];
    }
    
    echo "Grand Total School: {$grand_total['school']}, Grand Total Health: {$grand_total['health']}";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

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.