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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:10:51+00:00 2026-05-13T21:10:51+00:00

So I’ve got 3 different columns (basket 1, 2, and 3). Sometimes these columns

  • 0

So I’ve got 3 different columns (basket 1, 2, and 3). Sometimes these columns have all the information and sometimes one or two of them are null. I have another column that I’m going to average these values into and save.

Is there a sleek/easy way to get the average of these three columns even if one of them is null? Or do I have to have a special check for each one being null?

Example data( ~~ is null)

- B1 - B2 - B3 - Avg
------------------------------
- 10 - 20 - 30 - 20
- 10 - ~~ - 30 - 20
- ~~ - 20 - ~~ - 20

How would I write the T-SQL to update my temp table?

UPDATE @MyTable
   SET Avg = ???

Answer:
Thanks to Aaronaught for the method I used. I’m going to put my code here just in case someone else has the same thing.

WITH AverageView AS
(
    SELECT Results_Key AS xxx_Results_Key,
            AVG(AverageValue) AS xxx_Results_Average
    FROM @MyResults
        UNPIVOT (AverageValue FOR B IN (Results_Basket_1_Price, Results_Basket_2_Price, Results_Basket_3_Price)) AS UnpivotTable
    GROUP BY Results_Key
)   
UPDATE @MyResults
    SET Results_Baskets_Average_Price = xxx_Results_Average
    FROM AverageView
    WHERE Results_Key = xxx_Results_Key;
  • 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-13T21:10:51+00:00Added an answer on May 13, 2026 at 9:10 pm

    Assuming you have some sort of ID column, the most effective way is probably to use UNPIVOT so you can use the normal row-based AVG operator (which ignores NULL values):

    DECLARE @Tbl TABLE
    (
        ID int,
        B1 int,
        B2 int,
        B3 int
    )
    
    INSERT @Tbl (ID, B1, B2, B3) VALUES (1, 10, 20, 30)
    INSERT @Tbl (ID, B1, B2, B3) VALUES (2, 10, NULL, 30)
    INSERT @Tbl (ID, B1, B2, B3) VALUES (3, 10, NULL, NULL)
    
    SELECT ID, AVG(Value) AS Average
    FROM @Tbl
    UNPIVOT (Value FOR B IN (B1, B2, B3)) AS u
    GROUP BY ID
    

    If you don’t have the ID column, you can generate a surrogate ID using ROW_NUMBER:

    ;WITH CTE AS
    (
        SELECT
            B1, B2, B3,
            ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS ID
        FROM @Tbl
    )
    SELECT ID, AVG(Value)
    FROM CTE
    UNPIVOT (Value FOR B IN (B1, B2, B3)) AS u
    GROUP BY ID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i got an object with contents of html markup in it, for example: string
I've got a string that has curly quotes in it. I'd like to replace
I have just tried to save a simple *.rtf file with some websites and
I have a JSP page retrieving data and when single or double quotes are
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.