Solved

SQL Select - AVG

Posted on 2016-09-21
3
61 Views
Last Modified: 2016-09-22
I need to know how to average multiple columns in SQL Select.  There 3 columns in the table and I want to compute two averages.  Average 1 would be average of the Base Model Nbr and Image NBR and Average 2 would be the average of the Base Model Nbr, Image Nbr and New Features Nbr.

My Table:

Supplier ID     Base Model Nbr    Image Nbr  New Features Nbr
100                                   0.04                      0.03                  0.03                    
1002                           0.09                      1.00                  0.00
1006                           0.50                      0.50            1.00
10082                           1.00                      1.00                 1.00

I need two additional columns to compute these averages so the output would look like this:

Supplier ID     Base Model Nbr    Image Nbr  New Features Nbr  Average 1    Average 2
100                                   0.04                      0.03                  0.03                           .035          .0335
1002                           0.09                      1.00                  0.00                          .545           .0363
1006                           0.50                      0.50            1.00                           0.5             0.666
10082                           1.00                      1.00                 1.00                                1             1
0
Comment
Question by:tmajor99
[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
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 500 total points
ID: 41809495
If you do not need to compute the average across rows, this simple query should work:
SELECT SupplierID, [Base Model Nbr], [Image Nbr], [New Features Nbr],
    ([Base Model Nbr] + [Image Nbr]) / 2.00 [Average 1], 
    ([Base Model Nbr] + [Image Nbr] + [New Features Nbr]) / 3.00 [Average 2]
FROM <your table>

Open in new window

0
 
LVL 35

Expert Comment

by:YZlat
ID: 41809562
try

SELECT SupplierID, BaseModelNbr, ImageNbr, NewFeaturesNbr, (BaseModelNbr + ImageNbr)/2 As Average1, (BaseModelNbr + ImageNbr + NewFeaturesNbr)/3 As Average2
FROM MyTable

Open in new window

0
 
LVL 35

Expert Comment

by:YZlat
ID: 41809566
Shaune Kline beat me to it:)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

749 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