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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:53:35+00:00 2026-05-13T23:53:35+00:00

I have a large table (of sites) with several numeric columns – say a

  • 0

I have a large table (of sites) with several numeric columns – say a through f. (These are site rankings from different organizations, like alexa, google, quantcast, etc. Each has a different range and format; they’re straight dumps from the outside DBs.)

For many of the records, one or more of these columns is null, because the outside DB doesn’t have data for it. They all cover different subsets of my DB.

I want column t to be their weighted average (each of a..f have static weights which I assign), ignoring null values (which can occur in any of them), except being null if they’re all null.

I would prefer to do this with a simple SQL calculation, rather than doing it in app code or using some huge ugly nested if block to handle every permutation of nulls. (Given that I have an increasing number of columns to average over as I add in more outside DB sources, this would be exponentially more ugly and bug-prone.)

I’d use AVG but that’s only for group by, and this is w/in one record. The data is semantically nullable, and I don’t want to average in some “average” value in place of the nulls; I want to only be counting the columns for which data is there.

Is there a good way to do this?

Ideally, what I want is something like UPDATE sites SET t = AVG(a*@a_weight,b*@b_weight,...) where any null values are just ignored and no grouping is happening.

EDIT: What I ended up using, based on van’s and adding in correct weighted averages (assuming that a has already been normalized as needed, in this case to a float 0-1 (1 = better):

 UPDATE sites 
 SET t = (@a_weight * IFNULL(a, 0) + ...) / (IF(a IS NULL, 0, @a_weight) + ...) 
 WHERE (IF(a IS NULL, 0, 1) + ...) > 0
  • 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-13T23:53:36+00:00Added an answer on May 13, 2026 at 11:53 pm
    UPDATE  sites
            --// TODO: you might need to round it depending on your type
    SET     t =(COALESCE(a, 0) +
                COALESCE(b, 0) +
                COALESCE(c, 0) +
                COALESCE(d, 0) +
                COALESCE(e, 0) +
                COALESCE(f, 0)
               ) /
               ((CASE WHEN a IS NULL THEN 0 ELSE 1 END CASE) +
                (CASE WHEN b IS NULL THEN 0 ELSE 1 END CASE) +
                (CASE WHEN c IS NULL THEN 0 ELSE 1 END CASE) +
                (CASE WHEN d IS NULL THEN 0 ELSE 1 END CASE) +
                (CASE WHEN e IS NULL THEN 0 ELSE 1 END CASE) +
                (CASE WHEN f IS NULL THEN 0 ELSE 1 END CASE)
               )
    WHERE   0<>((CASE WHEN a IS NULL THEN 0 ELSE 1 END CASE) +
                (CASE WHEN b IS NULL THEN 0 ELSE 1 END CASE) +
                (CASE WHEN c IS NULL THEN 0 ELSE 1 END CASE) +
                (CASE WHEN d IS NULL THEN 0 ELSE 1 END CASE) +
                (CASE WHEN e IS NULL THEN 0 ELSE 1 END CASE) +
                (CASE WHEN f IS NULL THEN 0 ELSE 1 END CASE)
               )
    

    You could use COALESCE also in the other parts, but this will not handle the case when you have a rating with value 0 properly because it will be excluded. The WHERE clause avoids DivideByZero, but you might need to have additional UPDATE statement to handle this case, if there is no rating for the entry.

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

Sidebar

Related Questions

We have a large table (450 million rows containing 34 columns of numeric or
I have a large table I imported from an Excel spreadsheet. For five of
I have a large table with a multi-part index. I need to run several
I have a large table that get populated from a view. This is done
I have a SQL Server 2005 database from which I'm removing several large tables
I have large database table, approximately 5GB, now I wan to getCurrentSnapshot of Database
I have a large table (between 74 and 88million rows) which is the middle
I have a large table (~1M rows now, soon ~10M) that has two ranked
I have a large table I want to include in a LaTeX document. It
1) My question is that, when i have two large table which cannot be

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.