主頁 > 資料庫 > 選讀SQL經典實體筆記04_日期運算(上)

選讀SQL經典實體筆記04_日期運算(上)

2023-07-11 08:15:10 資料庫

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

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/556989.html

標籤:SQL Server

上一篇:嵌入式資料庫簡介

下一篇:返回列表

標籤雲
其他(162362) Python(38273) JavaScript(25530) Java(18294) C(15239) 區塊鏈(8275) C#(7972) AI(7469) 爪哇(7425) MySQL(7294) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5876) 数组(5741) R(5409) Linux(5347) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4615) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2438) ASP.NET(2404) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) HtmlCss(1995) .NET技术(1986) 功能(1967) Web開發(1951) C++(1942) python-3.x(1918) 弹簧靴(1913) xml(1889) PostgreSQL(1882) .NETCore(1863) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • GPU虛擬機創建時間深度優化

    **?桔妹導讀:**GPU虛擬機實體創建速度慢是公有云面臨的普遍問題,由于通常情況下創建虛擬機屬于低頻操作而未引起業界的重視,實際生產中還是存在對GPU實體創建時間有苛刻要求的業務場景。本文將介紹滴滴云在解決該問題時的思路、方法、并展示最終的優化成果。 從公有云服務商那里購買過虛擬主機的資深用戶,一 ......

    uj5u.com 2020-09-10 06:09:13 more
  • 可編程網卡芯片在滴滴云網路的應用實踐

    **?桔妹導讀:**隨著云規模不斷擴大以及業務層面對延遲、帶寬的要求越來越高,采用DPDK 加速網路報文處理的方式在橫向縱向擴展都出現了局限性。可編程芯片成為業界熱點。本文主要講述了可編程網卡芯片在滴滴云網路中的應用實踐,遇到的問題、帶來的收益以及開源社區貢獻。 #1. 資料中心面臨的問題 隨著滴滴 ......

    uj5u.com 2020-09-10 06:10:21 more
  • 滴滴資料通道服務演進之路

    **?桔妹導讀:**滴滴資料通道引擎承載著全公司的資料同步,為下游實時和離線場景提供了必不可少的源資料。隨著任務量的不斷增加,資料通道的整體架構也隨之發生改變。本文介紹了滴滴資料通道的發展歷程,遇到的問題以及今后的規劃。 #1. 背景 資料,對于任何一家互聯網公司來說都是非常重要的資產,公司的大資料 ......

    uj5u.com 2020-09-10 06:11:05 more
  • 滴滴AI Labs斬獲國際機器翻譯大賽中譯英方向世界第三

    **桔妹導讀:**深耕人工智能領域,致力于探索AI讓出行更美好的滴滴AI Labs再次斬獲國際大獎,這次獲獎的專案是什么呢?一起來看看詳細報道吧! 近日,由國際計算語言學協會ACL(The Association for Computational Linguistics)舉辦的世界最具影響力的機器 ......

    uj5u.com 2020-09-10 06:11:29 more
  • MPP (Massively Parallel Processing)大規模并行處理

    1、什么是mpp? MPP (Massively Parallel Processing),即大規模并行處理,在資料庫非共享集群中,每個節點都有獨立的磁盤存盤系統和記憶體系統,業務資料根據資料庫模型和應用特點劃分到各個節點上,每臺資料節點通過專用網路或者商業通用網路互相連接,彼此協同計算,作為整體提供 ......

    uj5u.com 2020-09-10 06:11:41 more
  • 滴滴資料倉庫指標體系建設實踐

    **桔妹導讀:**指標體系是什么?如何使用OSM模型和AARRR模型搭建指標體系?如何統一流程、規范化、工具化管理指標體系?本文會對建設的方法論結合滴滴資料指標體系建設實踐進行解答分析。 #1. 什么是指標體系 ##1.1 指標體系定義 指標體系是將零散單點的具有相互聯系的指標,系統化的組織起來,通 ......

    uj5u.com 2020-09-10 06:12:52 more
  • 單表千萬行資料庫 LIKE 搜索優化手記

    我們經常在資料庫中使用 LIKE 運算子來完成對資料的模糊搜索,LIKE 運算子用于在 WHERE 子句中搜索列中的指定模式。 如果需要查找客戶表中所有姓氏是“張”的資料,可以使用下面的 SQL 陳述句: SELECT * FROM Customer WHERE Name LIKE '張%' 如果需要 ......

    uj5u.com 2020-09-10 06:13:25 more
  • 滴滴Ceph分布式存盤系統優化之鎖優化

    **桔妹導讀:**Ceph是國際知名的開源分布式存盤系統,在工業界和學術界都有著重要的影響。Ceph的架構和演算法設計發表在國際系統領域頂級會議OSDI、SOSP、SC等上。Ceph社區得到Red Hat、SUSE、Intel等大公司的大力支持。Ceph是國際云計算領域應用最廣泛的開源分布式存盤系統, ......

    uj5u.com 2020-09-10 06:14:51 more
  • es~通過ElasticsearchTemplate進行聚合~嵌套聚合

    之前寫過《es~通過ElasticsearchTemplate進行聚合操作》的文章,這一次主要寫一個嵌套的聚合,例如先對sex集合,再對desc聚合,最后再對age求和,共三層嵌套。 Aggregations的部分特性類似于SQL語言中的group by,avg,sum等函式,Aggregation ......

    uj5u.com 2020-09-10 06:14:59 more
  • 爬蟲日志監控 -- Elastc Stack(ELK)部署

    傻瓜式部署,只需替換IP與用戶 導讀: 現ELK四大組件分別為:Elasticsearch(核心)、logstash(處理)、filebeat(采集)、kibana(可視化) 下載均在https://www.elastic.co/cn/downloads/下tar包,各組件版本最好一致,配合fdm會 ......

    uj5u.com 2020-09-10 06:15:05 more
