选读SQL经典实例笔记04_日期运算(上)

news2024/9/22 15:39:57

1. 年月日加减法

1.1. DB2

  • 1.1.1.  sql
select hiredate -5 day   as hd_minus_5D,
        hiredate +5 day   as hd_plus_5D,
        hiredate -5 month as hd_minus_5M,
        hiredate +5 month as hd_plus_5M,
        hiredate -5 year  as hd_minus_5Y,
        hiredate +5 year  as hd_plus_5Y
   from emp
  where deptno = 10

1.2. Oracle

  • 1.2.1.  sql
select hiredate-5                 as hd_minus_5D,
        hiredate+5                 as hd_plus_5D,
        add_months(hiredate,-5)    as hd_minus_5M,
        add_months(hiredate,5)     as hd_plus_5M,
        add_months(hiredate,-5*12) as hd_minus_5Y,
        add_months(hiredate,5*12)  as hd_plus_5Y
   from emp
  where deptno = 10

1.3. PostgreSQL

  • 1.3.1.  sql
select hiredate - interval '5 day'   as hd_minus_5D,
        hiredate + interval '5 day'   as hd_plus_5D,
        hiredate - interval '5 month' as hd_minus_5M,
        hiredate + interval '5 month' as hd_plus_5M,
        hiredate - interval '5 year'  as hd_minus_5Y,
        hiredate + interval '5 year'  as hd_plus_5Y
   from emp
  where deptno=10

1.4. MySQL

  • 1.4.1.  sql
select hiredate - interval 5 day   as hd_minus_5D,
        hiredate + interval 5 day   as hd_plus_5D,
        hiredate - interval 5 month as hd_minus_5M,
        hiredate + interval 5 month as hd_plus_5M,
        hiredate - interval 5 year  as hd_minus_5Y,
        hiredate + interval 5 year  as hd_plus_5Y
   from emp
  where deptno=10
  • 1.4.2.  sql
select date_add(hiredate,interval -5 day)   as hd_minus_5D,
        date_add(hiredate,interval  5 day)   as hd_plus_5D,
        date_add(hiredate,interval -5 month) as hd_minus_5M,
        date_add(hiredate,interval  5 month) as hd_plus_5M,
        date_add(hiredate,interval -5 year)  as hd_minus_5Y,
        date_add(hiredate,interval  5 year)  as hd_plus_5DY
   from emp
  where deptno=10

1.5. SQL Server

  • 1.5.1.  sql
select dateadd(day,-5,hiredate)   as hd_minus_5D,
        dateadd(day,5,hiredate)    as hd_plus_5D,
        dateadd(month,-5,hiredate) as hd_minus_5M,
        dateadd(month,5,hiredate)  as hd_plus_5M,
        dateadd(year,-5,hiredate)  as hd_minus_5Y,
        dateadd(year,5,hiredate)   as hd_plus_5Y
   from emp
  where deptno = 10

1.6. SQL 的ISO 标准语法里规定了INTERVAL关键字以及紧随其后的字符串常量

  • 1.6.1. 该标准要求INTERVAL值必须位于英文单引号内

  • 1.6.2. PostgreSQL ( 和Oracle 9i数据库及其后续版本 ) 遵循了该标准

  • 1.6.3. MySQL 则不支持英文单引号,略微偏离了标准

2. 两个日期之间的天数

2.1. 内嵌视图X和Y被用于分别获取WARD 和ALLEN 的HIREDATE

  • 2.1.1. sql
select ward_hd, allen_hd
  from (
select hiredate as ward_hd
  from emp
 where ename = 'WARD'
       ) y,
       (
select hiredate as allen_hd
  from emp
 where ename = 'ALLEN'
       ) x
WARD_HD     ALLEN_HD
----------- ---------
22-FEB-1981 20-FEB-1981
  • 2.1.1.1. 因为X和Y之间没有任何连接条件,这里会产生笛卡儿积

  • 2.1.1.2. X和Y都只有一条数据,因而即使没有连接条件也不会有问题,结果集最终只会有一行

