Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

Help with AVG

Hi,

How can I get the average/sum of a column in this query.  The query has about 30 records in it, here is the SQL I'm using to get the data out.

SELECT q.SurveyID, q.questionID, q.QuestionScore, q.QuestionName
FROM Questions q


surveyID | questionID | QuestionScore | QuestionName

28323          2                     45                       Do you like food?
34852          2                     88                       Do you like food?
38052          2                     14                       Do you like food?
28323          55                   15                       Do you like milk?
34852          55                   28                       Do you like milk?
38052          55                   48                       Do you like milk?


I want to return one row  with an average score here for each questionID.  So I'm trying to get just two rows back.

How can I modify my query to do this?

Thanks...
0
Westside2004
Asked:
Westside2004
1 Solution
 
jogosCommented:
Syntax at http://msdn.microsoft.com/en-us/library/ms177677.aspx
SELECT D, q.questionID, AVG(q.QuestionScore)
FROM Questions q
group by  q.questionID

Open in new window


With name
SELECT D, q.questionID, AVG(q.QuestionScore), q.QuestionName
FROM Questions q
group by  q.questionID, q.QuestionName
--or 
SELECT D, q.questionID, AVG(q.QuestionScore), max(q.QuestionName)
FROM Questions q
group by  q.questionID

Open in new window

0
 
Éric MoreauSenior .Net ConsultantCommented:
I think jogos forgot to remove D from the SELECT list. it should be:

SELECT q.questionID, AVG(q.QuestionScore)
FROM Questions q
group by  q.questionID
0
 
keyuCommented:
It will return just 2 rows

SELECT questionID,QuestionName, [28323], [34852] ,[38052]
FROM (select questionID,SurveyID,QuestionScore,QuestionName
from Questions) as sourcetable
pivot
(
avg(q.QuestionScore) for SurveyID in (28323,34852,38052)
) as pvt
orderby pvt.questionID
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jogosCommented:
<<It will return just 2 rows>>
That is conform the question
<<I want to return one row  with an average score here for each questionID.  So I'm trying to get just two rows back.>>
0
 
jogosCommented:
Did you already tried the suggested comments?

SELECT  q.questionID, AVG(q.QuestionScore)
FROM Questions q
group by  q.questionID

Open in new window

                                   
With name

SELECT  q.questionID, AVG(q.QuestionScore), q.QuestionName
FROM Questions q
group by  q.questionID, q.QuestionName
--or 
SELECT  q.questionID, AVG(q.QuestionScore), max(q.QuestionName)
FROM Questions q
group by  q.questionID

Open in new window

0
 
ValentinoVCommented:
Westside2004: what jogos posted is exactly what you've asked for.  Please post follow-up comments if you're looking for something else or have some sort of issue with the answer.
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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now