Solved

Help with AVG

Posted on 2012-03-22
7
339 Views
Last Modified: 2012-08-13
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
Comment
Question by:Westside2004
7 Comments
 
LVL 25

Accepted Solution

by:
jogos earned 500 total points
Comment Utility
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
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
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
 
LVL 9

Expert Comment

by:keyu
Comment Utility
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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 25

Expert Comment

by:jogos
Comment Utility
<<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
 
LVL 25

Expert Comment

by:jogos
Comment Utility
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
 
LVL 37

Expert Comment

by:ValentinoV
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now