2.2. DB2

  • 2.2.1.   sql
select days(ward_hd) - days(allen_hd)
     from (
   select hiredate as ward_hd
     from emp
    where ename = 'WARD'
          ) x,
          (
   select hiredate as allen_hd
     from emp
   where ename = 'ALLEN'
         ) y

2.3. Oracle

2.4. PostgreSQL

2.5. sql

select ward_hd - allen_hd
     from (
   select hiredate as ward_hd
     from emp
    where ename = 'WARD'
          ) x,
          (
   select hiredate as allen_hd
     from emp
   where ename = 'ALLEN'
         ) y

2.6. MySQL

2.7. SQL Server

2.8. sql

select datediff(day,allen_hd,ward_hd)
     from (
   select hiredate as ward_hd
     from emp
    where ename = 'WARD'
          ) x,
          (
   select hiredate as allen_hd
     from emp
   where ename = 'ALLEN'
         ) y
  • 2.8.1.1. 对于MySQL 而言,只需去掉DATEDIFF函数的第一个参数,并翻转ALLEN_HD和WARD_HD的顺序即可

3. 两个日期之间的工作日天数

3.1. 思路

  • 3.1.1. 计算出开始日期和结束日期之间相隔多少天(包含开始日期和结束日期)

  • 3.1.2. 排除掉周末,统计有多少个工作日(实际是在计算有多少条记录)

    • 3.1.2.1. sql
select case when ename = 'BLAKE'
            then hiredate
       end as blake_hd,
       case when ename = 'JONES'
            then hiredate
       end as jones_hd
  from emp
 where ename in ( 'BLAKE','JONES' )
BLAKE_HD    JONES_HD
----------- -----------
            02-APR-1981
01-MAY-1981
  • 3.1.2.2. sql
select max(case when ename = 'BLAKE'
            then hiredate
       end) as blake_hd,
       max(case when ename = 'JONES'
            then hiredate
       end) as jones_hd
  from emp
 where ename in ( 'BLAKE','JONES' )
BLAKE_HD    JONES_HD
----------- -----------
01-MAY-1981 02-APR-1981
>  3.1.2.2.1. 使用了聚合函数MAX,其目的在于排除掉Null
  • 3.1.3. T500表的ID列每一个值都等于前面一行的值加上1

    • 3.1.3.1. sql
select x.*, t500.*, jones_hd+t500.id-1
  from (
select max(case when ename = 'BLAKE'
                then hiredate
           end) as blake_hd,
       max(case when ename = 'JONES'
                then hiredate
           end) as jones_hd
  from emp
 where ename in ( 'BLAKE','JONES' )
       ) x,
       t500
 where t500.id <= blake_hd-jones_hd+1
BLAKE_HD    JONES_HD            ID JONES_HD+T5
----------- ----------- ---------- -----------
01-MAY-1981 02-APR-1981          1 02-APR-1981
01-MAY-1981 02-APR-1981          2 03-APR-1981
01-MAY-1981 02-APR-1981          3 04-APR-1981
01-MAY-1981 02-APR-1981          4 05-APR-1981
01-MAY-1981 02-APR-1981          5 06-APR-1981
01-MAY-1981 02-APR-1981          6 07-APR-1981
01-MAY-1981 02-APR-1981          7 08-APR-1981
01-MAY-1981 02-APR-1981          8 09-APR-1981
01-MAY-1981 02-APR-1981          9 10-APR-1981
01-MAY-1981 02-APR-1981         10 11-APR-1981
01-MAY-1981 02-APR-1981         11 12-APR-1981
01-MAY-1981 02-APR-1981         12 13-APR-1981
01-MAY-1981 02-APR-1981         13 14-APR-1981
01-MAY-1981 02-APR-1981         14 15-APR-1981
01-MAY-1981 02-APR-1981         15 16-APR-1981
01-MAY-1981 02-APR-1981         16 17-APR-1981
01-MAY-1981 02-APR-1981         17 18-APR-1981
01-MAY-1981 02-APR-1981         18 19-APR-1981
01-MAY-1981 02-APR-1981         19 20-APR-1981
01-MAY-1981 02-APR-1981         20 21-APR-1981
01-MAY-1981 02-APR-1981         21 22-APR-1981
01-MAY-1981 02-APR-1981         22 23-APR-1981
01-MAY-1981 02-APR-1981         23 24-APR-1981
01-MAY-1981 02-APR-1981         24 25-APR-1981
01-MAY-1981 02-APR-1981         25 26-APR-1981
01-MAY-1981 02-APR-1981         26 27-APR-1981
01-MAY-1981 02-APR-1981         27 28-APR-1981
01-MAY-1981 02-APR-1981         28 29-APR-1981
01-MAY-1981 02-APR-1981         29 30-APR-1981
01-MAY-1981 02-APR-1981         30 01-MAY-1981
>  3.1.3.1.1. Oracle语法

