| Author |
Topic |
|
sergeant_time
Yak Posting Veteran
73 Posts |
Posted - 2010-05-26 : 10:51:58
|
| I am trying to find a code for AVG excluding a number. I would like to get the an average of a column excluding all nines. |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-05-26 : 10:54:20
|
Do you mean a column with only the digit 9 as value?Use NULLIF() because AVG is ignoring NULL values:selectavg(nullif(your_column,9)) as avgColfrom your_table No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-05-26 : 11:07:38
|
erm. yeah I guess. OrSELECT AVG([<Your_Column>]) FROM your_tableWHERE [<your_column>] <> 9 Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-05-26 : 11:13:51
|
Yes that seems pretty simple but the OP wrote find a code for AVG excluding a number...not the query  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
sergeant_time
Yak Posting Veteran
73 Posts |
Posted - 2010-05-26 : 11:38:50
|
| webfred and Transact Charlie, Error Message: Operand data type char is invalid fro avg operator. |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-05-26 : 11:41:27
|
quote: Originally posted by sergeant_time webfred and Transact Charlie, Error Message: Operand data type char is invalid fro avg operator.
Isn't the error self explanatory? You are trying to get an average of a character field. If it contains just numbers, convert it to an integer.Read up on CAST() and CONVERT() to do the conversion. |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-05-26 : 11:42:19
|
| Can you show us the data_types of the table?Maybe post some sample data as well -- the column is obviously not a numeric type.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
sergeant_time
Yak Posting Veteran
73 Posts |
Posted - 2010-05-26 : 11:56:48
|
| SELECT QuestionOne AS Q1, QuestionTwo AS Q2, QuestionThree AS Q3, QuestionFour AS Q4, SurveyDate AS [Survey Date], SplitFROM hrc_survey_resultsWHERE (SurveyDate >= @Startdate) AND (SurveyDate < DATEADD(d, 1, @Enddate)) AND (Split IN (@PILOTNAME)) AND (Split IN (8, 28, 19, 9, 43)) AND (Split IS NOT NULL)GROUP BY QuestionOne, QuestionTwo, QuestionThree, QuestionFour, SurveyDate, SplitORDER BY [Survey Date] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-05-27 : 03:37:39
|
quote: Originally posted by sergeant_time SELECT QuestionOne AS Q1, QuestionTwo AS Q2, QuestionThree AS Q3, QuestionFour AS Q4, SurveyDate AS [Survey Date], SplitFROM hrc_survey_resultsWHERE (SurveyDate >= @Startdate) AND (SurveyDate < DATEADD(d, 1, @Enddate)) AND (Split IN (@PILOTNAME)) AND (Split IN (8, 28, 19, 9, 43)) AND (Split IS NOT NULL)GROUP BY QuestionOne, QuestionTwo, QuestionThree, QuestionFour, SurveyDate, SplitORDER BY [Survey Date]
Is this related to this topic?MadhivananFailing to plan is Planning to fail |
 |
|
|
|