Solved

I need an sql statement

Posted on 2008-10-10
2
197 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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