电商数据实例
原始数据中时间相关字段已时间戳格式进行存储时,需要操作如下:
1.添加新字段用来存储时间格式字段。
alter table [表名] add [字段名] [数据类型];
2.时间戳转化成时间格式,更新新字段数据。
update [表名] set [新字段] = from_unixtime([要转换字段]);
不同时段的累计销售额(聚合函数进行开窗)
select
hour(addtime_new) as 时段,
sum(OrderAmount) as 销售额,
sum(sum(OrderAmount)) over(order by hour(addtime_new)) as 累计销售额
from orderinfo
group by hour(addtime_new);
-- 哪种支付方式可能导致用户支付不成功而取消订单 (利用逻辑判断来计算)
select
PayTool,
sum(OrderState=3 and PayState=0) as 未支付而取消的订单数,
count(OrderState=3 and PayState=0) as 订单数,
avg(OrderState=3 and PayState=0) as 未支付而取消的订单占比
from orderinfo
group by PayTool;
-- 不同品牌的总销量(判断按哪个表字段进行分组)
品牌数量不一致,按品牌主表进行分组
select count(SupplierID) from goodsbrand;-- 64
select count(goodsid) from goodsinfo;-- 10000
select count(distinct typeid) from goodsinfo;-- 69
select typeid,brandtype,sum(amount) 总销量
from orderdetail
left join goodsinfo on orderdetail.goodsid=goodsinfo.goodsid
left join goodsbrand on typeid=SupplierID
group by typeid;
SQL计算复购率
https://blog.csdn.net/kejiayuan0806/article/details/106410139








暂无数据