select a.book_id, a.book_title, sum(case when date_format(b.borrow_date,'%Y%m')='202302' then 1 else 0 end) as feb_2023_borrows, sum(case when date_format(b.borrow_date,'%Y%m')='202402' then 1 else 0 end) as feb_2024_borrows, sum(case when date_format(b.borrow_date,'%Y%m')='202401' then 1 else 0 end) as jan_2024_borrows, sum(case when date_format(b.borrow_date,'%Y%m')='202402' then 1 else 0 end) - sum(case when date_format(b.borrow_date,'%Y%m')='202302' then 1 else 0 end) as yoy_delta, sum(case when date_format(b.borrow_date,'%Y%m')='202402' then 1 else 0 end) - sum(case when date_format(b.borrow_date,'%Y%m')='202401' then 1 else 0 end) as mom_delta, ifnull(     round(sum(case when c.region = '华北' and year(b.borrow_date)='2023' then 1 else 0 end)/sum(case when year(b.borrow_date)='2023' then 1 else 0 end)*100,2),0.00 ) as north_pct_2023, ifnull(    round(sum(case when c.region = '华南' and year(b.borrow_date)='2023' then 1 else 0 end)/sum(case when year(b.borrow_date)='2023' then 1 else 0 end)*100,2),0.00 ) as south_pct_2023, ifnull(     round(sum(case when c.region = '华东' and year(b.borrow_date)='2023' then 1 else 0 end)/sum(case when year(b.borrow_date)='2023' then 1 else 0 end)*100,2),0.00 ) as east_pct_2023 from Books a  left join BorrowRecords b on a.book_id = b.book_id left join Branches c on b.branch_id = c.branch_id group by a.book_id , a.book_title order by a.book_id , a.book_title
点赞

相关推荐

03-15 10:59
已编辑
美团_后端开发(实习员工)
爱写代码的菜code...:哎,自己当时拿到字节offer的时候也在感叹终于拿到了,自己当时最想去的企业就是字节,结果还是阴差阳错去了鹅厂。祝uu一切顺利!!!
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务