Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


I need an sql statement

Posted on 2008-10-10
Medium Priority
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.

Question by:theseowgroup
LVL 32

Accepted Solution

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
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;


Author Closing Comment

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…

580 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