当前位置: 首页 > article >正文

SQL笔记-2024/01/31

cross join
两个表的笛卡尔积
例如:
select s.name student_name,s.age student_age,s.class_id class_id,c.name class_name
from student s
cross join class c;

子查询
select s.name name,s.score score,s.class_id class_id
from student s
where s.class_id in (select distinct c.id from class c);

子查询 - exists
select s.name,s.age,s.class_id
from student s
where 1=1
and not exists (select 1 from class c where c.id = s.class_id);

组合查询
union 不保留重复行
union all 保留重复行
select s.name,s.age,s.score,s.class_id
from student s
union all
select sn.name,sn.age,sn.score,sn.class_id
from student_new sn;

开窗函数 - sum over
select s.id id,s.name name,s.age age,s.score score,s.class_id class_id,AVG(score) over (partition by class_id) as class_avg_score
from student s;

开窗函数 - sum over order by
select s.id id,s.name name,s.age age,s.score score,s.class_id class_id,SUM(score) over (partition by class_id order by score asc) as class_sum_score
from student s;

开窗函数 - rank
select s.id id,s.name name,s.age age,s.score score,s.class_id class_id,RANK() over (partition by class_id order by score desc) as ranking
from student s;

查询进阶 - 开窗函数 - row_number
select s.id id,s.name name,s.age age,s.score score,s.class_id class_id,ROW_NUMBER() over (partition by class_id order by score desc) as row_number
from student s;

开窗函数 - lag / lead
select s.id id,s.name name,s.age age,s.score score,s.class_id class_id,
LAG(name,1,null) over (partition by class_id order by score desc) as prev_name,
LEAD(name,1,null) over (partition by class_id order by score desc) as next_name
from student s;


http://www.kler.cn/a/232440.html

相关文章:

  • Qt 日志文件的滚动写入
  • 深入探索 TypeScript:从基础到高级特性
  • 无人机应用场景:石油管道巡检技术详解
  • 探索 Python HTTP 的瑞士军刀:Requests 库
  • 操作系统lab4-页面置换算法的模拟
  • C++单例模式与多例模式
  • 前后端通讯:前端调用后端接口的五种方式,优劣势和场景
  • 查大数据检测到风险等级太高是怎么回事?
  • 单片机的省电模式及策略
  • 自动驾驶稳步迈向商业化应用
  • [office] 5元+超过1以外的乘以3+地区费用 #微信#微信
  • leetcode(哈希表)49.字母异位词分组(C++详细解释)DAY5
  • 51单片机基础(C语言):定时器时钟
  • DockerPodman save
  • SpringCloud-Ribbon:负载均衡(基于客户端)
  • 【已解决】:pip is configured with locations that require TLS/SSL
  • 【力扣】快乐数,哈希集合 + 快慢指针 + 数学
  • echarts 曲线图自定义提示框
  • Kafka 生产调优
  • console.log导致内存泄露 打包时自动去掉console.log方法
  • 2024.2.8日总结(小程序开发5)
  • Flink Format系列(2)-CSV
  • C#用Array类的Reverse方法反转数组中元素
  • springboot/ssm出租车管理系统车辆调度管理系统Java系统
  • ChatGPT学习第一周
  • R语言rmarkdown使用