商业智能(BI)实战项目
期待您的关注
☀大数据学习笔记
1.实现的功能
2.数据库操作步骤
创建数据库:create database card;创建表:create table card_apply
(
cid bigint primary key auto_increment
,apply_uid bigint
,apply_entrance varchar(90)
,apply_dt varchar(90)
,access_state varchar(90)
,issuccess varchar(90)
,source varchar(90)
,isact varchar(10)
,isfirst varchar(10)
);
环比:这个月减去上个月的和上个月的比
同比:今年本月减去去年同期和去年同月的比
插入数据:insert into card_apply values
(null,1,'广告banner','2024-06-04','点击','不成功','直销','否','否')
,(null,2,'首页con','2024-06-04','点击','不成功','直销','否','否')
,(null,3,'交叉销售广告位','2024-06-04','点击','不成功','直销','否','否')
,(null,4,'信用卡专区','2024-05-02','点击','不成功','直销','否','否')
,(null,5,'交叉销售广告位','2024-05-03','点击','不成功','直销','否','否')
,(null,6,'广告banner','2024-05-03','点击','不成功','直销','否','否')
,(null,7,'广告banner','2023-06-03','点击','成功','直销','是','是')
,(null,8,'交叉销售广告位','2023-06-03','点击','成功','直销','是','是');select * from card_apply;
3.计算本月信用卡申请人数及同比环比
打开Tableau 连接MySQL 双击新自定义SQL 写入SQL语句
select
a.user_cnt curr_month_user_cnt,
(a.user_cnt-b.user_cnt)/b.user_cnt upper_month_user_cnt,
(a.user_cnt-c.user_cnt)/c.user_cnt last_year_user_cnt
from
(
select count(distinct(apply_uid)) user_cnt from card_apply where date_format(apply_dt,'%Y-%m')=date_format(current_date(),'%Y-%m')
)a
,(
select count(distinct(apply_uid)) user_cnt from card_apply where date_format(apply_dt,'%Y-%m')=date_format(date_sub(current_date(), interval 1 month),'%Y-%m')
)b
,(
select count(distinct(apply_uid)) user_cnt from card_apply where date_format(apply_dt,'%Y-%m')=date_format(date_sub(current_date(), interval 12 month),'%Y-%m')
)c
MySQL日期函数
然后跳转到工作表
4.计算本月信用卡核卡成功人数及同比环比
为了方便计算 我们再插入几条数据:insert into card_apply values
(null,9,'交叉销售广告位','2024-05-03','点击','成功','直销','是','是'),
(null,10,'信用卡专区','2024-06-03','点击','成功','直销','是','是'),
(null,11,'首页con','2024-05-08','点击','成功','直销','是','是');
打开Tableau 连接MySQL 双击新自定义SQL 写入SQL语句
SELECT
a.success_uid current_success_uid
,(a.success_uid-b.success_uid)/b.success_uid last_month_success_uid
,(a.success_uid-c.success_uid)/c.success_uid last_year_success_uid
FROM
(
select count(distinct(apply_uid)) success_uid from card_apply where issuccess = '成功' and date_format(apply_dt,'%Y-%M') = date_format(current_date(),'%Y-%M')
)a
,(
select count(distinct(apply_uid)) success_uid from card_apply where issuccess = '成功' and date_format(apply_dt,'%Y-%M') = date_format(date_sub(current_date(),interval 1 month),'%Y-%M')
)b
,(
select count(distinct(apply_uid)) success_uid from card_apply where issuccess = '成功' and date_format(apply_dt,'%Y-%M') = date_format(date_sub(current_date(),interval 12 month),'%Y-%M')
)c
最终结果
5.计算本月信用卡激活人数及同比环比
为了方便计算 我们再插入几条数据:insert into card_apply values
(null,12,'交叉销售广告位','2024-06-23','点击','成功','直销','是','是'),
(null,13,'信用卡专区','2024-06-18','点击','成功','直销','是','是'),
(null,14,'首页con','2024-05-08','点击','不成功','直销','是','是');
打开Tableau 连接MySQL 双击新自定义SQL 写入SQL语句
SELECT
a.act_uid current_act_uid
,(a.act_uid-b.act_uid)/b.act_uid last_month_act_uid
,(a.act_uid-c.act_uid)/c.act_uid last_year_act_uid
FROM
(
select count(distinct(apply_uid)) act_uid from card_apply where isact = '是' and date_format(apply_dt,'%Y-%M') = date_format(current_date(),'%Y-%M')
)a
,(
select count(distinct(apply_uid)) act_uid from card_apply where isact = '是' and date_format(apply_dt,'%Y-%M') = date_format(date_sub(current_date(),interval 1 month),'%Y-%M')
)b
,(
select count(distinct(apply_uid)) act_uid from card_apply where isact = '是' and date_format(apply_dt,'%Y-%M') = date_format(date_sub(current_date(),interval 12 month),'%Y-%M')
)c
最终结果
6.计算本月信用卡首刷人数及同比环比
为了方便计算 我们再插入几条数据:insert into card_apply values
(null,15,'交叉销售广告位','2024-06-26','点击','成功','直销','是','否'),
(null,16,'信用卡专区','2024-06-11','点击','成功','直销','是','是'),
(null,17,'首页con','2023-05-08','点击','不成功','直销','是','是');
打开Tableau 连接MySQL 双击新自定义SQL 写入SQL语句
SELECT
a.first_uid current_first_uid
,(a.first_uid-b.first_uid)/b.first_uid last_month_first_uid
,(a.first_uid-c.first_uid)/c.first_uid last_year_first_uid
FROM
(
select count(distinct(apply_uid)) first_uid from card_apply where isfirst = '是' and date_format(apply_dt,'%Y-%M') = date_format(current_date(),'%Y-%M')
)a
,(
select count(distinct(apply_uid)) first_uid from card_apply where isfirst = '是' and date_format(apply_dt,'%Y-%M') = date_format(date_sub(current_date(),interval 1 month),'%Y-%M')
)b
,(
select count(distinct(apply_uid)) first_uid from card_apply where isfirst = '是' and date_format(apply_dt,'%Y-%M') = date_format(date_sub(current_date(),interval 12 month),'%Y-%M')
)c
最终结果
7.求本月信用卡来源
为了方便计算 我们再插入几条数据:insert into card_apply values
(null,15,'交叉销售广告位','2024-06-29','点击','成功','网络','是','是'),
(null,16,'信用卡专区','2024-06-18','点击','成功','网络','是','是'),
(null,17,'首页con','2024-06-06','点击','不成功','网络','是','是'),
(null,18,'交叉销售广告位','2024-06-21','点击','成功','分行','是','是'),
(null,19,'信用卡专区','2024-06-30','点击','成功','分行','是','是'),
(null,20,'首页con','2024-06-09','点击','不成功','分行','是','是'),
(null,21,'信用卡专区','2024-06-30','点击','成功','网络外部平台','是','是'),
(null,22,'首页con','2024-06-09','点击','不成功','网络外部平台','是','是'),
(null,23,'信用卡专区','2024-06-30','点击','成功','网络外部平台','是','是'),
(null,24,'首页con','2024-06-09','点击','不成功','网络外部平台','是','是');
SELECT
count(*)
,source
FROM card_apply where date_format(apply_dt,'%Y-%M')=date_format(current_date(),'%Y-%M')
group by source;
8.求本月信用卡核卡成功用户来源
SELECT
COUNT(*),source
FROM card_apply
WHERE DATE_FORMAT(apply_dt,'%Y-%M')=DATE_FORMAT(CURRENT_DATE(),'%Y-%M') and issuccess = '成功'
GROUP BY source
9.求本月APP信用卡申请入口分布
SELECT
COUNT(*),apply_entrance
FROM card_apply
WHERE DATE_FORMAT(apply_dt,'%Y-%M')=DATE_FORMAT(CURRENT_DATE(),'%Y-%M')
GROUP BY apply_entrance
10.求本月APP信用卡申请和提交用户数
为了方便计算 我们再插入几条数据:insert into card_apply values
(null,25,'交叉销售广告位','2024-06-26','提交','成功','直销','是','否'),
(null,26,'信用卡专区','2024-06-11','提交','成功','直销','是','是'),
(null,27,'首页con','2023-05-08','提交','不成功','直销','是','是'),
(null,28,'交叉销售广告位','2024-06-25','提交','成功','直销','是','否'),
(null,29,'信用卡专区','2024-06-25','提交','成功','直销','是','是'),
(null,30,'首页con','2023-06-09','提交','不成功','直销','是','是'),
(null,31,'交叉销售广告位','2024-06-25','提交','成功','直销','是','否'),
(null,32,'信用卡专区','2024-06-24','提交','成功','直销','是','是'),
(null,33,'首页con','2023-06-09','提交','不成功','直销','是','是'),
(null,34,'交叉销售广告位','2024-06-18','提交','成功','直销','是','否'),
(null,35,'信用卡专区','2024-06-19','提交','成功','直销','是','是'),
(null,36,'首页con','2023-06-09','提交','不成功','直销','是','是'),
(null,37,'首页con','2023-06-21','提交','不成功','直销','是','是'),
(null,38,'首页con','2023-06-22','提交','不成功','直销','是','是'),
(null,39,'首页con','2023-06-23','提交','不成功','直销','是','是'),
(null,40,'首页con','2023-06-24','提交','不成功','直销','是','是'),
(null,41,'首页con','2023-06-20','提交','不成功','直销','是','是')
SELECT
access_state
,apply_dt
,count(distinct(apply_uid)) user_cnt
FROM card_apply
WHERE DATE_FORMAT(apply_dt,'%Y-%M')=DATE_FORMAT(CURRENT_DATE(),'%Y-%M')
and (access_state = '点击' or access_state = '提交')
group by apply_dt,access_state
由于数据量较小 所以曲线不是那么圆顺 如果我们想要让它变得更丝滑 那么我们可以添加大量数据
11.汽车服务用户分析的设计与实现
create database app;use app;create table app_log
(
log_id bigint primary key auto_increment
,access_dt varchar(90)
,user_id bigint
,browse_page varchar(10)
,app_status varchar(90)
,operator varchar(30)
,net_type varchar(20)
);
插入数据:insert into app_log values
(null,'2024-06-01',1,'A','异常','移动','4G'),
(null,'2024-06-02',2,'B','异常','移动','4G'),
(null,'2024-06-01',2,'C','异常','移动','4G'),
(null,'2024-06-02',3,'D','异常','移动','4G'),
(null,'2024-06-02',4,'D','异常','移动','4G')
1)求每日新增用户数
那么 该如何求新增用户数呢 (使用连接查询)
select
'2024-06-02' dt
,count(distinct(a.user_id)) num
from
(
select user_id from app_log where date_format(access_dt,'%Y-%m-%d') = '2024-06-02'
)a
left join
(
select user_id from app_log where date_format(access_dt,'%Y-%m-%d') < '2024-06-02'
)b
on a.user_id = b.user_id WHERE b.user_id is null
2)求新增用户7日留存率
日期加减:date_sub(current_date(),interval 8 day)
select
c.dt,
c.user_cnt/e.user_cnt remain_ration
from
(
select
date_sub('2024-06-02',interval 7 day) dt,
count(distinct(a.user_id)) user_cnt
from
(
select user_id from app_log where date_format(access_dt,'%Y-%m-%d') = '2024-06-02'
)a
inner join
(
select user_id from app_log where date_format(access_dt,'%Y-%m-%d') = date_sub('2024-06-02',interval 8 day)
)b
on a.user_id = b.user_id
)c
,
(
select count(distinct(user_id)) user_cnt
from
(
select user_id from app_log where date_format(access_dt,'%Y-%m-%d') = date_sub('2024-06-02',interval 8 day)
)b
)e
3)求每周新增用户数
日期转周:week(current_date())
select
count(distinct(a.user_id)) user_cnt
from
(
select
distinct(user_id) user_id
from app_log where week(access_dt) = 23
)a
left join
(
select
distinct(user_id) user_id
from app_log where week(access_dt) = 22
)b
on a.user_id = b.user_id where b.user_id is null
4)求新增用户八周留存
select
d.week,
d.user_cnt/e.user_cnt remain_ration
from
(
select
week(date_add(current_date(),interval -9 week)) week,
count(distinct(a.user_id)) user_cnt
from
(
select user_id from app_log where week(access_dt)=week(current_date())
)a
inner join
(
select user_id from app_log where week(access_dt)=week(date_add(current_date(),interval -9 week))
)b on a.user_id = b.user_id
)d,
(
select count(distinct(b.user_id)) user_cnt
from
(
select user_id from app_log where week(access_dt)=week(date_add(current_date(),interval -9 week))
)b
)e
5)求30日活跃用户
select
count(distinct(user_id)) user_cnt
from app_log
where access_dt between date_add(current_date(),interval -30 day) and current_date()