?
Solved

I need an sql statement

Posted on 2008-10-10
2
Medium Priority
?
203 Views
Last Modified: 2012-05-05
I have a table that contains several fields. I am going to use only three of the aggregate fields here. My SQL statement is:

select user_id, position_id, avg(g), avg(a), avg(p) from 1_f_pg_mod where user_id = 6 group by position_id;

This gives:
user_id      position_id                avg(g)                      avg(a)                        avg(p)
6                    2                  20.348837209302        28.720930232558        49.06976744186
6                    1               17.640845070423                31.514084507042        49.154929577465
6                    3                 20.919540229885        27.931034482759        48.850574712644

What I want to achieve is to get the average of all the positions joined up and call it say 4. The results would look something like (obviously the numbers are not right):
user_id      position_id                          avg(g)                                 avg(a)                 avg(p)
6      (Joined position 1,2 and 3=4)      20.348837209302      28.720930232558      49.06976744186

This is a touch difficult to explain but hope you get the drift.

Thank you.

0
Comment
Question by:theseowgroup
[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
2 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 2000 total points
ID: 22686602
Then you need to eliminate Position_ID from both your SELECT list and your GROUP BY:

select user_id,  avg(g), avg(a), avg(p) from 1_f_pg_mod where user_id = 6 group by user_id;


Perhaps you want both what you currently have and a summary row ...

select user_id, position_id, avg(g), avg(a), avg(p) from 1_f_pg_mod where user_id = 6 group by User_ID, position_id
UNION
select user_id, -1 as Position_ID  avg(g), avg(a), avg(p) from 1_f_pg_mod where user_id = 6 group by user_id, -1;

0
 

Author Closing Comment

by:theseowgroup
ID: 31504993
Thanks Daniel, I think you have got it!  So simpl,e when you know what you are doing!
0

Featured Post

Basic Security of Your VPC

So, you’ve got this shiny new VPC and a fancy new application configured on your EC2 servers ready to go. This application is only accessible from your computer, which is great for security, but you need your users to be able to access it! So, what’s the easiest way to do this?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 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