最新发布
  • 選讀SQL經典實體筆記04_日期運算(上)

    ![](https://img2023.cnblogs.com/blog/3076680/202307/3076680-20230710222226297-1155867049.png) # 1. 年月日加減法 ## 1.1. DB2 ### 1.1.1. sql ```sql select hir ......

    uj5u.com 2023-07-11 08:15:10 more
  • 嵌入式資料庫簡介

    ### 什么是嵌入式資料庫? 嵌入式資料庫主要有兩種定義: - 用于嵌入式系統(如移動設備或消費電子產品)的資料庫。嵌入式資料庫需要占用空間小,并在記憶體和CPU能力有限的環境中提供足夠的性能。 - 嵌入到應用程式中的資料庫--這意味著應用程式不與資料庫服務器通信,而是內置資料庫組件。這種型別的資料庫 ......

    uj5u.com 2023-07-11 08:14:53 more
  • 麒麟V10作業系統安裝達夢DM8常見問題分享

    一、麒麟V10關閉防火墻 kylinV10系統或linux系統關閉啟動防火墻開啟防火墻并設定開機自啟啟動: systemctl start firewalld關閉: systemctl stop firewalld查看狀態: systemctl status firewalld開機禁用 : syst ......

    uj5u.com 2023-07-11 08:14:43 more
  • TiDB簡述及TiKV的資料結構與存盤

    本文主要從TiDB的各類組件為起點,了解它的基礎架構,并重點分析它在存盤架構方面的設計,探究其如何組織資料,Table中的每行記錄是如何在記憶體和磁盤中進行存盤的。 ......

    uj5u.com 2023-07-11 08:14:05 more
  • MySQL之InnoDB存盤結構

    InnoDB存盤引擎最早由Innobase Oy公司開發(屬第三方存盤引擎)。從MySQL 5.5版本開始作為表的默認存盤引擎。該存盤引擎是第一個完整支持ACID事務的MySQL存盤引擎,特點是行鎖設計、支持MVCC、支持外鍵、提供一致性非鎖定讀,非常適合OLTP場景的應用使用。目前也是應用最廣泛的... ......

    uj5u.com 2023-07-11 08:14:00 more
  • 一站式運維管家 ChengYing 主機接入原理決議

    之前的文章中,我們已經為大家介紹了 [ChengYing](https://github.com/DTStack/chengying) 的安裝原理、產品包制作、產品線部署等內容,本篇將和大家介紹一個困擾許多開發者的內容——ChengYing 主機接入。幫助所有對 ChengYing 感興趣的開發者更 ......

    uj5u.com 2023-07-11 08:13:23 more
  • 06、etcd 寫請求執行流程

    > 本篇內容主要來源于自己學習的視頻,如有侵權,請聯系洗掉,謝謝。 上一節我們學習了 etcd 讀請求執行流程,這一節,我們來學習 etcd 寫請求執行流程。 ### 1、etcd寫請求概覽 **etcd 一個寫請求執行流程又是怎樣的呢?** ``` sh etcdctl put hello wor ......

    uj5u.com 2023-07-10 08:18:46 more
  • 華為云河圖KooMap:夯實數字孿生底座,點燃燎原星火

    摘要:7月8日,華為開發者大會2023(Cloud)華為云河圖KooMap技術分論壇在東莞溪村順利舉辦。 7月8日,華為開發者大會2023(Cloud)華為云河圖KooMap技術分論壇在東莞溪村順利舉辦。本次分論壇以“夯實數字孿生底座,點燃燎原星火”為主題,邀請了來自華為的技術專家和產學研領域的行業 ......

    uj5u.com 2023-07-10 08:18:25 more
  • 資料交換不失控:華為云EDS,讓你的資料你做主

    摘要:華為云EDS在“可信、可控、可證”的框架基礎上進行資料空間的關鍵設計,打造資料可控交換的全堆疊能力。 數字社會,每時每刻都有海量資料產生,資料也逐漸從生產程序的附屬產物,逐漸成為數字經濟的關鍵生產要素。作為生產要素,資料只有流通起來才能產生大規模的經濟價值。 資料流通發展三部曲 資料在企業中的流 ......

    uj5u.com 2023-07-10 08:18:09 more
  • 銀河麒麟V10安裝達夢資料庫DM8

    1. 系統準備 查看系統資訊:cat /proc/version查看 CPU:lscpu 或cat /proc/cpuinfo查看記憶體:free -m查看磁盤空間:cat /proc/meminfo或df -h查看tmp空間(至少1.5G以上):df -h /tmp發現tmp空間太小(安裝DM8需要 ......

    uj5u.com 2023-07-10 08:17:11 more