>  3.1.3.1.2. 一旦生成了所需数目的行记录,接着使用CASE表达式来标记每一个日期是工作日或者周末(若是工作日返回1,周末则返回0)

>  3.1.3.1.3. 使用聚合函数SUM来合计1的个数,并得到最终答案

3.2. DB2

  • 3.2.1.   sql
select sum(case when dayname(jones_hd+t500.id day -1 day)
                    in ( 'Saturday','Sunday' )
                   then 0 else 1
              end) as days
     from (
   select max(case when ename = 'BLAKE'
                   then hiredate
              end) as blake_hd,
          max(case when ename = 'JONES'
                  then hiredate
             end) as jones_hd
    from emp
   where ename in ( 'BLAKE','JONES' )
          ) x,
          t500
   where t500.id <= blake_hd-jones_hd+1
  • 3.2.1.1. WHERE子句的话,BLAKE_HD和JONES_HD相减后又加上了1

  • 3.2.1.2. SELECT列表里T500.ID减去了1,这是因为ID列的起始值是1,如果在JONES_HD基础上加上1就等同于从最终结果里排除掉了JONES_HD

3.3. Oracle

  • 3.3.1.   sql
select sum(case when to_char(jones_hd+t500.id-1,'DY')
                     in ( 'SAT','SUN' )
                   then 0 else 1
              end) as days
     from (
   select max(case when ename = 'BLAKE'
                   then hiredate
              end) as blake_hd,
          max(case when ename = 'JONES'
                  then hiredate
             end) as jones_hd
    from emp
   where ename in ( 'BLAKE','JONES' )
         ) x,
         t500
   where t500.id <= blake_hd-jones_hd+1

3.4. PostgreSQL

  • 3.4.1.   sql
select sum(case when trim(to_char(jones_hd+t500.id-1,'DAY'))
                     in ( 'SATURDAY','SUNDAY' )
                   then 0 else 1
              end) as days
     from (
   select max(case when ename = 'BLAKE'
                   then hiredate
              end) as blake_hd,
          max(case when ename = 'JONES'
                  then hiredate
             end) as jones_hd
    from emp
   where ename in ( 'BLAKE','JONES' )
         ) x,
         t500
   where t500.id <= blake_hd-jones_hd+1

3.5. MySQL

  • 3.5.1.   sql
select sum(case when date_format(
                           date_add(jones_hd,
                                    interval t500.id-1 DAY),'%a')
                     in ( 'Sat','Sun' )
                   then 0 else 1
              end) as days
     from (
   select max(case when ename = 'BLAKE'
                   then hiredate
             end) as blake_hd,
          max(case when ename = 'JONES'
                   then hiredate
              end) as jones_hd
    from emp
   where ename in ( 'BLAKE','JONES' )
         ) x,
         t500
   where t500.id <= datediff(blake_hd,jones_hd)+1

3.6. SQL Server

  • 3.6.1.   sql
