Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- table a:
- id, score
- table ab:
- a_id, b_id, b_type
- table b:
- id, name, type
- name, count(group by name), average(score)
- select name, count(*), average(score)
- from a
- joins ab
- on a.id = ab.a_id
- joins b
- on b.id = ab.b_id
- where type = 'sometype'
- group by name
- order by count(*) desc limit 10
- select filtered_ab.b_id, count(1) as ct
- from (
- select b_id
- from ab
- where b_type = 'sometype'
- ) as filtered_ab
- group by filtered_ab.b_id
- order by ct desc limit 1000
- select name, b_id, ct
- from b
- join (
- select filtered_ab.b_id, count(1) as ct
- from (
- select b_id
- from ab
- where b_type = 'sometype'
- ) as filtered_ab
- group by filtered_ab.b_id
- order by ct desc limit 1000
- ) as b_with_counts
- on b_with_counts.b_id = b.id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement