首页 > 试题广场 >

有如下场景,有一张品牌专场表(brand),商品表(good

[问答题]
有如下场景,有一张品牌专场表(brand),商品表(goods),订单商品表表(order_goods),具体表结构如下:

品牌专场表(brand)

Id

专场id

Bigint  

name

专场名称

Varchar(100)

Sale_time_start    

销售开始时间       

timestamp       

Sale_time_end

销售结束时间

timestamp

 

  商品表(Goods)

 

Id

商品id

bigint

Brand_id    

专场id(商品属于某个专场)    

Bigint

Name

商品名称

Varchar(100)   

Price

售卖价格

dboule

 

订单商品表(Order_goods)

id

自增id  

Bigint    

Order_id

订单id

Bigint

User_id

下单用户id     

Bigint

Goods_id

商品id

Bigint

Goods_num     

商品数量      

Int

 

请用sql统计在2017-03-15这一天内,专场A下的商品B的订单数、用户数、销售数量、销售额。
题目解析没有涉及时间的判断
发表于 2022-08-04 18:42:42 回复(0)

Select count(distinct Order_id) as 订单数,

count(distinct User_id) as 用户数,

sum(Goods_num)as销售数量,

sum(goods_num * price)as销售额

from brand a

Left join goods b on a.id = b.brand_id

Left join order_goods c on b.id = c.goods_id

Where a.nme = ‘A’ and b.name = ‘B’and a. Sale_time_start =” 2017-03-15” and a. Sale_time_end=” 2017-03-16”

发表于 2019-08-26 16:43:00 回复(2)
select count(o.Order_id) as order_num,count(o.User_id) as user_num,sum(o.Goods_num) as  sell_num,sum(o.Goods_num)*BG.Price as sell_money
from Order_goods as o left join
(select g.Id,g.Price
from brand as b left join Goods as g on b.Id = g.Brand_id
where b.Sale_time_start = "2017-03-15" and b.Id = "A" ) as BG
on o.Goods_id = BG.Id
groupby o.Goods_id having o.Goods_id = "B";





编辑于 2019-08-23 11:28:53 回复(0)