Skip to content

Instantly share code, notes, and snippets.

@chartjes
Created November 16, 2011 16:49
Show Gist options
  • Save chartjes/1370612 to your computer and use it in GitHub Desktop.
Save chartjes/1370612 to your computer and use it in GitHub Desktop.
More Aggregate Fun
More fun with aggregates. I have the following query:
SELECT brand.id, COUNT(DISTINCT(track_play.track_id)) as track_count
FROM track_play
INNER JOIN brand ON brand.id = track_play.brand_id
GROUP BY brand.id
which returns info like this:
"id","track_count"
1,1
2,13
3,631
4,15
9,3
11,12
13,12
14,4
20,9
22,13
24,12
26,9
29,2
34,5
35,4
39,7
40,13
43,1
44,3
48,11
54,14
I need to figure out the average track_count per brand_id. Not sure if it requires a nested sub query, and I've tried using AVG(...) but I keep getting "Invalid use of group function"
Can you tell I'm too used to ORM's spitting data out for me? :P
@RemiWoler
Copy link

Yeah, if you don't use any values from the brand table, the join is redundant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment