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

news/2024/7/7 20:39:54

 

1. 两个日期之间相差的月份和年份

1.1. DB2

1.2. MySQL

1.3. sql

select mnth, mnth/12
    from (  select (year(max_hd) - year(min_hd))*12 +
         (month(max_hd) - month(min_hd)) as mnth
    from (
  select min(hiredate) as min_hd, max(hiredate) as max_hd
    from emp
         ) x
         ) y

1.4. Oracle

1.4.1.  sql

select months_between(max_hd,min_hd),
         months_between(max_hd,min_hd)/12
    from (
  select min(hiredate) min_hd, max(hiredate) max_hd
    from emp
         ) x

1.5. PostgreSQL

1.5.1.   sql

select mnth, mnth/12
     from (
   select ( extract(year from max_hd) -
            extract(year from min_hd) ) * 12
          +
          ( extract(month from max_hd) -
            extract(month from min_hd) ) as mnth
     from (
   select min(hiredate) as min_hd, max(hiredate) as max_hd
    from emp
         ) x
         ) y

1.6. SQL Server

1.6.1.   sql

select datediff(month,min_hd,max_hd),
         datediff(month,min_hd,max_hd)/12
    from (
  select min(hiredate) min_hd, max(hiredate) max_hd
    from emp
         ) x

2. 两个日期之间相差的秒数、分钟数和小时数

2.1. 相差的天数分别乘以24(一天的小时数),1440(一天的分钟数)和86400(一天的秒数)

2.2. DB2

2.2.1.   sql

select dy*24 hr, dy*24*60 min, dy*24*60*60 sec
     from (
   select ( days(max(case when ename = 'WARD'
                     then hiredate
                end)) -
            days(max(case when ename = 'ALLEN'
                     then hiredate
                end))
          ) as dy
    from emp
         ) x

2.3. Oracle

2.4. PostgreSQL

2.5. sql

select dy*24 as hr, dy*24*60 as min, dy*24*60*60 as sec
     from (
   select (max(case when ename = 'WARD'
                   then hiredate
               end) -
           max(case when ename = 'ALLEN'
                    then hiredate
               end)) as dy
      from emp
          ) x

2.6. MySQL

2.7. SQL Server

2.8. sql

select datediff(day,allen_hd,ward_hd)*24 hr,
          datediff(day,allen_hd,ward_hd)*24*60 min,
          datediff(day,allen_hd,ward_hd)*24*60*60 sec
     from (
   select max(case when ename = 'WARD'
                    then hiredate
              end) as ward_hd,
          max(case when ename = 'ALLEN'
                   then hiredate
             end) as allen_hd
    from emp
         ) x

3. 当前记录和下一条记录之间的日期差

3.1. DB2

3.1.1.  sql

select x.*,
        days(x.next_hd) - days(x.hiredate) diff
   from (
 select e.deptno, e.ename, e.hiredate,
        (select min(d.hiredate) from emp d
          where d.hiredate > e.hiredate) next_hd
   from emp e
  where e.deptno = 10
        ) x

3.2. Oracle

3.2.1.   sql

select ename, hiredate, next_hd,
         next_hd - hiredate diff
    from (
  select deptno, ename, hiredate,
         lead(hiredate)over(order by hiredate) next_hd
    from emp
         )
   where deptno=10

3.3. PostgreSQL

3.3.1.  sql

select x.*,
        x.next_hd - x.hiredate as diff
   from (
 select e.deptno, e.ename, e.hiredate,
        (select min(d.hiredate) from emp d
          where d.hiredate > e.hiredate) as next_hd
   from emp e
  where e.deptno = 10
        ) x

3.4. MySQL

3.5. SQL Server

3.6. sql

select x.*,
        datediff(day,x.hiredate,x.next_hd) diff
   from (
 select e.deptno, e.ename, e.hiredate,
        (select min(d.hiredate) from emp d
          where d.hiredate > e.hiredate) next_hd
   from emp e
  where e.deptno = 10
        ) x

3.6.2.  datediff(x.next_hd, x.hiredate) diff

3.6.2.1. 对于MySQL 版本的DATEDIFF函数,需要省略第一个参数day,并把剩下的两个参数的顺序颠倒过来

4. 一年中有多少个星期一

4.1. 方案

4.1.1. 生成一年里所有可能的日期值

4.1.2. 格式化上述日期值,并找出它们分别是星期几

4.1.3. 统计每个“星期x”出现的次数

4.2. DB2

4.2.1.   sql

with x (start_date,end_date)
   as (
   select start_date,
          start_date + 1 year end_date
     from (
   select (current_date -
           dayofyear(current_date) day)
           +1 day as start_date
     from t1
         )tmp
   union all
  select start_date + 1 day, end_date
    from x
   where start_date + 1 day < end_date
  )
  select dayname(start_date),count(*)
    from x
   group by dayname(start_date)

4.3. Oracle

4.3.1.  sql

with x as (
  select level lvl
    from dual
   connect by level <= (
     add_months(trunc(sysdate,'y'),12)-trunc(sysdate,'y')
   )
  )
  select to_char(trunc(sysdate,'y')+lvl-1,'DAY'), count(*)
    from x
  group by to_char(trunc(sysdate,'y')+lvl-1,'DAY')

