踏踏实实练习SQL--day05

简介: 踏踏实实练习SQL--day05

炸裂函数的使用

image.png

合并函数

concat---合并--ABB
concat_ws---带分隔符 合并{
     A,B,B
     只要string字符串才能用到
}
concat_list---不去重合并---A,B,B
concat_set--去重合并

image.png

explode

select class,student_name,student_score from test
lateral view explode(split(student,',')) sn as student_name
lateral view explode(split(score,',')) sc as student_score 

image.png

select class,student_name,student_score from test
lateral view posexplode(split(student,',')) sn as student_index_sn,student_name lateral view
posexplode(split(score,',')) sc as student_index_sc,student_score 
where student_index_sn=student_index_sc


炸裂恢复

image.png

collect_list 收集并形成list集合,结果不去重
语法:collect_list(col)
返回值:array
说明:将某分组内该字段的所有值收集成为一个数组,结果不去重
hive>
select 
  sex,
  collect_list(job)
from
  employee
group by 
  sex
结果:
 ["行政","研发","行政","前台"]
 ["销售","研发","销售","前台"]
2)collect_set 收集并形成set集合,结果去重
语法:collect_set(col)
返回值:array
说明:将某分组内该字段的所有值收集成为一个数组,结果去重
hive>
select 
  sex,
  collect_set(job)
from
  employee
group by 
  sex
结果:
 ["行政","研发","前台"]
 ["销售","研发","前台"]
类型转换函数: cast
  语法: cast(expr as <type>)
  返回值: Expected "=" to follow "type"
  说明: 返回转换后的数据类型
hive> select cast(1 as bigintfrom iteblog;
1
select student,collect_list(concat_ws(',',class)) as class,
       collect_list(concat_ws(',',cast(score as string))) as score
       from
( select 'a' as class,'yuxing' as student,100 as score
    union all select 'c' as class,'yuxing1' as student,60 as score
    union all select 'b' as class,'yuxing2' as student,80 as score
    union all select 'b' as class,'yuxing1' as student,80 as score
    union all select 'c' as class,'yuxing2' as student,80 as score)t
group by student

image.png

知识

行转列(split+explode+laterview)
列转行(concat_ws+collect_list/set)
多行换成一行  列转行
行转列    炸开
列转行  合并
cast转化为数据格式
判定函数decode(判定字段,值1,结果1,值2,结果2.默认值) 对判定字段的值进行判定,如果值为1,函数的结果为1,与值2相等
coalesce(a,b,c,d)合并
Union all 是两个表格上下两段相加 带all不去重
union是将结果集合并 【上下合并】
join是将表合并 【左右字段合并】

每一门课大于60分的学生的所有科目成绩

image.png

image.png

image.png

select t0.student_name,t2.class_name,t1.score
from
join sc t1 on t0.id=t1.id
join class t2 on
t1.cid=t2.id
(select sid,min(score) as min_score from
sc
group by sid
having min(score)>60)t3

某平台七日存留计算

image.png

select a.date as date,
sum((case when datediff(b.date,a.date)=1 then 1 else 0 end))/count(distinct a.uid)
as lst1date_rate, --'次日留存率'
sum((case when datediff(b.date,a.date)=3 then 1 else 0 end))/count(distinct a.uid)
as lst3date_rate, --'三日留存率'
sum((case when datediff(b.date,a.date)=7 then 1 else 0 end))/count(distinct a.uid)
as lst7date_rate --'七日留存率'
from
(select uid,substr(datetime,1,10) as date from
table group by uid,substr(datatime,1,10)) a)
left join (
  select uid,substr(datatime,1,10) as date from table 
  group by uid,substr(datetime,1,10)
)b
on a.uid=b.uid and a.date<b.date
group by a.date

查询首次下单后第二天连续下单的用户比率

从订单信息表(order_info)中查询首次下单后第二天仍然下单的用户占所有下单用户的比例,结果保留一位小数,使用百分数显示,期望结果如下:


期望结果如下:

