selectcount(distinct shop_id)as 深圳门店总数
from T_shop
where which_city ='Shenzhen'
列出北京和上海所有门店中的图书名称中含有“福尔摩斯”的图书编号
select a.book_id
from T_book a
innerjoin T_shop b
on a.which_shop = b.shop_id
where b.which_city in('Beijing','Shanghai')and a.book_name like'%福尔摩斯%'
列出各门店的编号、名称、2019年6月的营业额(按租赁金额计算),各门店按营业额从大到小排序。
select b.shop_id,b.shop_name,isnull(sum(price_num*rental_days),0)as 营业额
from T_book a
leftjoin T_shop b
on a.which_shop = b.shop_id
leftjoin T_rental c
on a.book_id = c.which_book andleft(rental_date,7)='2019-06'groupby b.shop_id,b.shop_name
orderbysum(price_num*rental_days)desc
select a.which_book,b.book_name,c.author_name,d.publisher_name,e.shop_name,count(distinct a.rental_id)as 租赁次数
from T_rental a
leftjoin T_book b
on a.which_book = b.book_id
leftjoin T_author c
on b.which_author = c.author_id
leftjoin T_publisher d
on b.which_publisher = d.publisher_id
leftjoin T_shop e
on b.which_shop = e.shop_id
whereYear(a.rental_date)='2019'groupby a.which_book,b.book_name,c.author_name,d.publisher_name,e.shop_name
havingcount(distinct a.rental_id)>1000orderbycount(distinct a.rental_id)desc
select a.shop_id,a.shop_name,isnull(sum(b.cost_num),0)as 采购成本总额
from T_shop a
leftjoin T_book b
on a.shop_id = b.which_shop
where a.which_city ='shanghai'andnotexists(select*from T_rental o where b.book_id = o.which_book )groupby a.shop_id,a.shop_name
orderbysum(b.cost_num)desc