Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. table a:
  2. id, score
  3.  
  4. table ab:
  5. a_id, b_id, b_type
  6.  
  7. table b:
  8. id, name, type
  9.  
  10. name, count(group by name), average(score)
  11.  
  12. select name, count(*), average(score)
  13. from a
  14. joins ab
  15. on a.id = ab.a_id
  16. joins b
  17. on b.id = ab.b_id
  18. where type = 'sometype'
  19. group by name
  20. order by count(*) desc limit 10
  21.  
  22. select filtered_ab.b_id, count(1) as ct
  23. from (
  24. select b_id
  25. from ab
  26. where b_type = 'sometype'
  27. ) as filtered_ab
  28. group by filtered_ab.b_id
  29. order by ct desc limit 1000
  30.  
  31. select name, b_id, ct
  32. from b
  33. join (
  34. select filtered_ab.b_id, count(1) as ct
  35. from (
  36. select b_id
  37. from ab
  38. where b_type = 'sometype'
  39. ) as filtered_ab
  40. group by filtered_ab.b_id
  41. order by ct desc limit 1000
  42. ) as b_with_counts
  43. on b_with_counts.b_id = b.id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement