Oracle進階SQLDay03

一、函數進階復習

1、行轉列

select '用水儿量(噸)' 统计项,
sum(case when t_account.month='01' then USENUM end) 一月,
sum(case when t_account.month='02' then USENUM end) 二月,
sum(case when t_account.month='03' then USENUM end) 三月,
sum(case when t_account.month='04' then USENUM end) 四月,
sum(case when t_account.month='05' then USENUM end) 五月,
sum(case when t_account.month='06' then USENUM end) 六月
from t_account
    union all
select '金額(元)' 统计项,
sum(case when t_account.month='01' then money end) 一月,
sum(case when t_account.month='02' then money end) 二月,
sum(case when t_account.month='03' then money end) 三月,
sum(case when t_account.month='04' then money end) 四月,
sum(case when t_account.month='05' then money end) 五月,
sum(case when t_account.month='06' then money end) 六月
from t_account;

2、nvl函數統計0值

统计用水量 ,收费金额 (分类型统计)

根据业主类型分别统计每种居民的用水量 (整数 ,四舍五入) 及收费金额 ,如

果该类型在台账表中无数据也需要列出值为 0 的记录.

效果如下 :

分析 :这里所用到的知识点包括左外连接、sum()、分组 group by  、round()  和 nvl()

 

select  distinct t2.name 姓名,
       round(sum(nvl(usenum,0)) over (partition by t2.id),0 )用水量,
       sum(nvl(money,0)) over (partition by t2.id) 總金額
from
    t_account t1  right join t_ownertype t2 on t1.ownertype=t2.id;

 3、簡單查詢

统计每个区域的业主户数 ,如果该区域没有业主户数也要列出 0

select distinct t3.name 區域,
count(t1.id) over (partition by t3.id) 人數
from t_owners t1
join t_address t2 on t1.addressid=t2.id
right join t_area t3 on t2.areaid=t3.id;

二、窗口函數進階

1、學生成績查詢

现有“成绩表”,需要我们取得每名学生不同课程的成绩排名.

已知条件 分数表

结果

student_name

course_name

score

student_name

course_name

score

rn

小明

数学

85

小明

物理

92

1

小明

英语

78

小明

数学

85

2

小明

物理

92

小明

英语

78

3

小红

数学

90

小李

数学

90

1

小红

英语

80

小李

英语

85

2

小李

数学

90

小李

物理

85

3

小李

数学

60

小李

数学

60

4

小李

英语

85

小红

数学

90

1

小李

物理

85

小红

英语

80

2

代碼:

通過row number序號查詢

select student_name,course_name,score,
row_number() over (partition by student_name order by score desc ) 排名
from t_score;

2、 去除最大值、最小值后求平均值

“薪水表”中记录了雇员编号、部门编号和薪水。要求查询出每个部门去除最高、最低薪水后的平均薪水。

已知条件 薪资表

结果

employee_id

department_id

salary

department_id

avg_salary

1

1

50000

1

50000

2

1

52000

2

60000

3

1

48000

4

1

51000

5

1

49000

6

2

60000

7

2

58000

8

2

62000

9

2

59000

10

2

61000

代碼: 

with t1 as (
select department_id,salary,
row_number() over (partition by department_id order by salary  )序号,
row_number() over (partition by department_id order by salary desc )序号1
from t_salary_table )
select department_id,avg(salary)
from t1
where 序号>1 and 序号1>1
group by department_id;

3、Top N问题

 查询前三名的成绩

-- “成绩表”中记录了学生选修的课程号、学生的学号,以及对应课程的成绩。
-- 为了对学生成绩进行考核,现需要查询每门课程前三名学生的成绩。
-- # todo 注意:如果出现同样的成绩,则视为同一个名次

输入

输出

course_id

student_id

score

course_id

student_id

score

rn

1

1

85

1

3

92

1

1

2

78

1

6

92

1

1

3

92

1

8

92

1

1

4

90

1

4

90

2

1

5

80

1

1

85

3

1

6

92

1

9

85

3

1

7

78

2

3

90

1

1

8

92

2

8

90

1

1

9

85

2

1

88

2

2

1

88

2

6

88

2

2

2

82

2

4

85

3

2

3

90

2

4

85

2

5

78

2

6

88

2

7

82

2

8

90

2

9

82

代碼: 

with t1 as (
select student_id,course_id,score,
       dense_rank()  over (partition by COURSE_ID order by score desc )序号
from t_score2)
select course_id,student_id, score
from t1
where 序号 <=3;

4、Top N問題

查询排在前两名的工资 

“雇员表”中是公司雇员的信息,每个雇员有其对应的工号、姓名、工资和部门编号。

现在要查找每个部门工资排在前两名的雇员信息,若雇员工资一样,则并列获取。

已知条件 雇员表

结果表

emp_id

emp_name

salary

department_id

emp_id

emp_name

salary

department_id

rn

1

小明

50000

1

6

小刚

62000

1

1

2

小红

52000

1

4

小张

60000

1

2

3

小李

48000

1

10

小华

52000

2

1

4

小张

60000

1

11

小雷

52000

2

1

5

小王

58000

1

9

小晓

49000

2

2

6

小刚

62000

1

7

小丽

45000

2

8

小芳

47000

2

9

小晓

49000

2

10

小华

52000

2

11

小雷

52000

2

代碼:

with t1 as (
select emp_id,emp_name,department_id,salary,dense_rank() over (partition by  department_id order by salary desc )序号
from t_employee )
select emp_id,emp_name,department_id,salary from t1
where 序号<3;

5、連續問題

员工的 累计工资汇总 可以计算如下:

对于该员工工作的每个月,将 该月 和 前两个月 的工资 加 起来。这是他们当月的 3 个月总工资和 。如果员工在前几个月没有为公司工作,那么他们在前几个月的有效工资为 0 。

不要 在摘要中包括员工 最近一个月 的 3 个月总工资和。

不要 包括雇员 没有工作 的任何一个月的 3 个月总工资和。

返回按 id 升序排序 的结果表。如果 id 相等,请按 month 降序排序。

 

代碼:

select id,month,sum(salary)
    over (partition by id order by month desc
        range between current row and 2 following )馬内
from t_employee1;

6、连续空余座位抛真题 

-- # todo 查找电影院所有连续可用的座位。
-- # todo 返回按 seat_id 升序排序 的结果表。
-- # todo 测试用例的生成使得两个以上的座位连续可用。

代碼1:

-- 第一種方法
-- 1、先按照free分組 打亂seat——id
-- 2、再加入一條等差數列row num 進行對比  因爲使用over partition by 所以  可以直接加等差數列
select Cinema.*,row_number() over (partition by free order by seat_id) 等差數列
from CINEMA;
-- 3、再把沒有空位置的Free給篩選掉
select Cinema.*,row_number() over (partition by free order by seat_id) 等差數列
from CINEMA
where free=1;
-- 4、桌位id減去等差數列如果數字一樣(差值),則證明是連續的
select Cinema.*,
       seat_id-row_number() over (partition by free order by seat_id) 差值
from CINEMA
where free=1;
-- 5、按差值和判斷是否連續的字段(free)分組,進行計數
with t1 as (
select Cinema.*,
       seat_id-row_number() over (partition by free order by seat_id) 差值
from CINEMA
where free=1)
select t1.*,count(*) over (partition by t1.差值,free) 計數 from t1;
-- 6、計數條件判斷是否大於XXX,幾個連續的,如果連續2就大於等於2,即可
with t1 as (
select Cinema.*,
       seat_id-row_number() over (partition by free order by seat_id) 差值
from CINEMA
where free=1),
     t2 as (
select t1.*,count(*) over (partition by t1.差值,free) 計數 from t1)
select t2.seat_id from t2
where 計數>=2
order by seat_id ;

 

代碼2: 

-- 第二種方法
with t1 as (
    select Cinema.*,
           lead(free, 1) over (order by seat_id) rn1,
           lag(free, 1) over (order by seat_id)  rn2
    from Cinema)
select seat_id
from t1
where (t1.free = 1 and t1.rn1 = 1)
   or (t1.rn1 is null and t1.rn2 = 1 and t1.free=1);

 附(連續問題的解題思路):

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

STM32学习和实践笔记(15):STM32中断系统

中断概念 CPU执行程序时&#xff0c;由于发生了某种随机的事件(外部或内部)&#xff0c;引起CPU暂 时中断正在运行的程序&#xff0c;转去执行一段特殊的服务程序(中断服务子程序 或中断处理程序)&#xff0c;以处理该事件&#xff0c;该事件处理完后又返回被中断的程序 继…

飞桨Ai(二)paddle使用CPU版本可以正常识别,切换为GPU版本时无法识别结果

一、问题描述&#xff1a; 刚开始用paddle的CPU版本&#xff0c;对训练好的模型进行推理&#xff0c;正常识别出想要的结果后来尝试使用paddle的GPU版本&#xff0c;然后发现识别出来是空的 二、系统思路&#xff1a; 最终系统环境如下&#xff1a; 系统&#xff1a;win10 …

有哪些公认好用且免费的云渲染网渲平台?渲染100邀请码1a12

现在云渲染是越来越火了&#xff0c;无论是在建筑设计、影视动画还是效果图行业都有它的身影&#xff0c;云渲染能缩短制作周期&#xff0c;提高工作效率&#xff0c;那么市面上有哪些公认好用且免费的云渲染平台呢&#xff1f;这次我们来了解下。 首先&#xff0c;我们来看看有…

vulfocus靶场tomcat-cve_2017_12615 文件上传

7.0.0-7.0.81 影响版本 Windows上的Apache Tomcat如果开启PUT方法(默认关闭)&#xff0c;则存在此漏洞&#xff0c;攻击者可以利用该漏洞上传JSP文件&#xff0c;从而导致远程代码执行。 Tomcat 是一个小型的轻量级应用服务器&#xff0c;在中小型系统和并发访问用户不是很多…

「GO基础」在Windows上配置VS Code GO语言开发环境

&#x1f49d;&#x1f49d;&#x1f49d;欢迎莅临我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:「stormsha的主页」…

查看linux的主机配置脚本

废话不说 直接上指令 curl -Lso- bench.sh | bash 等待后&#xff0c;结果如图&#xff1a; 使用后没有问题&#xff0c;看情况使用 出事概不负责 介意勿用&#xff01;&#xff01;&#xff01;

LD-Pruner、EdgeFusion(On-Device T2I)、FreeDiff、TextCenGen、MemLLM

本文首发于公众号&#xff1a;机器感知 https://mp.weixin.qq.com/s/KiyNfwYWU-wBiCO-hE9qkA 苏 The devil is in the object boundary: towards annotation-free instance segmentation using Foundation Models Foundation models, pre-trained on a large amount of data…

Windows系统下安装paddle

开始使用_飞桨-源于产业实践的开源深度学习平台 (paddlepaddle.org.cn) 命令行下&#xff1a; python -m pip install --upgrade pip --user python -m pip install paddlepaddle2.6.1 -i https://pypi.tuna.tsinghua.edu.cn/simple 报异常 ERROR: Could not install packa…

Jmeter 测试Dubbo接口-实例

1、Dubbo插件准备 ①把jmeter-plugins-dubbo-2.7.4.1-jar-with-dependencies.jar包放在D:\apache-jmeter-5.5\lib\ext目录 ②重新打开Jmeter客户端 在线程组-添加-取样器-dubbo simple&#xff0c;添加dubbo接口请求 2、Jmeter测试lottery接口 ①配置zookeeper参数 由于dub…

windows和虚拟机互传文件

在虚拟机中设置共享文件夹 操作方法&#xff1a;打开VMware–>虚拟机–>设置–>选项–>共享文件夹&#xff08;见下图&#xff09;&#xff0c;大家在共享文件夹当中就可以把Windows当中的D盘或者其它盘共享到虚拟机中。比如我就是将D盘和E盘共享到了虚拟机中。 共…

【Vue】实现显示输入框字符长度

<div style"float: right; margin-right: 10px"><el-popover placement"top-start" width"200" trigger"hover" :content"当前输入的内容字节长度为&#xff1a; this.byteLength &#xff0c;剩余可输入的字节长度和最…

学校管网的仿写

工字形布局完成 效果 代码部分 在这里插入代码片 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta http-equiv"X-UA-Compatible" content"IEedge"><meta name"viewport…

某书Frida检测绕过记录

某书Frida检测绕过记录 前言Frida启动APPHook android_dlopen_ext查看加载的库分析libmsaoaidsec.soFrida检测绕过后记 前言 本来想要分析请求参数加密过程&#xff0c;结果发现APP做了Frida检测&#xff0c;于是记录一下绕过姿势(暴力但有用) Frida版本&#xff1a;16.2.1 AP…

ctfhub-ssrf(2)

1.URL Bypass 题目提示:请求的URL中必须包含http://notfound.ctfhub.com&#xff0c;来尝试利用URL的一些特殊地方绕过这个限制吧 打开环境发现URL中必须包含http://notfound.ctfhub.com&#xff0c;先按照之前的经验查看127.0.0.1/flag.php,发现没什么反应&#xff0c;按照题…

vue和react通用后台管理系统权限控制方案

1. 介绍 在任何企业级应用中&#xff0c;尤其是后台管理系统&#xff0c;权限控制是一个至关重要的环节。它确保了系统资源的安全性&#xff0c;防止非法访问和操作&#xff0c;保障业务流程的正常进行。本文件将详细解析后台管理系统中的权限控制机制及其实施策略。 那么权限…

Linux:zabbix自定义监控项(6)

本章去做一个监控ftp服务是否正常启动的监控项目 大概就是先创建一个模板&#xff0c;我们把要做的东西放入这个模板&#xff0c;然后把这个模板应用到某个监控主机上就可以生效 1.准备监控项脚本 其中的核心就是&#xff0c;通过脚本去判断一个东西的数值&#xff0c;通过这个…

算法刷题记录2

4.图 4.1.被围绕的区域 思路&#xff1a;图中只有与边界上联通的O才不算是被X包围。因此本题就是从边界上的O开始递归&#xff0c;找与边界O联通的O&#xff0c;并标记为#&#xff08;代表已遍历&#xff09;&#xff0c;最后图中剩下的O就是&#xff1a;被X包围的O。图中所有…

【Linux】进程和计划任务

目录 一、进程介绍 1.1 进程与线程的定义 1.1.1 进程(Process)** 1.1.2 线程(Thread)** 1.1.3 进程与线程的区别 1.2 进程的特征 1.3 进程状态 1.3.1 进程的基本状态 1.3.2 进程更多的状态 1.4 进程的优先级 1.5 进程间通信 1.6 进程的分类* 二、进程管理 2.1 查看…

java核心类

一,String字符串 1.1,String字符串是引用类型,且不可变 String str1 "Hello";String str2 str1.concat(" World"); // 使用concat方法连接字符串&#xff0c;返回一个新的字符串对象System.out.println(str1); // 输出&#xff1a;Hello&#xff0c;原始…

[大模型]InternLM2-7B-chat langchain 接入

InternLM2-7B-chat langchain 接入 InternLM2 &#xff0c;即书生浦语大模型第二代&#xff0c;开源了面向实用场景的70亿参数基础模型与对话模型 &#xff08;InternLM2-Chat-7B&#xff09;。模型具有以下特点&#xff1a; 有效支持20万字超长上下文&#xff1a;模型在20万…