Solved

I need an sql statement

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

20 Experts available now in Live!

Get 1:1 Help Now