RecipeDan
asked on
Average columns togather
Hello:
I am trying to average two columns together. When I average them it gets adds the columns together instead of averaging it.
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
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It is still adding the columns
create view test as
SELECT Question,
JAN_Y,
JAN_N,
JAN_NA,
CONVERT(numeric(9,2),ISNUL L(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),ISNUL L(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;
SELECT Question,
JAN_Y,
JAN_N,
JAN_NA,
CONVERT(numeric(9,2),ISNUL
FEB_Y,
FEB_N,
FEB_NA,
CONVERT(numeric(9,2),ISNUL
FROM DATA WITH (NOLOCK);
GO
select Question, AVG(JAN_TOTAL + FEB_TOTAL) as AvgTotal
from test
GROUP BY Question;
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?
ISNULL((JAN_Y)/NULLIF((JAN
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?
ASKER
The average of columns Jan_Total and Feb_Total
ASKER
I am looking for something like this:
Avg(Jan_Total + Feb_Total) AS TwoMonthAvg
Avg(Jan_Total + Feb_Total) AS TwoMonthAvg
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?
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?
ASKER
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)
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)
It is better if you can provide some sample data and what exactly you want as result?
http://technet.microsoft.com/en-us/library/ms177677(v=sql.105).aspx