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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:31:41+00:00 2026-06-09T05:31:41+00:00

Back again I’m afraid! I have this SQL Query – populating a table. echo

  • 0

Back again I’m afraid!

I have this SQL Query – populating a table.

echo SQLResultTable("SELECT SalesExec, COUNT(SalesExec)AS 'Total<br/> Checks', $AverageScore, `SUM(Autofails) AS 'Total <br/>AutoFails', $AverageAutofails FROM Data_Table WHERE BranchManager = '$BM'");`

In the database, I currently have 3 records. 2 for Sales Exec A, 1 for Sales Exec2.

The output form the above, is producing one row in a table. with just Sales Exec1’s name. However all the other fields are adding up / averaging etc, all 3 records values.

How do I get the table to produce the proper 2 rows I need with the averages/counts/sums etc just applying to the record that relates to that Sales Exec?

To get the table working, I snagged this function from a different site, that seems to work lovely other than this issue.

function SQLResultTable($Query)
{
    $host = "localhost";
$user = "root";
$pass = "";
$db = "Quality_Monitoring";
    $link = mysql_connect($host, $user, $pass) or die('Could not connect: ' . mysql_error());      //build MySQL Link
    mysql_select_db($db) or die('Could not select database');        //select database
    $Table = "";  //initialize table variable

    $Table.= "<table border='1' style=\"border-collapse: collapse; text-align: center; font-size: small; cellspacing: 5px; \">"; //Open HTML Table

    $Result = mysql_query($Query); //Execute the query
    if(mysql_error())
    {
        $Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>";
    }
    else
    {
        //Header Row with Field Names
        $NumFields = mysql_num_fields($Result);
        $Table.= "<tr style=\"background-color: #000066; text-align: center; color: #FFFFFF;\">";
        for ($i=0; $i < $NumFields; $i++)
        {     
            $Table.= "<th>" . mysql_field_name($Result, $i) . "</th>"; 
        }
        $Table.= "</tr>";

        //Loop thru results
        $RowCt = 0; //Row Counter
        while($Row = mysql_fetch_assoc($Result))
        {
            //Alternate colors for rows
            if($RowCt++ % 2 == 0) $Style = "background-color: #00CCCC;";
            else $Style = "background-color: #CCCCCC;";

            $Table.= "<tr style=\"$Style\">";
            //Loop thru each field
            foreach($Row as $field => $value)
            {
                $Table.= "<td>$value</td>";
            }
            $Table.= "</tr>";
        }
       // $Table.= "<tr style=\"background-color: #000066; color: #FFFFFF;\"><td colspan='$NumFields'>Query Returned " . mysql_num_rows($Result) . " records</td></tr>";
    }
    $Table.= "</table>";

    return $Table;

}

?>

Any help appreciated, cheers!

  • 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-09T05:31:43+00:00Added an answer on June 9, 2026 at 5:31 am

    The SQL statement makes use of aggregate functions COUNT(), SUM() but has no GROUP BY. GROUP BY SalesExec. Because MySQL is lenient about the presence and contents of the GROUP BY clause, it will return one row, probably with the first SalesExec, but with aggregates calculated over all SalesExec, which is not a meaningful result.

    echo SQLResultTable("
      SELECT SalesExec, COUNT(SalesExec)AS 'Total<br/> Checks', $AverageScore, `SUM(Autofails) AS 'Total <br/>AutoFails', $AverageAutofails 
      FROM Data_Table 
      WHERE BranchManager = '$BM' 
      GROUP BY SalesExec
    ");`
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

back again with some more SQLAlchemy shenanigans. Let me step through this. My table
I am back again with another python query. I have been trying to do
Back again, thanks for all the help last time. I'm running this query: $query
Back again this time working with data providers. Well i been doing a bit
Back again with another Flex question. I have an XML structure like... <Student> <Name>X</Name>
How do I convert hexadecimal to decimal (and back again) using Oracle SQL?
Back again! I've been doing some async socket programming with select() on windows,and it's
Possible Duplicate: Split A4 PDF page into two A5 and back again I have
Back again with yet another DirectSound question, this one regarding the ways DirectSound Buffers
I have a Gallery object that scrolls from left to right and back again.

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.