?
Solved

Calc Average Value for Values in Row of Matrix

Posted on 2007-11-21
8
Medium Priority
?
986 Views
Last Modified: 2012-05-05
I've have a returns matrix set up and I'd like to calculate the average values across a row.  Are there any built-in functions to do something like this?  I'll need to account for nulls etc.  Max there would be 12 columns of data.

Ex:
2,5,4, null are all the values in a row so the avg would be 3.66

Thanks!
0
Comment
Question by:gigglick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 20329017
No, nothing built in, you will have to code it yourself:

SELECT ...,
    (ISNULL(col1, 0) + ISNULL(col2, 0) + ... + ISNULL(col12, 0) /
    NULLIF(
    (CASE WHEN col1 IS NULL THEN 0 ELSE 1 END +
     CASE WHEN col2 IS NULL THEN 0 ELSE 1 END +
     ... +
     CASE WHEN col12 IS NULL THEN 0 ELSE 1 END), 0) AS AvgAcrossCols,
    ...
0
 
LVL 6

Expert Comment

by:messen1975
ID: 20329050
" Max there would be 12 columns of data."

Is there a variable number of columns displayed?
0
 
LVL 5

Author Comment

by:gigglick
ID: 20329098
Hi - didn't think there was anything built - DARN!  The storage table for the matrix will always have twelve columns, however, not all accounts on our end will have enough data to fill all twelve columns of the matrix.  Also, I am giving the user the option to choose the number of periods/columns they want.  So the problem is substituting 0 in for null will result in the wrong average.
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 20329119
Not with the code I posted it won't :-).  When I divide to get the average, I *don't count* those that are NULL, only those that aren't NULL.
0
 
LVL 6

Expert Comment

by:messen1975
ID: 20329130
Scotts answer should work if there are a fixed number of columns.
0
 
LVL 5

Author Comment

by:gigglick
ID: 20329185
Hi Scott -
I should have mentioned that I wasn't directing that at your reponse...but code I was toying with on my end.  I'm trying yours out now...

Giggs
0
 
LVL 5

Author Comment

by:gigglick
ID: 20330024
Hi Scott -

Worked perfectly and quick!! Thanks so much for your help.

Giggs
0
 
LVL 5

Author Closing Comment

by:gigglick
ID: 31410402
How does he always get it right?
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question