select sum(case when datename(dw,jones_hd+t500.id-1)
                     in ( 'SATURDAY','SUNDAY' )
                    then 0 else 1
              end) as days
     from (
   select max(case when ename = 'BLAKE'
                   then hiredate
              end) as blake_hd,
         max(case when ename = 'JONES'
                  then hiredate
             end) as jones_hd
    from emp
   where ename in ( 'BLAKE','JONES' )
         ) x,
         t500
   where t500.id <= datediff(day,jones_hd-blake_hd)+1

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

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

相关文章

【计算机网络】第三章 数据链路层(虚拟机与局域网)

文章目录 3.9 以太网交换机自学习和转发桢的流程3.10 以太网交换机的生成树协议STP3.11 虚拟局域网3.11.1 虚拟局域网VLAN概述3.11.2 虚拟局域网VLAN的实现机制 3.9 以太网交换机自学习和转发桢的流程 以太网交换机的自学习和转发数据帧的流程如下: 自学习&#x…

Mac中使用命令行来加密压缩zip文档

背景 最近需要对一些文件加密,但是Mac上没有找到相应的加密工具,macOS中创建密码保护的压缩 zip 文件很容易并且不需要任何额外附加物或下载。使用命令行的方式处理即可。对压缩包加密之后便意味着有人想要解压缩zip文件时,必须输入正确的密码…

Layui入门必看:登录注册界面搭建与功能开发解析

目录 Layui介绍 什么是Layui? Layui入门 Layui登录实例 导入jar 配置 导入Layui 编写公共jsp 编写代码 Layui注册实例 代码实例 Layui介绍 Layui是一款面向前端开发者的轻量级JavaScript库,旨在简化网页开发过程。它提供了丰富的基础UI组件和…

电气设备漏电保护方式研究

摘要:电气设备漏电故障可能对无防范意识人员产生触电危害,轻者灼伤人体接触位置,重者危及人员生命,甚至会产生漏电火花引起火灾,给企业带来不可估计的损失。文中浅谈电气设备漏电危害性及漏电保护方式,意指…

C++使用rapidjson读写json数据

一、背景 RapidJSON简介及使用_fengbingchun的博客-CSDN博客 rapidjson是腾讯的高效C Json解析器,只有头文件,可跨平台使用 mirrors / Tencent / rapidjson GitCode 二、读数据 使用rapidjson解析和组装json_youyicc的博客-CSDN博客 三、写数据 …

AES加解密算法强化训练

目标: 使用openssl算一遍,再使用网页在线工具算一遍,看看结果是否一样 构造数据 如何编写一个二进制规律性的文件, 比如你可以编写一个"0123456789abcdef"的文本文件,记得删除换行符 然后用ultraedit打开,…

常见加密算法介绍

文章目录 一、背景:二、几种常见的加密算法1. 不可逆加密算法(哈希算法):1.1 MD51.1.1 优点:1.1.2 缺点:1.1.3 Demo:1.1.4 案例分析: 1.2 SHA-2561.2.1 Demo:1.2.2 案例分…

day19三数之和 int *returnSize,int ** returnColumnSizes的理解

题目描述 *1.关于参数 int returnSize, int ** returnColumnSizes的理解 具体看这篇文章 [https://blog.csdn.net/m0_52775920/article/details/121461911?spm1001.2014.3001.5502] (1)*returnSize 的理解 returnSize 返回大小为returnSize的二维数组&…

代码随想录算法二刷 day49 | 动态规划 之121 买卖股票的最佳时机 122 买卖股票的最佳时机II

day49 121. 买卖股票的最佳时机1.确定dp数组(dp table)以及下标的含义2.确定递推公式3.dp数组如何初始化4.确定遍历顺序5.举例推导dp数组 122.买卖股票的最佳时机II 121. 买卖股票的最佳时机 题目链接 解题思路: 动规五部曲分析如下&#xf…

【Linux-Windows】 关于PCI和PCIE接口

【Linux-Windows】 关于PCI和PCIE接口 1、背景2、物理外观区别3、其它区别 1、背景 最近在配置电脑主机。 由于要在主机上安装了一块PCI接口的固高控制卡,其系统架构如下图: 使用的PCI接口的固高控制卡外形如下图: 为此,我额外…

2023-7-10-第十五式命令模式

🍿*★,*:.☆( ̄▽ ̄)/$:*.★* 🍿 💥💥💥欢迎来到🤞汤姆🤞的csdn博文💥💥💥 💟💟喜欢的朋友可以关注一下&#xf…

java 代码块

文章目录 代码块的描述静态代码块静态代码块的特点 非静态代码块分析加载顺序 代码块的描述 代码块(或初始化块)的作用: 对Java类或对象进行初始化 代码块(或初始化块)的分类: 一个类中代码块若有修饰符,则只能被static修饰,称为…

嵌入式_一种非常简单实用的基于GD32的裸机程序框架

嵌入式_一种非常简单实用的基于GD32的裸机程序框架 搜索了一下关于GD或ST裸机程序的问题,网上有非常多也非常的例子,但是针对裸机开发的程序框架却比较少,这里简单整理了一下在项目中使用过的一种比较小巧便携的裸机程序框架(确切…

cloud Alibab+nacos+gateway集成swaggerui,统一文档管理(注意点)

首先说明&#xff1a;本文只说整合注意点 效果图和功能参考链接 1.使用gateway访问nacos服务&#xff0c;503 在网关服务添加依赖即可解决 <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign&…

1.入门matlab数理统计随机数的产生(matlab程序)

1.简述 一、常见分布的随机数的产生 随机数是专门的随机试验的结果。在统计学的不同技术中需要使用随机数&#xff0c;比如在从统计总体中抽取有代表性的样本的时候。而matlab直接提供了产生随机数的通用函数&#xff0c;但针对不同的分布&#xff0c;函数形式会有所不同&#…

DITA技巧:将DITA或Markdown发布成CHM

- 1 - 场景 CHM是英文Compiled HTML Help的缩写&#xff0c;是微软公司专有的联机帮助格式&#xff0c;由HTML页面、索引和其他导航工具的集合组成。这些文件被压缩并部署为二进制格式&#xff0c;扩展名为.CHM&#xff0c;用于编译HTML。CHM格式通常用于软件文档。 虽然CHM…

16. 最接近的三数之和(双指针+减去多余步骤)

16. 最接近的三数之和 双指针减去多余步骤测试代码测试结果 给你一个长度为 n 的整数数组 nums 和 一个目标值 target。请你从 nums 中选出三个整数&#xff0c;使它们的和与 target 最接近。 返回这三个数的和。 假定每组输入只存在恰好一个解。 示例 1&#xff1a; 输入&…

JSP环境搭建教程(保姆级!!)

简介 Java Server Pages (JSP) 是一种由 Sun Microsystems 开发的用于创建动态网页的技术。它是 Java EE (Java Enterprise Edition) 技术的一部分&#xff0c;允许开发者在 HTML 中嵌入 Java 代码&#xff0c;从而实现动态内容的生成。 JSP 主要由两部分组成&#xff1a;静态…

LiveGBS流媒体平台GB/T28181功能-如何对接海康大华宇视等监控摄像头报警消息报警订阅国标报警信息

LiveGBS流媒体平台GB/T28181功能-如何对接海康大华宇视等监控摄像头报警消息报警订阅国标报警信息 1、报警信息1.1、报警查询1.2、配置开启报警订阅1.2.1、国标设备编辑1.2.2、选择开启报警订阅 1.3、配置摄像头报警1.3.1、配置摄像头报警通道ID1.3.2、配置摄像头开启侦测1.3.3…

红外雨量计(光学雨量传感器)在预防地质灾害中怎样发挥作用

红外雨量计&#xff08;光学雨量传感器&#xff09;在预防地质灾害中怎样发挥作用 红外雨量计是一种利用红外线原理测量雨量的设备。在预防地质灾害中&#xff0c;红外雨量计可以发挥以下作用&#xff1a; 1.准确测量雨水的降雨量。地质灾害往往与雨水的降雨量密切相关&#x…