Oracle數(shù)據(jù)庫如何獲取當前自然周,當前周的起始和結束日期
Oracle獲取當前自然周,當前周的起始和結束日期
SELECT to_char(sysdate,'iw') from dual; --本周是第幾個自然周 SELECT to_char(sysdate,'yyyy') into v_sbzq_nf from dual; -- 當前年份 SELECT to_char(TRUNC(TO_DATE(to_char(sysdate,'yyyy-MM-dd'),'yyyy-MM-dd'),'IW'),'yyyy-MM-dd') FROM DUAL;--本周的起始時間(本周周一日期) SELECT to_char(TRUNC(TO_DATE(to_char(sysdate,'yyyy-MM-dd'),'YYYY-MM-DD'),'IW') + 6,'yyyy-MM-dd') FROM DUAL;--本周的結束時間(本周周日日期)
下面是使用存儲過程向數(shù)據(jù)庫中插入一條數(shù)據(jù)
數(shù)據(jù)格式 : 上報周期 起始時間 結束時間
xxxx年第xx周 xx.xx xx.xx
存儲過程如下:
create or replace procedure PRC_T_SJSB_ZYGYPJG is v_sbzq_zs varchar2(20);--上報周期周數(shù) v_sbzq_nf varchar2(10); --上報周期年份 v_start varchar2(10); --起始月份 v_end varchar2(10); --結束月份 v_sbzq varchar2(20); --上報周期 begin SELECT to_char(sysdate,'iw') into v_sbzq_zs from dual; --本周是第幾個自然周 SELECT to_char(sysdate,'yyyy') into v_sbzq_nf from dual; SELECT to_char(TRUNC(TO_DATE(to_char(sysdate,'yyyy-MM-dd'),'yyyy-MM-dd'),'IW'),'yyyy-MM-dd') into v_start FROM DUAL;--本周的起始時間(本周周一日期) SELECT to_char(TRUNC(TO_DATE(to_char(sysdate,'yyyy-MM-dd'),'YYYY-MM-DD'),'IW') + 6,'yyyy-MM-dd') into v_end FROM DUAL;--本周的結束時間(本周周日日期) v_start:=substr(v_start,6); v_end :=substr(v_end,6); v_start :=replace(v_start,'/','.'); v_start :=replace(v_start,'-','.'); v_end :=replace(v_end, '/','.'); v_end :=replace(v_end, '-','.'); v_sbzq :=v_sbzq_nf||'年第'||v_sbzq_zs||'周'; insert into T_SJSB_ZYGYPJG (sbzq,sbzt,startdate,enddate) values(v_sbzq,'0',v_start,v_end); commit; end PRC_T_SJSB_ZYGYPJG;
Oracle獲取自然周數(shù)
在Oracle中,Mysql中以及Hive中,SQL實現(xiàn)同樣的功能有時候可能要遵循不同的語法結構,尤其在日期操作方面區(qū)別較為明顯。接下來,將Oracle中常用的周期統(tǒng)計梳理出來。
1. 按天統(tǒng)計
Oracle中通過to_char()函數(shù)來操作日期變量,通過其中的格式參數(shù)配置輸出日期的格式。
格式參數(shù)值為’yyyymmdd’時,將日期統(tǒng)一轉(zhuǎn)換為yyyymmdd(年月日)的方式輸出。
select to_char(created_time,'yyyymmdd') as day,count(mobile_no) from table where to_char(created_time,'yyyymmdd') >= 20181201 group by to_char(created_time,'yyyymmdd') order by min(created_time) asc
輸出的結果如下所示:
2. 按自然周統(tǒng)計
to_char()函數(shù)的格式參數(shù)值為’iw’時,表示按自然周方式輸出日期在全年中的周數(shù)排序值,自然周即日歷上顯示的周排列結果。
如果只輸出周數(shù),不便于排查數(shù)據(jù),所以按自然周進行聚合時,最好能把該自然周的起始日期或結束日期顯示出來,使結果一目了然。
select to_char(created_time,'iw') as week, min(created_time), count(mobile_no) from table where to_char(created_time,'yyyymmdd') >= 20181201 group by to_char(created_time,'iw') order by min(created_time) asc
結果如下所示,其中week表示周數(shù)。通過MIN(created_time)可以展示出每個自然周的起始日期:
3. 按月統(tǒng)計
to_char()函數(shù)的格式參數(shù)值為’yyyymm’時可輸出格式為yyyymm(年月)的月份統(tǒng)計結果。
select to_char(created_time,'yyyymm') as Month, count(mobile_no) from table where to_char(created_time,'yyyymm') >= 20181201 group by to_char(created_time,'yyyymm')
其結果如下所示:
4. 按季統(tǒng)計
to_char()的格式參數(shù)值為’q’,可實現(xiàn)按季度輸出統(tǒng)計結果。
select to_char(created_time,'q') as q,count(mobile_no) from table where to_char(created_time,'yyyymmdd') >= 20180101 group by to_char(created_time,'q') order by min(created_time) asc
其結果如下所示,Q表示季度。
5. 按年統(tǒng)計
to_char()函數(shù)的參數(shù)值為’yyyy’時可以實現(xiàn)按年輸出統(tǒng)計結果。
select to_char(created_time,'yyyy') as y, count(mobile_no) from table where to_char(created_time,'yyyy') >= 2016 group by to_char(created_time,'yyyy') order by y asc
其輸出結果如下所示:
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Win7 64環(huán)境下Oracle10g 64位版本安裝教程
這篇文章主要為大家詳細介紹了Win7 64環(huán)境下Oracle10g 64位版本安裝教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03Oracle23ai 新特性IF [NOT] EXISTS語法支持的使用
IF [NOT] EXISTS語法用于抑制因給定對象的存在或不存在而引發(fā)的潛在錯誤,允許您編寫冪等DDL腳本,本文主要介紹了Oracle23ai 新特性IF [NOT] EXISTS語法支持的使用,感興趣的可以了解一下2024-08-08Oracle實現(xiàn)透明數(shù)據(jù)加密的代碼示例
透明數(shù)據(jù)加密(TDE)是一種用于保護數(shù)據(jù)庫中靜態(tài)數(shù)據(jù)的加密技術,TDE通過自動加密數(shù)據(jù)庫文件和日志文件,確保數(shù)據(jù)在磁盤上是加密的,從而防止未經(jīng)授權的訪問,以下以 Microsoft SQL Server 為例,實現(xiàn) TDE 的步驟和代碼示例,需要的朋友可以參考下2024-09-09