mysql load data infile 导入数据 某一列 空_Sql数据挑战赛amp;网络销售案例分析

SQL挑战赛

第一期:

1: 编写一个查询,列出员工姓名列表,员工每月工资超过2000美元且员工工作时间少于10个月。通过提升employee_id对结果进行排序

select name from employee where salary > 2000 and months < 10 order by employee_id;

2: 查询 Employee 表格中以元音字母开头的 name 名字。结果不包含名字重复记录。

方法一:left函数
select distinct name from employee where left(name,1) in ("a","o","i""e","u")方法二:like模糊匹配
select distinct name from employee where name like '%a'||'%o'|| '%i'|| '%e'|| '%u'方法三:substr函数  SUBSTR (str, pos, len):由 <str> 中的第 <pos> 位置开始,选出接下去的 <len> 个字元。
select distinct name from employee where substr(name,1,1) in ("a","o","i""e","u")

3:编写一个查询,去掉一个最高收入,去掉一个最低收入,该公司员工平均收入是多少?

方法一:
select avg(salary) from employee 
where 
salary != (select max(salary) from employee)  
And
salary !=(select min(salary) from employee);方法二:
select (sum(salary)-max(salary)-min(salary))/(count(*)-2) from employee;方法三:
select avg(salary) from employee t
where 
salary not in  (select max(salary) from employee
unionselect min(salary) from employee)

4:简述NULL, 空字符串""与 0的区别

NULL在数据库中表示没有这条记录

空字符串""为一个长度为0的字符串

0为数字0.

在count时,count(0) 与 count("")都会被聚合,但count(null)不会

第二期

数据表解释:

market_data表的字段介绍为:

order_id(订单ID),order_time(订单时间),

customer_name(用户名称),quantity(购买数量),

sale(销售额),profit(利润)

各项指标的定义为:

R值为:用户最后一次购买到现在(2016年12月31日)的时间间隔,输出月份。

L值为:用户第一次购买和最后一次购买之间的时间间隔,输出月份。

F值为:用户的总共购买次数,仅计算2016年的即可。

M值为:用户的全部销售额,仅计算2016年的即可。

1.查询所有用户的R值和L值。

#TIMESTAMPDIFF(interval,datetime_expr1,datetime_expr2)
返回日期或日期时间表达式datetime_expr1 和datetime_expr2the 之间的整数差。其结果的单位由interval 参数给出。#TIMESTAMPADD(interval,int_expr,datetime_expr)
将整型表达式int_expr 添加到日期或日期时间表达式 datetime_expr中。式中的interval和上文中列举的取值是一样的。#DATEDIFF(date1,date2)返回两个日期之间的天数
select customer_name,timestampdiff(month,max(order_time),'2016-13-31') as R,timestampdiff(month,min(order_time),max(order_time)) as L
from market_data
group bycustomer_name

2.查询用户的R值,F值和M值,注意F值和M值,仅计算2016年度的数字。

#IF(expr1,expr2,expr3)
如果 expr1 是TRUE (expr1 <> 0 and expr1 <> NULL),则 IF()的返回值为expr2; 
否则返回值则为 expr3。IF() 的返回值为数字值或字符串值,具体情况视其所在语境而定。select customer_name,timestampdiff(month,max(order_time),'2016-13-31') as R,count(if(year(order_time)=2016,order_id,null)) as F,round(sum(if(year(order_time)=2016,sale,null)),2) as M
frommarket_data
Group bycustomer_name

3.查询用户的R值,L值和用户生命周期划分。生命周期划分如下:

(新用户:R<=6 and L<=12;忠诚用户:R<=6 and L>12;

流失的老用户:R>6 and L>12; 一次性用户:R>6 and L<=12)

select temp.*,
casewhen R<=6 and L<=12 then '新用户'when R<=6 and L>12 then '忠诚用户'when R>6 and L>12 then '流失的老用户'when R>6 and L<=12 then '一次性用户'
end as 用户生命周期
from(select customer_name,timestampdiff(month,max(order_time),'2016-13-31') as R,timestampdiff(month,min(order_time),max(order_time)) as L
from market_data
group bycustomer_name) as temp

第三期

Cinema表结构各字段介绍如下:

Seat_id(座位号,依次递增),free(0表示有人,1表示空座),fare(对应座位的票价)

题目为:

1:查找表中最便宜的票价是多少?

方法一:
select * from cinema 
where free=1 
order by fare
limit 1方法二
select min(fare)
from cinema
where free=1

