Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. part_id | part_name
  2.  
  3. post_id | part_id | type | title | content
  4.  
  5. part_id | part_name | count_posts_type1 | count_posts_type2 | count_posts_type3
  6.  
  7. select
  8. pa.part_id, pa.part_name,
  9. coalesce(sum(case when po.type = 1 then 1 else 0 end), 0) as count_posts_type1,
  10. coalesce(sum(case when po.type = 2 then 1 else 0 end), 0) as count_posts_type2,
  11. coalesce(sum(case when po.type = 3 then 1 else 0 end), 0) as count_posts_type3
  12. from parts pa
  13. left join posts po on po.part_id = pa.part_id
  14. group by pa.part_id, pa.part_name
  15.  
  16. SELECT
  17. parts.part_id,
  18. parts.part_name,
  19. COUNT(t1.post_id) AS count_posts_type1,
  20. COUNT(t2.post_id) AS count_posts_type2,
  21. COUNT(t3.post_id) AS count_posts_type3
  22. FROM posts
  23. RIGHT JOIN part ON posts.part_id=parts.part_id
  24. INNER JOIN ( SELECT post_id FROM posts WHERE type=1 ) as t1(post_id) ON posts.post_id = t1.post_id
  25. INNER JOIN ( SELECT post_id FROM posts WHERE type=1 ) as t2(post_id) ON posts.post_id = t2.post_id
  26. INNER JOIN ( SELECT post_id FROM posts WHERE type=1 ) as t3(post_id) ON posts.post_id = t3.post_id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement