商品销售数据分析实验

进入虚拟机,启动HDFS和Yarn

1.创建表

复制代码
hive
show databases;
use test;

销售订单表

复制代码
create table t_dml (
  detail_id bigint,
  sale_date date,  
  province string,
  city string,
  product_id bigint,
  cnt bigint,
  amt double
)row format delimited
fields terminated by ',';

商品详细表:

复制代码
create table t_product (
    product_id bigint, 
    product_name string, 
    category_id bigint, 
    category_name string, 
    price double
)row format delimited
fields terminated by ',';

2.加载数据

创建目录存放数据

复制代码
mkdir /opt/data

加载数据

复制代码
load data local inpath '/opt/data/t_dml.csv' into table t_dml;
load data local inpath '/opt/data/t_product.csv' into table t_product;

3. 销售数据分析

1.查询t_dml中的销售记录的时间段:

复制代码
select max(sale_date), min(sale_date) from t_dml;

2.查询各商品类别的总销售额

复制代码
select t.category_name, sum(t.amt) as total_money
 from
 ( select a.product_id, a.amt, b.category_name
 from t_dml a
 join t_product b
 on a.product_id=b.product_id
 ) t
group by t.category_name;

3.店主想知道哪个商品最畅销以及销量排行榜,请查询销量前10的商品,显示商品名称,销量,排名。

复制代码
select a.product_name , t.cnt_total,
 rank() over (order by t.cnt_total desc) as rk
 from
 ( select product_id, sum(cnt) as cnt_total
 from t_dml
 group by product_id
 order by cnt_total desc
 limit 10
 ) t
 join t_product a
 on t.product_id=a.product_id;

4.店主想知道各个市县的购买力,同时也想知道自己的哪个商品在该地区最热卖,通过创建中间表,优化查询。

1. 创建结果存放表:

复制代码
create table t_city_amt
( province string,
 city string,
 total_money double
);
create table t_city_prod
( province string,
 city string,
 product_id bigint,
 product_name string,
 cnt bigint
);

2.插入数据

复制代码
insert into t_city_amt
select province,city,sum(amt)
from t_dml group by province,city;

SELECT * FROM t_city_amt;
复制代码
insert into t_city_prod
select t.province,t.city,t.product_id,t.product_name,sum(t.cnt) from
(
select a.product_id,b.product_name,a.cnt,a.province,a.city
from t_dml a join t_product b
on a.product_id = b.product_id
) t
group by t.province,t.city,t.product_id,t.product_name;

SELECT * FROM t_city_prod;

3. 优化

复制代码
from 
( select a.*, b.product_name
 from t_dml a
 join t_product b
 on a.product_id=b.product_id
) t
insert overwrite table t_city_amt
 select province, city, sum(amt)
 group by province, city
insert overwrite table t_city_prod
 select province, city, product_id, product_name, sum(cnt)
 group by province, city, product_id, product_name;

5.统计各省最强购买力地区:

复制代码
select province, city, total_money
 from
 ( 
  select province, city, total_money,
  dense_rank() over (partition by province order by total_money desc) as rk
  from t_city_amt
 ) t
where t.rk=1
order by total_money desc;

6.统计各地区的最畅销商品

复制代码
select province, city, product_id, product_name
 from
 ( select province, city, product_id, product_name,
 dense_rank() over (partition by province order by cnt desc) as rk
 from t_city_prod
 ) t
where t.rk=1
order by province, city;
相关推荐
*星星之火*3 小时前
【大模型评估】大模型评估框架 HELM(Holistic Evaluation of Language Models)全解析:原理、工具与实践
windows·语言模型·数据挖掘
好开心啊没烦恼3 小时前
图数据库:基于历史学科的全球历史知识图谱构建,使用Neo4j图数据库实现中国历史与全球历史的关联查询。
大数据·数据库·python·数据挖掘·数据分析·知识图谱·neo4j
葡萄城技术团队4 小时前
Wyn 商业智能软件:3D 可视化大屏搭建与工具使用全指南
3d·数据分析
rggrgerj4 小时前
VUE3+element plus 实现表格行合并
javascript·vue.js·elementui
huangql5204 小时前
UniApp + Vite + Vue3 + TypeScript 项目中 ESLint 与 Prettier 的完整配置指南
vue.js·typescript·团队开发·代码规范
fxshy4 小时前
Vue3和element plus在el-table中使用el-tree-select遇到的change事件坑
javascript·vue.js·elementui
北慕阳4 小时前
自存19-48
javascript·vue.js·elementui
补三补四4 小时前
卡尔曼滤波
python·算法·机器学习·数据挖掘
B站_计算机毕业设计之家14 小时前
大数据 Python小说数据分析平台 小说网数据爬取分析系统 Django框架 requests爬虫 Echarts图表 17k小说网 (源码)✅
大数据·爬虫·python·机器学习·数据分析·可视化·小说