关闭。这个问题需要更加突出重点。它目前不接受答案。**想改进这个问题吗?**通过编辑这篇文章更新这个问题,使它只关注一个问题。
11个月前关门了。改进这个问题是否可以创建将返回此结果集的sql查询?谢谢你的建议和反馈。
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_comicfrom bag binner join things_in_bag tib on tib.bag_id = b.idgroup by b.id, b.name
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
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_bookfrom things_in_baggroup by bag_id;
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;
2条答案
按热度按时间dm7nw8vv1#
您可以加入并执行条件聚合:
cetgtptt2#
可以使用条件聚合: