(013)每日SQL学习:日期的各种计算
1.确定两个日期之间的工作日天数
--确定两个日期之间的工作日天数
with x0 as
(select to_date('2018-01-01','yyyy-mm-dd') as 日期 from dual
union all
select to_date('2018-01-15','yyyy-mm-dd') as 日期 from dual ),
x1 as --日期并列显示
(select min (日期) 开始日期,max(日期) 结束日期 from x0 ),
x2 as --日期之间的天数
(select 结束日期-开始日期+1 as 天数 ,开始日期,结束日期 from x1),
x3 as --构造一个从开始日期到结束日期的日期集合 (枚举日期)
(select to_char(开始日期+level-1,'DY') as 枚举日期 from x2 connect by level <=天数)
--统计日期
select sum(case when 枚举日期 in ('sat','sun')then 0 else 1 end ) as 工作日 from x3
2.计算一年周内各日期次数
--计算一年周内各日期次数
with x0 as
(select to_date('2018-01-01','yyyy-mm-dd') as 年初 from dual ),
x1 as
(select 年初,add_months(年初,12) as 下年年初 from x0),
x2 as
(select 年初,下年年初,下年年初-年初 as 天数 from x1),
x3 as
(select 年初+level-1 as 日期 from x2 connect by level <=天数 ),
x4 as
(select 日期,to_char(日期,'DAY') AS 星期 FROM X3) SELECT 星期,count(*) from x4 group by 星期
3.确定当前记录和下一条记录之间的相差天数
with x0 as
(select '1'as name ,to_date('2018-01-01','yyyy-mm-dd') as 日期 from dual
union all
select '2' as name,to_date('2018-01-15','yyyy-mm-dd') as 日期 from dual
union all
select '3' as name, to_date('2018-01-26','yyyy-mm-dd') as 日期 from dual ) select
name ,日期,next_d,(next_d-日期) as 相差天数
from (
select name ,日期,
lead(日期,1,null) over (order by 日期) as next_d
from x0
)
lead 函数见下文说明:(014)每日SQL学习:oracle下lag和lead分析函数
4.日期操作
select 列,行
from (
select
to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') 日期,
to_char(sysdate,'hh24') as 时,
to_char(sysdate,'mm') as 分,
to_char(sysdate,'ss') as 秒,
to_char(sysdate,'dd') as 日,
to_char(sysdate,'mm') as 月,
to_char(sysdate,'yyyy') as 年,
to_char(sysdate,'ddd') as 年内第几天,
to_char(trunc(sysdate,'day'),'yyyy-mm-dd') as 周初,
to_char(trunc(sysdate,'mm'),'yyyy-mm-dd') as 月初,
to_char(last_day(sysdate),'yyyy-mm-dd') as 月末,
to_char(add_months(trunc(sysdate,'mm'),1),'yyyy-mm-dd') as 下月初,
to_char(trunc(sysdate,'yy'),'yyyy-mm-dd') as 年初,
to_char(sysdate,'day') as 周几,
to_char(sysdate,'month') as 月份
from dual
)
unpivot( 行 for 列 in (日期,时,分,秒,日,月,年,年内第几天,周初,月初,月末,下月初,年初,周几,月份 ))
结果:

说明:注意此处last_day用法,该函数返回的时分秒和参数中的一样。既:如果参数日期为 2018-03-03 12:25:34 那么last_day(参数)结果是:2018-03-31 12:25:34
用此函数做区间条件时要小心数据被过滤了。一个月的数据最好采用add_months(trunc(sysdate,'mm'),1)为区间条件。
5.时间间隔(interval)
select
interval '50' month as "month",
interval '99' day as "day",
interval '80' hour as "hour",
interval '90' minute as "minute",
interval '3.99' second as "second",
interval '123-3' year(3) to month as "year to month"
from dual
结果:
month day hour minute second year to month
+04-02 +99 00:00:00 +03 08:00:00 +00 01:30:00 +00 00:00:03.990000 +123-03
6.提取timestamp年月日函数(extract)
select systimestamp ,
extract(year from systimestamp) as "year",
extract(month from systimestamp) as "month",
extract(day from systimestamp) as"day",
extract(hour from systimestamp) as "hour",
extract(minute from systimestamp) as "minute",
extract(second from systimestamp) as "second"
from dual
说明:
1.extract不能提取date类型中的时分秒。
2.extract可以提取interval中的信息,而to_char()不行。
3.timestamp与date类型的区别:
1.比date类型更为精确,秒后面加了.0000
2.求日期差时 date类型的差值是天数,timestamp的差值的具体到时分秒。例如:
select to_date('2018-7-28 03:12:00','yyyy-mm-dd hh24:mi:ss')-sysdate from dual;
结果:148.422858796296
select to_timestamp('2018-7-28 03:12:00','yyyy-mm-dd hh24:mi:ss')-systimestamp from dual
结果:+000000148 10:08:54.229000000
7.周的计算
with x0 as
(select trunc(sysdate)-trunc(sysdate,'yy') as d from dual ),
x1 as
( select trunc(trunc(sysdate,'yy')+level-1) as d1 from x0 connect by level<=d)
select d1,to_char(d1,'d') as dd,
to_char(d1,'day') as 星期,
next_day(d1,1) as 下个周日,
to_char(d1,'iw')第几周
from x1

8.确定某月的第一个和最后一个“”周内某天“的日期
select next_day(trunc(sysdate,'mm')-1,2) as 第一个星期一 ,
next_day(last_day(trunc(sysdate))-7,2) as 最后一个星期一
from dual
说明:next_day是“下一”的意思。每个星期第一天是周日,第二天才是周一。
9.创建本月日历
with x0 as
/*取出当前日期月初月末*/
(select trunc(sysdate,'mm') as first_day,last_day(trunc(sysdate,'mm')) as last_day from dual),
x1 as
/*这个月的天数*/
(select (last_day-first_day+1) as day_num ,first_day,last_day from x0 ),
x2 as
/*枚举本月每一天*/
(select first_day+level-1 as dd from x1 connect by level<=day_num),
x3 as
/*提取周信息*/
(select
to_char(dd,'iw') 第几周 ,
to_char(dd,'dd') as 日期,
to_number(to_char(dd,'d')) 周几
from x2
)
select min(case when 周几 ='2' then 日期 end ) as 周一,
max(case when 周几 ='3' then 日期 end) as 周二,
max(case when 周几 ='4' then 日期 end) as 周三,
max(case when 周几 ='5' then 日期 end) as 周四,
max(case when 周几 ='6' then 日期 end) as 周五,
max(case when 周几 ='7' then 日期 end) as 周六,
max(case when 周几 ='1' then 日期 end) as 周日
from x3 group by 第几周 order by 第几周
结果:

10.创建本年日历
with x0 as
/*取出当前日期月初月末*/
(select trunc(sysdate,'yy') as first_day,add_months(trunc(sysdate,'yy'),12)-1 as last_day from dual),
x1 as
/*这个月的天数*/
(select (last_day-first_day+1) as day_num ,first_day,last_day from x0 ),
x2 as
/*枚举本月每一天*/
(select first_day+level-1 as dd from x1 connect by level<=day_num),
x3 as
/*提取周信息*/
(select
to_char(dd,'iw') 第几周 ,
to_char(dd,'mm') 月,
dd as 日期,
to_number(to_char(dd,'d')) 周几
from x2
),
x4 as
/*修正十二月份53周变为第一周的问题*/
( select case when 月='12' and 第几周='01' then '53' else 第几周 end 第几周,日期, 周几 from x3 )
select min(case when 周几 ='2' then 日期 end ) as 周一,
max(case when 周几 ='3' then 日期 end) as 周二,
max(case when 周几 ='4' then 日期 end) as 周三,
max(case when 周几 ='5' then 日期 end) as 周四,
max(case when 周几 ='6' then 日期 end) as 周五,
max(case when 周几 ='7' then 日期 end) as 周六,
max(case when 周几 ='1' then 日期 end) as 周日
from x4 group by 第几周 order by 第几周
11.确定指定年份季度的开始日期和结束日期
select
sn as 季度,
(sn-1)*3+1 as 月份,
add_months(to_date(年,'yyyy'),(sn-1)*3) as 开始日期,
add_months(to_date(年,'yyyy'),sn*3)-1 as 结束日期
from (select '2013' as 年,level as sn from dual connect by level<=4)
结果:
季度 月份 开始日期 结束日期
1 1 1 2013/3/1 2013/5/31
2 2 4 2013/6/1 2013/8/31
3 3 7 2013/9/1 2013/11/30
4 4 10 2013/12/1 2014/2/28
(013)每日SQL学习:日期的各种计算的更多相关文章
- (011)每日SQL学习:SQL开窗函数
开窗函数:在开窗函数出现之前存在着很多用 SQL 语句很难解决的问题,很多都要通过复杂的相关子查询或者存储过程来完成.为了解决这些问题,在 2003 年 ISO SQL 标准加入了开窗函数,开窗函数的 ...
- (009)每日SQL学习:Oracle各个键说明(转)
原文地址:http://www.agiledata.org/essays/keys.html 本文概述关系数据库中为表指定主键的策略.主要关注于何时使用自然键或者代理键的问题.有些人会告诉你应该总是使 ...
- (004)每日SQL学习:物化视图之二
一. 物化视图概述 Oracle的物化视图是包括一个查询结果的数据库对像,它是远程数据的的本地副本,或者用来生成基于数据表求和的汇总表.物化视图存储基于远程表的数据,也可以称为快照. 物化视图可 ...
- (003)每日SQL学习:普通视图和物化视图
关于这一点一直就是很懵懂的状态,今天特意网上查了一下资料,以下摘抄网上比较好的答案.以作记录. 普通视图和物化视图的区别答曰:普通视图和物化视图根本就不是一个东西,说区别都是硬拼到一起的,首先明白基本 ...
- (014)每日SQL学习:oracle下lag和lead分析函数
/*语法*/ lag(exp_str,offset,defval) over() Lead(exp_str,offset,defval) over() --exp_str要取的列 --offset取偏 ...
- (012)每日SQL学习:TO_CHAR(DATE,FORMAT)
SYSDATE 2009-6-16 15:25:10 TRUNC(SYSDATE) 2009-6-16 TO_CHAR(SYSDATE,'YYYYMMDD') 20090616 到日 TO_CHAR( ...
- (010)每日SQL学习:按字母顺序排列字符串
需求:一串字母'ADFGH',需要按照顺序来显示:A D F G H 第一步:先把字符串拆分 with test as( select 'ADFGH' as a from dual ) select ...
- (008)每日SQL学习:Oracle Not Exists 及 Not In 使用
今天遇到一个问题,not in 查询失效,我以为是穿越了,仔细查了点资料,原来理解有误! select value from temp_a a where a.id between 1 and 100 ...
- (007)每日SQL学习:将字符和数字分离
with aa as ( select 'sad10' as data from dual union all select 'datf20' as data from dual union all ...
随机推荐
- 6. 抹平差异,统一类型转换服务ConversionService
目录 ✍前言 版本约定 ✍正文 ConverterRegistry ConversionService ConfigurableConversionService GenericConversionS ...
- 配置简单的拦截器java中
springMVC.xml文件中==== <!-- 拦截器 --> <mvc:interceptors> <mvc:interceptor> <mvc:map ...
- java 去掉html标签 使用正则表达式删除HTML标签。
import java.util.regex.Matcher; import java.util.regex.Pattern; public class HTMLSpirit{ public stat ...
- codeforces 1438D,思路非常非常巧妙的构造题
大家好,欢迎来到codeforces专题. 今天选择的问题是contest1438的D题,全场通过人数为1325人.一般在codeforces当中千人通过的题难度都不算太高,但是这题有点例外,虽然没有 ...
- idea中maven的安装与配置
说明:类似maven安装和配置的帖子在网上有很多,本人也有做过参照,但是有些帖子的步骤跳跃性比较大,故此,本人整理了一下,给大家做个参考. 一.下载安装 一般都是在官网进行下载 https://mav ...
- Java8接口的默认方法
项目实战 实现上图接口的实现类有很多,其中有些实现类已经在生成环境了,现在需要新增几个实现类,都需要有回调方法,所以在接口中添加了一个回调的默认方法,如果使用接口的普通方法就得改所有实现了接口的实现类 ...
- spark知识点_datasources
来自官网DataFrames.DataSets.SQL,即sparkSQL模块. 通过dataframe接口,sparkSQL支持多种数据源的操作.可以把dataframe注册为临时视图,也可以通过关 ...
- 十八般武艺玩转GaussDB(DWS)性能调优:SQL改写
摘要:本文将系统介绍在GaussDB(DWS)系统中影响性能的坏味道SQL及SQL模式,帮助大家能够从原理层面尽快识别这些坏味道SQL,在调优过程中及时发现问题,进行整改. 数据库的应用中,充斥着坏味 ...
- Ts有限状态机
ts版本的有限状态机 最近做小游戏要做切换人物状态,花点时间写了一个有限状态机,使用语言为Ts,也可改成自己的语言 按照目前的逻辑,这个可以继续横向扩展,某些做流程管理 先上预览图 Fsm:状态机类 ...
- 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs
前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...