2:女友要求你定的座位必须是连续的(输出可用位置的seat_id)

select c1.seat_id,c2.seat_id
from cinema c1- -两表连接(自连接)join cinema c2- -限制条件位连续座位on c1.seat_id+1 =c2.seat_id- -空闲座位
where c1.free=1 and c2.free=1;

3:女友要求买连续的座位中总价最低的两个座位(输出对应的seat_id和总价)

select c1.seat_id,c2.seat_id,c1.fare+c2.fare
from cinema c1- -两表连接(自连接)join cinema c2- -限制条件位连续座位on c1.seat_id+1 =c2.seat_id- -空闲座位
where c1.free=1 and c2.free=1- -对价格排序并限制输出一个
order by c1.fare+c2.fare limit 1

第四期

employ表内字段的解释如下:

position_name(职位名称),min_salary(最低薪资,单位元),

max_salary(最高薪资,单位元),city(工作城市),

educational(学历要求),people(招聘人数),industry(行业)

题目为:

1.查找不同学历要求的平均最低薪资和平均最高薪资;

select educational,round(avg(min_salary),round(avg(max_salary))
from employ
group by educational;

2.查找每个行业,最高工资和最低工资差距最大的职位名称。

selectin industry,position_name,max(max_salary-min_salary)
from employ
group by industry

3.查找各个城市电商行业的平均薪资水平,并按照薪资高低水平进行排序。(岗位的薪资用最低薪资加最高薪资除以二当成平均薪资计算即可,注意要考虑到职位招聘人数)

select city,round(sum((max_salary+min_salary)/2*people)/sum(people)) as 平均薪资
from employee
where industry='互联网/电子商务'
group by city
order by 平均薪资 desc;

4.问答题:说明UNION和UNION ALL的差别

都是做表的合并连结

union会删除重复值;union all 表中数据全部合并,忽略重复值

数据来源于某网站销售统计

  1. 网络订单数据
  2. 用户信息
提取码:3k6i​pan.baidu.com

分析步骤

0b1b50f5c3fbfd878d5a8c8ab02f998b.png

0.数据导入

首先需要先创建对应的数据库和相应的表

  1. 创建orderinfo 表

07c6a7422a317ac20136f5cb89e99e5b.png

2..创建userinfo表

2ef22b0b64d24af2c01992a41ef6d9e2.png
#userinfo和orderinfo数据信息如下:
userinfo  客户信息表       userId    客户idsex       性别birth     出生年月日orderinfo 订单信息表       orderId   订单序号userId    客户idisPaid    是否支付price     商品价格paidTime  支付时间
  1. 登录mysql导入相应的数据

load data local infile "file" into table dbname.tablename ...

# 登录
mysql --local-infile -uroot -p
# 导入数据orderinfo
load data local  infile 'F:BaiduNetdiskDownloadSQLorder_info_utf.csv' into table data.orderinfo fields terminated by ',';
# 导入数据userinfo
load data local  infile 'F:BaiduNetdiskDownloadSQLuser_info_utf.csv' into table data.userinfo fields terminated by ',';

2.观察数据,对时间进行处理 ; 更新字符串为日期格式

update orderinfo set paidtime=replace(paidtime,'/','-') where paidtime is not null
update orderinfo set paidtime=str_to_date(paidtime,'%Y-%m-%d %H:%i') where paidtime is not null

3.查看数据

5e4bb22ee5634516c7f019a73a7e2ae0.png

1.不同月份的下单人数

思路 :按月份进行分组,对用户进行去重统计

select month(paidTime) as dtmonth, 
count(distinct userId) as count_users
from orderinfo
where isPaid = '已支付'
group by month(paidTime)

16a0d7284825f5be10a746e66b22090d.png

2 用户三月份的回购率和复购率

  1. 复购率 : 自然月内,购买多次的用户占比
  • 首先先找出已支付中3月份的用户id和对应次数,按用户分组
  • 然后再嵌套一层,复购率:购买次数大于1/ 总购买次数
select count(ct),count(if(ct>1,1,null)) from(select userID,Count(userId) as ct from orderinfowhere isPaid = '已支付'and month(paidTime) = 3group by userIdorder by userId) t

fff68208e9e21da225552b4fa2602601.png

复购率: 16916 / 54799 = 0.308

  1. 回购率: 曾经购买过的用户在某一时期内再次购买的占比

首先先查询已支付userId ,和 支付月份的统计

select userId, date_format(paidTime, '%Y-%m-01') as m from orderinfowhere isPaid = '已支付'group by userId , date_format(paidTime,'%Y-%m-01')

然后使用date_sub函数,将表关联,筛选出本月的消费的userID,和下月的回购userID,即可计算出回购率

select t1.m,count(t1.m) as 消费总数,count(t2.m) as 复购率,count(t2.m)/ count(t1.m) as 回购率 from ( select userId, date_format(paidTime, '%Y-%m-01') as m from orderinfowhere isPaid = '已支付'group by userId , date_format(paidTime,'%Y-%m-01')) t1
left join ( select userId, date_format(paidTime, '%Y-%m-01') as m from orderinfowhere isPaid = '已支付'group by userId , date_format(paidTime,'%Y-%m-01')) t2
on t1.userId = t2.userId and t1.m = date_sub(t2.m, interval 1 month)
group by t1.m

81e874bae347849effb0946d7dabea5d.png

3 统计男女用户的消费频次

  • userinfo因为性别有空值,需要筛选出t orderinfo 再和表t连接 统计出用户男女消费次数
select o.userId,sex,count(o.userId)as ct from orderinfo oinner join(select * from userinfowhere sex != '') ton o.userId = t.userIdgroup by userId,sexorder by userId

a9f37626f3c8afeac71fa06c4b02cb93.png
  • 根据上表,在进行子查询,统计出男性消费频次
select sex,avg(ct) from(select o.userId,sex,count(o.userId)as ct from orderinfo oinner join(select * from userinfowhere sex != '') ton o.userId = t.userIdgroup by userId,sexorder by userId)t2
group by sex

6fc6d42801a898ee007fb5ae9b6cd4c1.png

4 统计多次消费用户,分析第一次和最后一次的消费间隔

  • 首先把多次消费的用户,和相应第一次最后一次消费时间提取出来
  • 然后使用datediff 计算时间间隔,以天为单位
select userId,max(paidTime),min(paidTime),datediff(max(paidTime),min(paidTime)) from data.orderinfo
where isPaid = '已支付'
group by userId having count(1) > 1
order by userId

6a123886ac64c7ccc6dc2583bc79b0ce.png

5 统计不同年龄段用户的消费金额差异

通过表联结,给用户划分不同的年龄段,以10年为基准,过滤出生日期为1900-00-00的异常值,筛选出用户消费频次和消费金额

select o.userId,age,price,count(o.userId)as ct from orderinfo o
inner join (select userId, ceil((year(now()) - year(birth))/10) as agefrom userinfowhere birth > 1901-00-00) t
on o.userId = t.userId
where isPaid = '已支付'
group by userId
order by userId

832e2b3df4255855d3c521f8ad40a586.png

统计出年龄段的消费频次和消费金额

select t2.age,avg(ct),avg(price) from (select o.userId,age,price,count(o.userId)as ct from orderinfo o inner join(select userId, ceil((year(now()) - year(birth))/10) as agefrom userinfowhere birth > 1901-00-00)ton o.userId = t.userIdwhere ispaid = '已支付'group by userId, age) t2
group by age
order by age

ac8ec55a0f14f502785b4ed6d582de6c.png
  • ceil : 向上取整

6 统计消费的二八法则:消费top20%的用户贡献了多少消费额度

按照用户消费总额排序

select userId,sum(price) as total from orderinfo o
where isPaid = '已支付'
group by userId
order by total desc

8130e907cfab7bf1ebb13f20e86085e2.png

查看总用户数和总金额

select count(userId),sum(total) from (select userId,sum(price) as total from orderinfo owhere isPaid = '已支付'group by userIdorder by total desc) as t

8deb7688fb89e153ee2204c7bdcf3dc9.png

查看前20%的用户数量有多少

select count(userId)*0.2,sum(total) from (select userId,sum(price) as total from orderinfo owhere isPaid = '已支付'group by userIdorder by total desc)as t

7e920b97804ef15a0b3a9cd1b0785710.png

limit限制前17000用户

select count(userId),sum(total) from (
select userId,sum(price) as total from orderinfo o
where isPaid = '已支付'
group by userId
order by total desc
limit 17129) t

834a7e3ebeda1d0b24d19234482cb2b4.png

top20%用户的消费总额占比情况:top20%用户的消费总额/所有用户的消费总额=73.93%
top20%的用户贡献了73.93%消费额度。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/498797.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

html项目列表页面源码,HTML 列表

HTML 列表HTML 支持有序、无序和定义列表:HTML 列表有序列表第一个列表项第二个列表项第三个列表项无序列表列表项列表项列表项在线实例无序列表本例演示无序列表。有序列表本例演示有序列表。(可以在本页底端找到更多实例。)HTML无序列表无序列表是一个项目的列表&#xff0c;…

html用转义字符画菱形,JavaScript生成字符画(ASCII Art)

今天玩一些新的东西&#xff0c;大家都没有看过这样的视频&#xff1a;或者 这样的图片&#xff1a;网上有很多生成这种图片/视频的工具&#xff0c;但是每个程序员都有一颗造轮子的心&#xff0c;我们当然要玩出自己的花样啦。老规矩&#xff0c;还是先讲原理&#xff0c;建议…

微信更新对html影响,微信再次大更新 将极大影响用户使用习惯

[导读]微信再次迎来历史性大更新&#xff0c;小程序终于展露腾讯野心&#xff01;微信再次迎来历史性大更新&#xff0c;小程序终于展露腾讯野心!...微信小程序桌面在昨日1月22日晚&#xff0c;微信更新7.0.3版本&#xff0c;腾讯在App Store中只表示&#xff1a;本次是一次小更…

什么叫pmt测试分析_RVS — 面向目标硬件的软件性能测试工具

Rapita Verification Suite(简称&#xff1a;RVS)&#xff0c;为英国Rapita Systems公司提供的一款嵌入式系统在板测试套件。其产品符合ISO-26262、IEC-61508等行业标准&#xff0c;兼容Vxworks、Linux、SYSBIOS 等操作系统&#xff0c;支持C、C、Ada多种语言&#xff0c;多方位…

版本不见了_王者荣耀复古版本来袭?第四代主宰形象回归!可以给小兵加速

山河不足重&#xff0c;重在遇知已。好久不见&#xff0c;别来无恙来自小助理的文章推送~时值中秋&#xff0c;先祝大家中秋节快乐&#xff01;王者荣耀的新版本即将上线&#xff0c;不知道大家还记不记得新版本将会上线很多新的东西&#xff0c;峡谷路线更改&#xff0c;鼓励同…

python从小到大的顺序输出_「小白专栏」Python中使用for循环,为什么输出结果不是按顺序?...

欢迎各位小哥哥小姐姐阅读本的文章,对大家学习有帮助,请点赞加关注哦!!!!!!!!!!您的点赞和关注将是我持续更新的动力呢.^v^有不懂的问题可以私聊我哦!前言如图&#xff0c;为什么输出的不是按Jen, Sarah, Phil, Edward的顺序呢&#xff1f;大家可以先想想为什么&#xff1f;思考…

雷库兹韦尔量子计算机,熬到2045年,人类可能靠人工智能战胜死亡了

这几天差评君在网上冲浪的时候&#xff0c;无意间挖到了这一张坟图。虽然这已经是五六年前的老梗了&#xff0c;但依旧今人唏嘘不已&#xff0c;毕竟这些年来的技术发展真的是又快又粗暴。让人不由得想像公知们一样阴阳怪气一番&#xff1a;科技啊&#xff0c;请你慢些走&#…

elxel表格纸张尺寸_一本书的诞生:纸张知识

平张纸的数量以令来计算&#xff0c;不论纸张(百科)大小&#xff0c;每500张为一令。卷筒纸的数量通常以吨来计算&#xff0c;即用重量来反映数量。单张纸的重量以每平方米的克重来表示&#xff0c;单位是gsm&#xff0c;即g&#xff0f;m2&#xff0c;如果说80g的纸,就是每平方…

追加的英文计算机,Latex同时添加中英文摘要

注重版权&#xff0c;若要转载烦请附上作者和链接作者&#xff1a;Joshua_yi链接&#xff1a;https://blog.csdn.net/weixin_44984664/article/details/106168468哎&#xff0c;已经步入了开始写论文的年纪了&#xff0c;从之前的上传作业也慢慢变成了上交论文第一次用latex这玩…

React 路由

引言 在我们之前写的页面当中&#xff0c;用我们的惯用思维去思考的话&#xff0c;可能会需要写很多的页面&#xff0c;例如做一个 tab 栏&#xff0c;我们可能会想每个选项都要对应一个 HTML 文件&#xff0c;这样会很麻烦&#xff0c;甚至不友好&#xff0c;我们把这种称为 …

清华大学06届 计算机王煜,祝贺!这两位三年前从超银中学毕业的学霸,今年被清华大学“破格”录取啦...

青岛日报社/观海新闻8月13日讯 今年是“强基计划”首年招生&#xff0c;“强基计划”属于单独批次录取&#xff0c;也是高考所有批次录取中最早公布结果的&#xff0c;一经录取&#xff0c;就不再参加后续高考志愿录取投档。观海新闻记者从超银中学获悉&#xff0c;青岛二中今年…

jmeter web服务器协议,【JMeter4.0学习(三)】之SoapUI创建WebService接口模拟服务端以及JMeter测试SOAP协议性能测试脚本开发(示例代码)...

目录&#xff1a;【阐述】&#xff1a;首先应该遇到了一个共同的问题&#xff0c;JMeter3.2之后就没有WebService(SOAP) Request,后来经过查询网上资料得知其实可以用HTTP请求来操作&#xff0c;结果是一样的。【步骤】&#xff1a;一、创建WebService接口模拟服务端如果大家有…

flask bootstrap ajax,使用Flask集成bootstrap的方法

1. 下载flask-bootstrappip install flask-bootstrap2. 找到base.html文件将site-packages\flask_bootstrap\templates文件夹下的bootstrap目录copy到你的项目\templates目录下&#xff0c;确保bootstrap目录下包含base.html文件&#xff0c;因为我们后面要用到。3. 代码user.h…

chrome切换前端模式_H5暗黑模式在京东收银台中的实践

背景暗黑主题下&#xff0c;用户可以选择采用深色的系统范围外观而不是浅色外观。在暗黑模式下&#xff0c;系统对所有窗口&#xff0c;视图&#xff0c;菜单和控件采用较暗的调色板。谷歌的 Gmail 和 Chrome 浏览器、聊天工具 slack、telegram、Edge 浏览器和 Office 移动版 A…

使用udp协议实现服务器端程序时,用VisualC#实现UDP协议(二)

12.并以下面代码替换Form.cs中由系统产生的InitializeComponent过程。private void InitializeComponent ( ){this.button1 new System.Windows.Forms.Button ( ) ;this.button2 new System.Windows.Forms.Button ( ) ;this.textBox1 new System.Windows.Forms.TextBox ( ) …

魅族16无信号服务器,魅族16信号差的解决办法

手机信号问题一直都是人们关注的问题&#xff0c;在日常使用时有些地方手机可能出现突然信号变差&#xff0c;可能别人的手机信号一直很好只有你的出现了问题。魅族手机最早的几个版本都很容易出现这种问题&#xff0c;新款的魅族16怎么样呢&#xff1f;魅族16信号差怎么解决呢…

服务器系统核心和带gui区别,Windows Server 2012图形用户界面(GUI)和服务器核心(Server Core)之间的切换...

当安装 Windows Server 2012 时&#xff0c;咱们能够在“服务器核心安装”和“彻底安装”之间任选其一。“带 GUI 选项的服务器”选项Windows Server 2012 等效于 Windows Server 2008 R2 中的彻底安装选项。“服务器核心安装”选项可减小所需的磁盘空间、潜在的***面&#xff…

头条自己提问的问题在哪看_在头条的这三十天

文、图&#xff1a;书海履痕今天入头条三十天&#xff0c;按民间俗语&#xff0c;满月了。 三十个日子&#xff0c;真得是感慨万千。特别是昨日的文章&#xff0c;经头条君和各位友友们的厚爱&#xff0c;让我经历了过山车的感觉&#xff0c;各种滋味存于心底&#xff0c;在此谢…

c可以 char* 赋值但是c++不可以_雷佳音的妻子完全可以女团C位出道,这么有气质的女人,谁能不爱...

导读&#xff1a;雷佳音的妻子完全可以女团C位出道&#xff0c;这么有气质的女人&#xff0c;谁能不爱各位点开这篇文章的朋友们&#xff0c;想必都是很高的颜值吧&#xff0c;我们真的是很有缘哦&#xff0c;小编每天都会给大家带来不一样的汽车资讯&#xff0c;如果对小编的文…

oracle sequence 不同 会话 不连续_序列 Sequence

Sequence是一个数据库对象&#xff0c;多个用户可以从中生成唯一的整数&#xff0c;可以使用序列自动生成主键值。生成序列号时&#xff0c;序列号将递增&#xff0c;独立于事务提交或回滚;如果两个用户同时递增同一序列&#xff0c;因为序列号是由另一个用户生成的&#xff0c…