Jump to content

[SOLVED] GROUP BY or DISTINCT?


l3asturd

Recommended Posts

ok, I don't have any code yet, my module is a concept until I can figure out if it's possible. So here's what I have:

 

A table that has user names, golf courses, and scores at each course. There are multiple entries for each course but different scores. OK here's an example table:

 

    Name        Score      Course

-------------------------------------

John              50          Visalia

John              49          Visalia

Rick              48          Visalia

John              52          Tulare

Rick              77          Hanford

Rick              50          Tulare

 

 

OK, now, on my stats page, I wanted users to be able to see their score averages for each course. I know how to pull the data from SQL and GROUP BY course. I know how to AVG(Score) AS TotalAverage.  Problem is, how can I average scores for each course AND for each player at the same time.

 

John's average score at Visalia is 50

John's average score at Tulare is 52

 

See what I'm getting at. I tried something like:

 

SELECT * from TABLE WHERE name=John GROUP BY course AVG(score) AS courseaverage

 

but this is bad syntax, and wouldn't result in the data I'm looking for. Any ideas?

 

 

Link to comment
Share on other sites

  • 3 months later...

You almost never want distinct...

Why do you almost never want distinct? I'm trying to find out which is best (performance-wise) - group by or distinct. seems to be a big debate on this with most people saying they are both identical. your comment suggests you think there is a difference.

i.e.

SELECT DISTINCT id FROM phrase WHERE campaign LIKE 'proper%'

versus

SELECT id FROM phrase WHERE campaign LIKE 'proper%' GROUP BY advert_id

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.