Solved

I need an sql statement

Posted on 2008-10-10
2
194 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
2 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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