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

The Archive Base Latest Questions

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

I have a column of numbers in a MySQL database, along with a timestamp

  • 0

I have a column of numbers in a MySQL database, along with a timestamp column and other stuff. I want to know the average of all of the values within a set parameter (say, within the last week), and I also want the average of all of the values that are >= X during that same timeframe. So, if my X is 10, and my values are:
0 0 10 15 20, then I want avg = 9 and avg2 = 15. One way I can do this is run two separate queries, with and without WHERE val >= 10, but I’d like to do it in just one query, if possible. Is there a way of doing this? I’ve looked at using a CASE, but I can’t get the syntax right. Below is the query that I tried:

SELECT AVG(watts), AVG(CASE watts WHEN watts >= 10 THEN watts END CASE) FROM power WHERE meterID = 100 AND time >= '2010-07-29 00:00:00'

Edit: This is what I ended up with.

SELECT AVG(watts) AS avg1,
AVG(IF (watts >= 10, watts, NULL)) AS avg2
FROM power
WHERE meterID = 100 AND time >= '2010-07-30 00:00:00'
  • 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-16T00:39:53+00:00Added an answer on May 16, 2026 at 12:39 am
    SELECT
       AVG(watts),
       SUM(IF(watts>10,watts,0))/SUM(IF(watts>10,1,0))
    FROM FOO
    WHERE
       meterID   = 100 and
       time      >= '2010-07-29 00:00:00'
    

    Version 2

    SELECT
     AVG(watts),
     IF(SUM(IF(watts>10,1,0))=0,NULL,SUM(IF(watts>10,watts,0))/SUM(IF(watts>10,1,0)))
    FROM FOO
    WHERE
       meterID   = 100 and
       time      >= '2010-07-29 00:00:00'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All tables in the database have one timestamp column [ float ] followed by
I have column in a postgresql database. They are lottery numbers. Four digits in
I have an integer column and I want to find numbers that start with
So we have a database column that can contain just about anything. Unicode, numbers,
If I have two tables in a MySQL database that both have a column
I have a MySQL database with a column of numeric value. When I query
I have a MySQL database with, amongst others, contains a created_at column. The column
i have a database and I want to sort the a column according to
I have a MySQL database that contains all the words in the standard English
So I have a database setup in MySQL with three columns. The first column

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.