4.3.2.  sql

select to_char(trunc(sysdate,'y')+rownum-1,'DAY'),
        count(*)
   from t500
  where rownum <= (add_months(trunc(sysdate,'y'),12)
                   - trunc(sysdate,'y'))
  group by to_char(trunc(sysdate,'y')+rownum-1,'DAY')

4.3.2.1. Oracle早期版本

4.4. PostgreSQL

4.4.1.   sql

select to_char(
             cast(
      date_trunc('year',current_date)
                  as date) + gs.id-1,'DAY'),
          count(*)
     from generate_series(1,366) gs(id)
    where gs.id <= (cast
                     ( date_trunc('year',current_date) +
                          interval '12 month' as date) -
  cast(date_trunc('year',current_date)
                        as date))
   group by to_char(
               cast(
         date_trunc('year',current_date)
            as date) + gs.id-1,'DAY')

4.5. MySQL

4.5.1.   sql

select date_format(
             date_add(
                 cast(
               concat(year(current_date),'-01-01')
                      as date),
                      interval t500.id-1 day),
                      '%W') day,
          count(*)
     from t500
   where t500.id <= datediff(
                        cast(
                      concat(year(current_date)+1,'-01-01')
                             as date),
                        cast(
                      concat(year(current_date),'-01-01')
                             as date))
   group by date_format(
               date_add(
                   cast(
                 concat(year(current_date),'-01-01')
                        as date),
                        interval t500.id-1 day),
                        '%W')

4.6. SQL Server

4.6.1.   sql

with x (start_date,end_date)
   as (
   select start_date,
          dateadd(year,1,start_date) end_date
     from (
   select cast(
          cast(year(getdate()) as varchar) + '-01-01'
               as datetime) start_date
     from t1
         ) tmp
  union all
  select dateadd(day,1,start_date), end_date
    from x
   where dateadd(day,1,start_date) < end_date
  )
  select datename(dw,start_date),count(*)
    from x
   group by datename(dw,start_date)
 OPTION (MAXRECURSION 366)

http://lihuaxi.xjx100.cn/news/1333451.html

相关文章

Python(四):Pycharm的安装配置

❤️ 专栏简介:本专栏记录了我个人从零开始学习Python编程的过程。在这个专栏中,我将分享我在学习Python的过程中的学习笔记、学习路线以及各个知识点。 ☀️ 专栏适用人群 :本专栏适用于希望学习Python编程的初学者和有一定编程基础的人。无…

实例成员静态成员、对象、内置构造函数

实例成员&静态成员 实例成员 通过构造函数创建的对象 称为实例对象 实例对象中的属性(实例属性)和方法(实例方法)称为实例成员 // 为构造函数传入参数,创建结构相同值不同的对象 // 构造函数创建的实例对象彼此独立互不影响function Person(name,age,gender…

尖叫测试(scream test)

尖叫 尖叫测试是一种在产品或服务器关闭之前进行的压力测试方法。它的主要目的是模拟在高负载情况下系统的极限性能,并验证系统是否能够在压力情况下正常工作。 尖叫测试可以通过模拟大量用户同时访问系统或服务器来制造高负载环境。 测试人员可以使用各种工具和技…

市面上的ipad国产触控笔怎么样?好用的电容笔合集

而对那些把IPAD当作学习工具的人而言,这个Apple Pencil绝对是不可或缺的。然而,苹果版本的Pencil却是昂贵得让许多人望而却步。因此,最佳方法是选择一个平替的电容笔。我是从几年前开始用IPAD的,也是一个数码爱好者,近…

负载均衡的知识点

目录 1.负载均衡是什么 2.负载均衡的分类 客户端负载均衡: 服务端负载均衡: 软件实现:根据OSI模型可以分为四层负载均衡和七层负载均衡 硬件实现: 附1:客户端和服务端: 附2:OSI…

win10系统切换到macOS,开发环境与软件资源,目录清单

文章目录 1、生活2、文书3、开发4、算法5、美术6、科研 1、因为考研自习室或学校图书馆,随身携带游戏本(全能本)受限于不插电源就不续航和掉性能,以及风扇噪音非常大,以及发热很烫等问题。 2、所以想考虑给主力机换个…

提升1Panel的安全性:在Armbian上设置个性化SSL证书

文章目录 基础知识OpenSSLOpenSSL常用命令SSL证书与DDoS攻击SSL证书常见办法机构 armbian安装OpenSSLOpenSSL下载和安装生成私钥和证书生成私钥和证书提示信息的解释challenge password 可能遇到的错误 安装完成后的思考完整的操作演示 基础知识 OpenSSL OpenSSL是一个开源的…

C++学习第二十二天(deque容器及案例)

deque容器的基本概念 功能:双端数组,可以对头端进行删除和插入 deque和vector的区别 vector对于头部的插入删除效率低,数据量越大,效率越低 deque相对而言,对头部的插入和删除比较块 vector访问元素的速度比deque快&am…