Solved

Average columns togather

Posted on 2014-03-31
11
109 Views
Last Modified: 2014-08-23
Hello:

I am trying to average two columns together. When I average them it gets adds the columns together instead of averaging it.

SELECT Question, JAN_Y, JAN_N, JAN_NA, JAN_TOTAL, FEB_Y, FEB_N, FEB_NA, FEB_TOTAL, AVG(JAN_TOTAL + FEB_TOTAL) AS TOTAL
(
SELECT Question, JAN_Y, JAN_N, JAN_NA, CONVERT(numeric(5,2), JAN_TOTAL) AS JAN_TOTAL, FEB_Y, FEB_N, FEB_NA, CONVERT(numeric(5,2), FEB_TOTAL) AS FEB_TOTAL   
FROM
(
SELECT Question,
JAN_Y,
JAN_N,
JAN_NA,
ISNULL((JAN_Y)/NULLIF((JAN_Y+JAN_N),0),0)*100 AS JAN_TOTAL,
FEB_Y,
FEB_N,
FEB_NA,
ISNULL((FEB_Y)/NULLIF((FEB_Y+FEB_N),0),0)*100 AS FEB_TOTAL
FROM
DATA
)
TABLE1
)
TABLE2
GROUP BY Question, JAN_Y, JAN_N, JAN_NA, JAN_TOTAL, FEB_Y, FEB_N, FEB_NA, FEB_TOTAL 

Open in new window

0
Comment
Question by:RecipeDan
[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
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 40

Expert Comment

by:lcohan
ID: 39967196
I'm not 100% sure what do you mean however...why not using SQL Aerage function instead?

http://technet.microsoft.com/en-us/library/ms177677(v=sql.105).aspx
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 39967214
I am using the average function

SELECT Question, JAN_Y, JAN_N, JAN_NA, JAN_TOTAL, FEB_Y, FEB_N, FEB_NA, FEB_TOTAL, AVG(JAN_TOTAL + FEB_TOTAL) AS TOTAL

Open in new window

0
 
LVL 40

Accepted Solution

by:
lcohan earned 500 total points
ID: 39967249
I meant like:

SELECT Question,
      JAN_Y,
      JAN_N,
      JAN_NA,
      CONVERT(numeric(9,2), ISNULL((JAN_Y)/NULLIF((JAN_Y+JAN_N),0),0)*100) AS JAN_TOTAL,
      FEB_Y,
      FEB_N,
      FEB_NA,
      CONVERT(numeric(9,2), ISNULL((FEB_Y)/NULLIF((FEB_Y+FEB_N),0),0)*100) AS FEB_TOTAL,
      AVG(JAN_TOTAL + FEB_TOTAL)
FROM DATA
GROUP BY Question, JAN_Y, JAN_N, JAN_NA, JAN_TOTAL, FEB_Y, FEB_N, FEB_NA, FEB_TOTAL
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 1

Author Comment

by:RecipeDan
ID: 39967271
It is still adding the columns
0
 
LVL 40

Expert Comment

by:lcohan
ID: 39967284
create view test as
SELECT Question,
      JAN_Y,
      JAN_N,
      JAN_NA,
      CONVERT(numeric(9,2),ISNULL(JAN_Y,0))/CONVERT(numeric(9,2), NULLIF((JAN_Y+JAN_N),0),0) * 100 AS JAN_TOTAL,
      FEB_Y,
      FEB_N,
      FEB_NA,
      CONVERT(numeric(9,2),ISNULL(FEB_Y,0))/CONVERT(numeric(9,2), NULLIF((FEB_Y+FEB_N),0),0) * 100 AS FEB_TOTAL
FROM DATA WITH (NOLOCK);
GO

select Question, AVG(JAN_TOTAL + FEB_TOTAL) as AvgTotal
from test
GROUP BY Question;
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39967292
This computation:
ISNULL((JAN_Y)/NULLIF((JAN_Y+JAN_N),0),0)*100 AS JAN_TOTAL
gives you a ratio (%) of Y to N: you can't accurately avg ratios anyway.

I think for this you need to use the other method of calculating an average: SUM() / COUNT().

That way, you can use CASE within the SUM and COUNT to get the specific average(s) you need.


I'd code it real quick for you, but I'm not sure what average(s) you're trying to get.

What average(s) are you trying to calculate?
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 39967318
The average of columns Jan_Total and Feb_Total
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 39967327
I am looking for something like this:

Avg(Jan_Total + Feb_Total) AS TwoMonthAvg
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39967357
But "Jan_Total" and "Feb_Total" are themselves ratios, and you can't avg a ratio.

So do you want the average of the total of Jan_Y = 1 or Feb_Y = 1?  Or Jan and Feb?

That is, if the whole table had just one row, with Jan_Y = 1 and Feb_Y = 0, would the "avg" be 1.0 or 0.5?
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 39967377
For example say the Jan_Total is 89.75 and Feb_Total is 79.4. I want a column that shows 84.57. When finished I need the columns to look like this:

Jan_Y            Jan_N         Jan_NA     Jan_Total            Feb_Y           Feb_N          Feb_NA  Feb_Total             2MonthAvg (which is the averaging of Jan_total _ Feb_Total)
0
 
LVL 41

Expert Comment

by:Sharath
ID: 39968296
It is better if you can provide some sample data and what exactly you want as result?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

733 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