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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:10:24+00:00 2026-05-28T03:10:24+00:00

I have the following query: $strQuery = SELECT siteid, SUM(watts) AS wattage, unit, device,

  • 0

I have the following query:

$strQuery = "SELECT siteid, SUM(watts) AS wattage, unit, device, time FROM inverter WHERE siteid = '528' AND time Between '$time1' AND '$time2' Order By device Asc";

I’m making a graph in fusion charts and need the total watts for each device but when i do the query above it takes all values and places them for just the first device. I have 40 devices and need each one to have its total watts produced.

On the chart i am displaying device as the x axis name label and the wattage as the value.

Thanks

  • 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-28T03:10:24+00:00Added an answer on May 28, 2026 at 3:10 am

    SUM() is an aggregate function which should be used together with a GROUP statement. If GROUP statement is omitted, all selected rows are aggregated as a group.

    see docs: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html

    Look into GROUP BY statement:

    SELECT siteid, device, 
      SUM(watts) AS wattage, 
      MIN(time) as start_time, 
      MAX(time) as end_time 
      unit
    FROM inverter
    WHERE siteid = '528' 
      AND time BETWEEN '$time1' AND '$time2'
    GROUP BY device, unit
    ORDER BY device ASC
    

    Also, when you use aggregation and select fields that are not contained in the GROUP statement (here: time) you must make sure you really get the value you want.

    The values of the aggregated row you get for each device are calculated using many rows with possibly individual values in those fields. When you don’t use an aggregate function (MIN, MAX, AVG, ..) to determine which of the multiple values your aggregated row should contain, you will receive a value selected more or less selected randomly by MySQL.

    You should either

    • do not select such field, when not necessary. (e.g. time)
    • use an appropriate aggregate function (MIN, MAX, AVG, ..) for each field that is not contained in the GROUP BY clause to ensure you get the right value. (here i inserted both start and end time for the measure timespan using MIN and MAX)
    • or include it in the GROUP BY clause
      (i did that for “unit” in the example, because you get a wrong result when using SUM on both megawatts and kilowatts .. i assume that is what you use “unit” for?)

    Again: Otherwise the resulting value in such fields is random and not deterministic.

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

Sidebar

Related Questions

I have the following query: select column_name, count(column_name) from table group by column_name having
I have the following query: SELECT c.* FROM companies AS c JOIN users AS
I have the following query: SELECT rowid FROM table1 ORDER BY RANDOM() LIMIT 1
I have the following query SELECT e.topicShortName, d.catalogFileID, e.topicID FROM catalog_topics a LEFT JOIN
I have following query working : SELECT COUNT(id), AgeRange FROM ( select id, case
I have following query, SELECT UNIX_TIMESTAMP() - UNIX_TIMESTAMP(rc_timestamp) AS LAG_TIME FROM recentchanges ORDER BY
I have following query in PostgreSQL: SELECT COUNT(a.log_id) AS overall_count FROM Log as a,
I have following SQL query that selects some results from my table: select avg(c3),
i have following query SELECT *, count(jx_commissions.commission_amount) AS summe FROM jx_members INNER JOIN jx_commissions
I have following sql query for finding 2nd max salary. Select * From Employee

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.