ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

mysql 练习基础 11:行转列的方法

2021-10-16 21:02:10  阅读:187  来源: 互联网

标签:11 count sum 转列 user behavior mysql type id


-- 数据准备
create database taobao;

use taobao;

create table UserBehavior(
    user_id int,
    item_id int,
    item_category int,
    behavior_type varchar(10),
    user_geohash varchar(10),
    times datetime,
    amount decimal(5,2)
);

show variables like '%secure%';-- 查看安全路径,随后将表格粘贴到该安全路径下,
load data infile "C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/UserBehavior.csv"
into table UserBehavior
fields terminated by ','
ignore 1 lines;

select * from UserBehavior limit 10;
select count(*) from UserBehavior;#1048575



-- 数据清洗
-- 缺失值处理
select
count(user_id),
count(item_id),
count(item_category),
count(behavior_type),
count(user_geohash),
count(times),
count(amount)
from UserBehavior;



-- 异常值检查

select min(times),max(times),min(amount),max(amount) from userbehavior;

-- 重复记录处理

select distinct * from userbehavior;

-- 字段处理:根据times字段增加计算字段用户行为日期、周和小时,排除后续分析不需要的user_geohash字段,并将筛选后的结果保存到新表
#保存方式一:直接导出表格csv文件
select 
user_id,
item_id,
item_category,
behavior_type,
date(times) as 日期,
hour(times) as 小时,
date_format(times,'%w') as 星期,
amount
from (select distinct * from userbehavior) as t #从去重的表里面去查询
limit 100;

#保存方式二:代码保存(至安全路径)
select 
user_id,
item_id,
item_category,
behavior_type,
date(times) as 日期,
hour(times) as 小时,
date_format(times,'%w') as 星期,
amount
from (select distinct * from userbehavior) as t  
limit 100
into outfile "C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/t2.csv"-- 保存到安全路径并命名为t2
fields terminated by ','
lines terminated by '\r\n';

#保存方式三:视图 
#视图:优点 1.简化SQL语句,快速提取,将复杂查询语句进行封装;2.安全,保护数据,设置访问权限 让不同级别用户看到不同的视图;缺点 操作视图比直接操作基础表要慢
create view userbehavior_new as 
select 
user_id,
item_id,
item_category,
behavior_type,
date(times) as 日期,
hour(times) as 小时,
date_format(times,'%w') as 星期,
amount
from (select distinct * from userbehavior) as t;

select * from userbehavior_new limit 100;

select count(*) from userbehavior_new;#962097

  
-- 数据预览
select
    count(distinct user_id) as 用户数,
    count(distinct item_id) as 商品数,
    count(distinct item_category) as 类目数
from userbehavior_new;

select 
    behavior_type,
    count(*) as 行为次数
from userbehavior_new
group by behavior_type;



-- 数据分析
-- 1.流量指标分析
-- 每日PV、UV、人均浏览量、成交量、销售额
select 
日期,
sum(behavior_type="pv") as 浏览量,
count(distinct user_id) as 访客数,
sum(behavior_type="pv")/count(distinct user_id) as 人均浏览量,
sum(behavior_type="buy") as 成交量,
sum(if(behavior_type="buy",amount,0)) as 销售额
from userbehavior_new
group by 日期;


-- 周一到周日PV、UV、人均浏览量、成交量、销售额
select 
星期,
sum(behavior_type="pv") as 浏览量,
count(distinct user_id) as 访客数,
sum(behavior_type="pv")/count(distinct user_id) as 人均浏览量,
sum(behavior_type="buy") as 成交量,
sum(if(behavior_type="buy",amount,0)) as 销售额
from userbehavior_new
group by 星期;


-- 每小时PV、UV、人均浏览量、成交量、销售额
select 
小时,
sum(behavior_type="pv") as 浏览量,
count(distinct user_id) as 访客数,
sum(behavior_type="pv")/count(distinct user_id) as 人均浏览量,
sum(behavior_type="buy") as 成交量,
sum(if(behavior_type="buy",amount,0)) as 销售额
from userbehavior_new
group by 小时;



-- 2.行为转化分析(转化率=当前行为用户数/上一行为用户数)
select 
    behavior_type,
    count(distinct user_id) as 用户人数,
    lag(count(distinct user_id),1) over(order by behavior_type desc) as 上一行为用户人数,
    ifnull(count(distinct user_id)/lag(count(distinct user_id),1) over(order by behavior_type desc),1) as 转化率
from userbehavior_new
group by behavior_type;


-- 浏览—加购—购买的转化率
select 
    behavior_type,
    count(distinct user_id) as 用户人数,
    lag(count(distinct user_id),1) over(order by behavior_type desc) as 上一行为用户人数,
    ifnull(count(distinct user_id)/lag(count(distinct user_id),1) over(order by behavior_type desc),1) as 转化率
from userbehavior_new
where behavior_type in ('pv','cart','buy')
group by behavior_type;


-- 每日浏览—加购—购买的转化率
# 行转列的方法
-- charlly  整理
select 
日期,
sum(if(behavior_type="pv",用户人数,0)) as 浏览人数, -- 用if条件判断将行转化成列,然后对其进行聚合
sum(if(behavior_type="cart",用户人数,0)) as 加购人数,
sum(if(behavior_type="cart",用户人数,0))/sum(if(behavior_type="pv",用户人数,0)) as 浏览_加购转化率,
sum(if(behavior_type="buy",用户人数,0)) as 购买人数,
sum(if(behavior_type="buy",用户人数,0))/sum(if(behavior_type="cart",用户人数,0)) as 加购_购买转化率
from
    (select 
        日期,
        behavior_type,
        count(distinct user_id) as 用户人数     
    from userbehavior_new
    where behavior_type in ('pv','cart','buy')
    group by 日期, behavior_type) as t
group by 日期;



-- 每日浏览—加购—购买的转化率  老师答案
select 
    日期,
    sum(if(behavior_type='pv',用户人数,0)) as 浏览人数,
    sum(if(behavior_type='cart',用户人数,0)) as 加购人数,
    sum(if(behavior_type='buy',用户人数,0)) as 购买人数,
    sum(if(behavior_type='cart',用户人数,0))/sum(if(behavior_type='pv',用户人数,0)) as 浏览_加购转化率,
    sum(if(behavior_type='buy',用户人数,0))/sum(if(behavior_type='cart',用户人数,0)) as 加购_够买转化率
from 
    (select 
        日期,
        behavior_type,
        count(distinct user_id) as 用户人数
    from userbehavior_new
    where behavior_type in ('pv','cart','buy')
    group by 日期,behavior_type) as t
group by 日期;

 

标签:11,count,sum,转列,user,behavior,mysql,type,id
来源: https://www.cnblogs.com/charlly/p/15415272.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有