percentage
<string>
70.0%

需要用到的表:

订单信息表:order_info

order_id (订单id) user_id (用户id) create_date (下单日期) total_amount (订单金额)
1 101 2021-09-30 29000.00
10 103 2020-10-02 28000.00
select
  concat(sum(if(datediff(second_create_date,first_create_date)=1,1,0))/count(*)*100,'%') percentage

from 
(select 
  user_id,
    min(create_date) first_create_date,
    max(create_date) second_create_date
from
(select 
 user_id,
  create_date
from(select
  user_id,
  create_date,
  rank() over (
    partition by
      user_id
    order by
      create_date
  ) rk
from
  (
    select distinct
      create_date,
      user_id
    from
      order_info
  ) t1
)t2
where rk<=2
)t3
group by user_id)t1;

从订单明细表(order_detail)统计每个商品销售首年的年份,销售数量和销售总额

select sku_id,
       year(create_date) year,
       sum(sku_num) order_num,
       sum(price*sku_num) order_amount
from (
         select order_id,
                sku_id,
                price,
                sku_num,
                create_date,
                rank() over (partition by sku_id order by year(create_date)) rk
         from order_detail
     ) t1
where rk = 1
group by sku_id,year(create_date);


相关文章
|
1月前
踏踏实实练习SQL--day09
踏踏实实练习SQL--day09
16 1
|
1月前
踏踏实实练习SQL--day08
踏踏实实练习SQL--day08
17 1
|
16天前
|
SQL 人工智能 算法
【SQL server】玩转SQL server数据库:第二章 关系数据库
【SQL server】玩转SQL server数据库:第二章 关系数据库
52 10
|
1月前
|
SQL 数据库 数据安全/隐私保护
Sql Server数据库Sa密码如何修改
Sql Server数据库Sa密码如何修改
|
2月前
|
SQL 算法 数据库
【数据库SQL server】关系数据库标准语言SQL之数据查询
【数据库SQL server】关系数据库标准语言SQL之数据查询
96 0
|
26天前
|
SQL
启动mysq异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案
启动mysq异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案
17 0
|
16天前
|
SQL 算法 数据库
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(二)数据查询
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(二)数据查询
88 6
|
3天前
|
SQL 数据管理 关系型数据库
如何在 Windows 上安装 SQL Server,保姆级教程来了!
在Windows上安装SQL Server的详细步骤包括:从官方下载安装程序(如Developer版),选择自定义安装,指定安装位置(非C盘),接受许可条款,选中Microsoft更新,忽略警告,取消“适用于SQL Server的Azure”选项,仅勾选必要功能(不包括Analysis Services)并更改实例目录至非C盘,选择默认实例和Windows身份验证模式,添加当前用户,最后点击安装并等待完成。安装成功后关闭窗口。后续文章将介绍SSMS的安装。
7 0
|
12天前
|
SQL 安全 网络安全
IDEA DataGrip连接sqlserver 提示驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接的解决方法
IDEA DataGrip连接sqlserver 提示驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接的解决方法
27 0
|
17天前
|
SQL 存储 数据挖掘
数据库数据恢复—RAID5上层Sql Server数据库数据恢复案例
服务器数据恢复环境: 一台安装windows server操作系统的服务器。一组由8块硬盘组建的RAID5,划分LUN供这台服务器使用。 在windows服务器内装有SqlServer数据库。存储空间LUN划分了两个逻辑分区。 服务器故障&初检: 由于未知原因,Sql Server数据库文件丢失,丢失数据涉及到3个库,表的数量有3000左右。数据库文件丢失原因还没有查清楚,也不能确定数据存储位置。 数据库文件丢失后服务器仍处于开机状态,所幸没有大量数据写入。 将raid5中所有磁盘编号后取出,经过硬件工程师检测,没有发现明显的硬件故障。以只读方式将所有磁盘进行扇区级的全盘镜像,镜像完成后将所
数据库数据恢复—RAID5上层Sql Server数据库数据恢复案例