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

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

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

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

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

dm7nw8vv

dm7nw8vv1#

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

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

cetgtptt2#

可以使用条件聚合:

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

相关问题