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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:20:08+00:00 2026-05-12T21:20:08+00:00

I have the following table: id | kwh | cost ——————— 1 | 10

  • 0

I have the following table:

id  |  kwh  |   cost
---------------------
 1  |  10   |   8.95
 2  |  20   |    0
 3  |  100  |  16.54   

I need to get the average cost.

If the cost is still the default (0), the cost value is calculated with the following formula: (7.87 + (kwh * 8.19)/100) * 1.03

So id 2’s cost will be estimated at 9.79 for an average cost of 35.28/3 = 11.76.

Is there a mySQL query that will calculate the cost if the column has the default value and use inputed value otherwise?

  • 1 1 Answer
  • 1 View
  • 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-12T21:20:08+00:00Added an answer on May 12, 2026 at 9:20 pm

    You can use a CASE expression to test whether the cost is 0:

    SELECT
      *,
      CASE cost WHEN 0 THEN (7.87 + (kwh * 8.19) / 100) * 1.03 ELSE cost END calc_cost
    FROM
      tablename
    

    You can then use this expression with AVG to get the average:

    SELECT
      AVG(CASE cost WHEN 0 THEN (7.87 + (kwh * 8.19) / 100) * 1.03 ELSE cost END)
    FROM
      tablename
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table: compare . I need to do an inner join
I have following table in hive user-id, user-name, user-address,clicks,impressions,page-id,page-name I need to find out
I have following table. <form method=post action=test.php> <input name=id[] type=text value=ID1 /> <input name=value[]
I have following table Managers (simplified): ID, int Name, nvarchar(100) In a stored procedure
I have following table Id | Status | date ------------------- 1 | Onsite |2007
I have the following table structure (in MySQL): DocID, Code, IsDup, DopOf , where
I have the following table. When i try to insert records using LINQ to
I have the following table of TIME datatypes clarification: I am representing hours/mins/seconds of
I have the following table EVENT_LOG : EVENT_ID: pk, int, not null TYPEID: fk,
I have the following table: mysql> SELECT * FROM `bright_promotion_earnings`; +----+----------+------------+----------+-------+ | id |

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.