创建此聚合sql查询需要帮助吗

wpx232ag  于 2021-08-13  发布在  Java
关注(0)|答案(2)|浏览(566)

关闭。这个问题需要更加突出重点。它目前不接受答案。
**想改进这个问题吗?**通过编辑这篇文章更新这个问题,使它只关注一个问题。

11个月前关门了。
改进这个问题

是否可以创建将返回此结果集的sql查询?
谢谢你的建议和反馈。

dm7nw8vv

dm7nw8vv1#

您可以加入并执行条件聚合:

  1. select
  2. b.id,
  3. b.name
  4. max(case when tib.content = 'math book' then 'Yes' else 'No' end) has_math,
  5. max(case when tib.content = 'SQL book' then 'Yes' else 'No' end) has_sql,
  6. max(case when tib.content = 'comic book' then 'Yes' else 'No' end) has_comic
  7. from bag b
  8. inner join things_in_bag tib on tib.bag_id = b.id
  9. group by b.id, b.name
cetgtptt

cetgtptt2#

可以使用条件聚合:

  1. select bag_id,
  2. max(case when content = 'math book' then 'Yes' else 'No' end) as has_math_book,
  3. max(case when content = 'SQL book' then 'Yes' else 'No' end) as has_sql_book,
  4. max(case when content = 'comic book' then 'Yes' else 'No' end) as has_comic_book
  5. from things_in_bag
  6. group by bag_id;

相关问题