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

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

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)
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,204评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,091评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,548评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,657评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,689评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,554评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,302评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,216评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,661评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,851评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,977评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,697评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,306评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,898评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,019评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,138评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,927评论 2 355

推荐阅读更多精彩内容