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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:36:02+00:00 2026-06-06T21:36:02+00:00

I have got the following query: WITH data AS( SELECT * FROM partstat WHERE

  • 0

I have got the following query:

WITH data AS( 
    SELECT * FROM partstat WHERE id=4
)
SELECT id, AVG(Value) AS Average
FROM (
    SELECT id, 
    AVG(column_1) as column_1, 
    AVG(column_2) as column_2, 
    AVG(column_3) as column_3 
    FROM data
    GROUP BY id
) as pvt
UNPIVOT (Value FOR V IN (column_1,column_2,column_3)) AS u
GROUP BY id

if column_1,column_2 and column_3 (or one of this columns) have values then i get result as the following:

id, Average

4, 5.12631578947368

if column_1,column_2 and column_3 have NULL values then the query does not return any rows as the following:

id, Average

my question is how can i get as the following result if columns contents NULL values?

id, Average

4, NULL

  • 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-06-06T21:36:04+00:00Added an answer on June 6, 2026 at 9:36 pm

    Have you tried using COALESCE or ISNULL?

    e.g.

    ISNULL(AVG(column_1), 0) as column_1,   
    

    This does mean that you will get 0 as the result instead of ‘NULL’ though – do you need null when they are all NULL?

    Edit:

    Also, is there any need for an unpivot? Since you are specifying all 3 columns, why not just do:

    SELECT BankID, (column_1 + column_2 + column_3) / 3 FROM partstat
    WHERE bankid = 4
    

    This gives you the same results but with the NULL

    Of course this is assuming you have 1 row per bankid

    Edit:

    UNPIVOT isn’t supposed to be used like this as far as I can see – I’d unpivot first then try the AVG… let me have a go…

    Edit:

    Ah I take that back, it is just a problem with NULLs – other posts suggest ISNULL or COALESCE to eliminate the nulls, you could use a placeholder value like -1 which could work e.g.

    SELECT bankid, AVG(CASE WHEN value = -1 THEN NULL ELSE value END) AS Average 
    FROM ( 
        SELECT bankid,  
        isnull(AVG(column_1), -1) as column_1 ,
        AVG(Column_2) as column_2 ,
        Avg(column_3) as column_3 
        FROM data     
        group by bankid
    ) as pvt 
    UNPIVOT (Value FOR o in (column_1, column_2, column_3)) as u
    GROUP BY bankid 
    

    You need to ensure this will work though as if you have a value in column2/3 then column_1 will no longer = -1. It might be worth doing a case to see if they are all NULL in which case replacing the 1st null with -1

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

Sidebar

Related Questions

I have got the following exception when try to select data from SQL Server
All, I've got the following SQL query as of now: SELECT * FROM $wpdb->posts
I have the following ms-sql query select * from category where categoryId in (1)
I have the following code and got myself confused: I have a query that
I have the following SQL Query: select Subjects.S_ID as ID, Subjects.S_ParentID as ParentID, Subjects.S_Name
I have the following jquery Autocomplete form that pulls data in from a local
I have drawn up the following PHP code which processes the data from a
Folks, we have the following problem: we've got several objects containing table data that
I have got the following class structure, and have plenty of classes like C
I have got the following very simple code: function init() { var articleTabs =

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.