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中,(41)用于定义文档的标题。

Packet switching is a method of slicing __(71)__ messages into parcels called “packets,” sending the packets along different communication __(72)__ as they become available, and then reassembling the packets __(73)__they arrive at their destination. Prio…

stm32的afio初始化代码_STM32-IO-AFIO(复用功能IO和调试配置)

最近在学习STM32&#xff0c;在BZ上一篇关于的串口通信文章里有这么一段代码&#xff1a;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD| RCC_APB2Periph_AFIO,ENABLE);当初是参考开发的里子写的一直对GPIOD或上“RCC_APB2Periph_AFIO”这句话的意思没搞懂&#xff0c;通过这几…

js获取dom html元素属性,JS如何通过元素的CLASS属性得到对应的DOM对象?

非IE6,7,8可以直接用自带的属性 getElementsByClassName,如果需要考虑兼容&#xff0c;就需要自己写了。下面是自己写的&#xff1a;function getClassName(obj,sName) //-->obj是要获取元素的父级{ //-->sName是class名字if(document.getElementsByClassName){return ob…

feign调用多个服务_spring cloud各个微服务之间如何相互调用(Feign、Feign带token访问服务接口)...

1、首先先看什么是Feign。2、若其他服务的接口未做权限处理&#xff0c;参照上文第1点的博文即可。3、若其他服务的接口做了权限的处理(例如OAuth 2)时该如何访问?a、有做权限处理的服务接口直接调用会造成调用时出现http 401未授权的错误&#xff0c;继而导致最终服务的http …

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

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

vue 实现数据滚动显示_vue实现动态添加数据滚动条自动滚动到底部的示例代码...

在使用vue实现聊天页面的时候&#xff0c;聊天数据动态加到页面中&#xff0c;需要实现滚动条也自动滚动到底部。这时我找到网上有个插件 vue-chat-scroll但是安装后发现是用不了的&#xff0c;报错信息如下&#xff1a;VM14383:27 [Vue warn]: Failed to resolve directive: c…

linux关机方法有哪些?有何区别_Linux关机命令大全:Linux各关机命令之间的区别和用法...

Linux怎么用命令来进行关机/重启呢&#xff1f;Linux是一套免费使用和自由传播的类Unix操作系统&#xff0c;想使用Linux系统的关机命令必须要拿到root权限&#xff0c;下面给大家介绍一些Linux常用的关机命令以及各关机命令之间的区别和用法。Linux系统下常用的一些关机命令&a…

html5 canvas裁剪图片,html5-canvas 使用画布裁剪图像

示例本示例显示了一个简单的图像裁剪功能&#xff0c;该功能获取图像和裁剪坐标并返回裁剪后的图像。function cropImage(image, croppingCoords) {var cc croppingCoords;var workCan document.createElement("canvas"); // 创建一个画布workCan.width Math.floor…

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

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

大数据职业理解_大数据带给我们职业三大根本改变

那么&#xff0c;大数据为什么成为所有人关注的焦点?大数据带来了什么样的本质性改变?为此&#xff0c;我们与中国计算机学会大数据学术带头人、中国人民大学信息学院院长杜小勇教授进行了访谈。大数据(Big data)通常用来形容数字化时代下创造出的大量非结构化和半结构化数据…

微信更新对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;多方位…

matlab2019b 深度学习_Matlab 2019b 数据标注问题、工具与自动标注

从影像中自动提取目标&#xff0c;并进行稳定跟踪&#xff0c;是自动驾驶感知系统的重要目标。本技术博文&#xff0c;介绍了基于卷积神经网络技术(CNN)的图像目标识别的全过程&#xff0c;包括&#xff1a;l FasterCNN中影响标注的数据格式l imageLabeler的结果&#xff0c;非…

帕斯卡三角形html,04_帕斯卡三角形

# 用时0msclass Solution(object):def generate(self, numRows):""":type numRows: int:rtype: List[List[int]]"""# 第 n 行要用到 n-1 行的数据&#xff0c;应该是个动态规划吧# n [1 n-1[0]n-1[1] .... n-1[n-2]n-1[n-1]]if numRows 0:retu…

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

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

jxl读取html格式excel,基于Java+Selenium的WebUI自动化测试框架(十)-----读取Excel文件(JXL)...

packagewebui.xUtils;importjava.io.File;importorg.testng.Reporter;importjxl.Cell;importjxl.Sheet;importjxl.Workbook;public classExcelReadJXL {/***jxl读取Excel*指定文档路径及名称* 指定开始及结束行&#xff0c;开始及结束列*paramsheet_name 为sheet工作表名称&…

sqlserver垮库查询_SQLServer跨库查询--分布式查询

--用openrowset连接远程SQL或插入数据--如果只是临时访问,可以直接用openrowset--查询示例select * from openrowset(SQLOLEDB,sql服务器名;用户名;密码,数据库名.dbo.表名)在跨库查询时需要启用Ad Hoc Distributed Queries启用Ad Hoc Distributed Queries&#xff1a;exec sp_…

360产品演示代码 css3 html5,HTML5 CSS3代码的三维展示

CSS语言&#xff1a;CSSSCSS确定body {background-color: #ececec;}pre {white-space: pre-wrap;}#perspective {position: relative;width: 560px;height: 446px;margin: 80px auto 0;z-index: 1;user-select: none;cursor: default;transform-style: preserve-3d;}#perspecti…

js原生后代选择器_CSS 后代选择器

具体应用后代选择器的功能极其强大。有了它&#xff0c;可以使 HTML 中不可能实现的任务成为可能。假设有一个文档&#xff0c;其中有一个边栏&#xff0c;还有一个主区。边栏的背景为蓝色&#xff0c;主区的背景为白色&#xff0c;这两个区都包含链接列表。不能把所有链接都设…

计算机组装没步的组件,计算机组装教案.doc

计算机组装教案计算机基础—组装计算机襄城职高 崔红燕一、教案背景1、面向学生&#xff1a;■职业技术学校2、学科&#xff1a;计算机信息技术3、课时&#xff1a;2课时4、课前准备&#xff1a;①利用网络搜集计算机各部件的图片及组装视频。②准备计算机主机的各个实物部件(主…