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
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#
可以使用条件聚合: