Jump to content

[SOLVED] AVG one column, only if DISTINCT in another.


Recommended Posts

I have a site where people vote on art.

Im currently using this.

SELECT AVG(vote_score) As Rating FROM vote WHERE vote_print= 212

 

But there are a bunch of people signing up over and over again to push their score higher.

I keep track of all of the ip address the votes came from.  So I was thinking of using DISTINCT, but Im not sure how that would be structured.

 

Im looking for the proper way of this

SELECT AVG(vote_score) As Rating FROM vote WHERE vote_print= 212

But only if DISTINCT (vote_ip)

 

Thanks

That was my first thought and try, but I get this back

 

#1140 - Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause

 

 

Something like this:

 

SELECT DISTINCT(vote_ip), AVG(vote_score) As Rating FROM vote WHERE vote_print= 212

That was my second try.  :)

It works in a way, but it lists all the IP address as groups, and has the averages per address.

 

 

 

 

SELECT DISTINCT(vote_ip), AVG(vote_score) As Rating FROM vote WHERE vote_print= 212 GROUP BY vote_ip

Man... We need to hang out.

That was my #3 try (but that is where I stopped).

 

For what ever reason, it does not only select the distinct IPs. It gives me the same rating as

 

SELECT AVG(vote_score) As Rating FROM vote WHERE vote_print= 212

 

Even though one IP has rated this one 33 times.

 

How about this:

 

SELECT DISTINCT(vote_ip), AVG(vote_score) As Rating FROM vote WHERE vote_print= 212 GROUP BY vote_print

No... same thing.

And if I change AVG to COUNT just to make sure, it brings up 117. It should only be 53.

 

 

Maybe removing the parenthesis around the distinct value will give you something you want...

 

SELECT DISTINCT vote_ip, AVG(vote_score) As Rating FROM vote WHERE vote_print= 212 GROUP BY vote_print

Nice!!!!

That worked.

 

Any chance doing the same but selecting all prints ?

So I can list them by rating.

 

 

Right now It works with this, but does not neglect the repeat customers.  ::)

 

SELECT print_id, AVG(vote_score) As rating FROM vote, print WHERE vote_print=print_id GROUP BY vote_print

 

try

SELECT AVG(x.vote_score) as rating FROM
(
   SELECT vote_ip, vote_score 
   FROM vote 
   WHERE vote_print= 212
   GROUP BY vote_ip
) as x

 

(untested)

This didnt work... but I think im on the right track.

 

SELECT x.print_id, AVG(x.vote_score) as Rating FROM 
(
SELECT print_id, vote_ip, vote_score 
FROM vote, print 
WHERE vote_print= print_id GROUP BY vote_ip
) as x
GROUP BY print_id

 

if this doesn't work I'll have to resort to setting up a test table, but perhaps

 

SELECT x. vote_print, AVG(x.vote_score) as Rating FROM

(

  SELECT vote_print, vote_ip, vote_score

  FROM vote

  GROUP BY vote_ip

) as x

GROUP BY x. vote_print

I looks like it works, but all the scores are even numbers, and limited to 173 entries (should be like 280)

 

I think this might not actually be possible.

There is no way to group by anything without losing information.

 

If I group by IP, then all the voyes the user has done on other prints is lost.

By print, I lose all the votes but one.

 

 

Do I call this solved?

 

if this doesn't work I'll have to resort to setting up a test table, but perhaps

 

SELECT x. vote_print, AVG(x.vote_score) as Rating FROM

(

  SELECT vote_print, vote_ip, vote_score

  FROM vote

  GROUP BY vote_ip

) as x

GROUP BY x. vote_print

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.