sql把一段时间分割成周,月,季度,年的时间段
--本周
select TO_CHAR(CREATE_DATE ,'yyyy-MM-dd')as NEW_DATE ,
TO_CHAR(trunc(CREATE_DATE, 'd')+ 1,'yyyy-MM-dd') as START_DATE ,
TO_CHAR(trunc(CREATE_DATE, 'd')+ 7,'yyyy-MM-dd') as END_DATE
from BG_PROJECT_INFO;
--本月
select TO_CHAR(CREATE_DATE ,'yyyy-MM-dd')as NEW_DATE ,
TO_CHAR(trunc(CREATE_DATE, 'mm'),'yyyy-MM-dd') as START_DATE ,
TO_CHAR(last_day(trunc(CREATE_DATE, 'mm')),'yyyy-MM-dd') as END_DATE
from BG_PROJECT_INFO;
--本季
select TO_CHAR(CREATE_DATE ,'yyyy-MM-dd')as NEW_DATE ,
TO_CHAR(trunc(CREATE_DATE, 'Q'),'yyyy-MM-dd') as START_DATE ,
TO_CHAR(add_months(trunc(CREATE_DATE, 'Q'),3)-1,'yyyy-MM-dd') as END_DATE
from BG_PROJECT_INFO;
--本年
select TO_CHAR(CREATE_DATE ,'yyyy-MM-dd')as NEW_DATE ,
TO_CHAR(trunc(CREATE_DATE, 'yyyy'),'yyyy-MM-dd') as START_DATE ,
TO_CHAR(add_months(trunc(CREATE_DATE, 'yyyy'),12)-1,'yyyy-MM-dd') as END_DATE
from BG_PROJECT_INFO; select to_char(t.CREATE_DATE, 'YYYY-MM-DD') as 天, t.PLAN_HOURS
from BG_PROJECT_INFO t ---按天统计
select to_char(t.CREATE_DATE+15/24, 'YYYY-MM-DD') as 天 ,sum(t.PLAN_HOURS) as 数量
from BG_PROJECT_INFO t
WHERE
1=1
group by to_char(t.CREATE_DATE+15/24, 'YYYY-MM-DD')
ORDER by 天 NULLS LAST; --按周统计
select to_char(next_day(t.CREATE_DATE+15/24 - 7,2),'YYYY-MM-DD') AS 周,sum(t.PLAN_HOURS) as 数量
from BG_PROJECT_INFO t
WHERE
1=1
group by to_char(next_day(t.CREATE_DATE+15/24 - 7,2),'YYYY-MM-DD')
ORDER BY 周; --按月统计
select to_char(t.CREATE_DATE,'YYYY-MM') as 月份,sum(t.PLAN_HOURS) as 数量
from BG_PROJECT_INFO t
WHERE
1=1
GROUP BY
to_char(t.CREATE_DATE,'YYYY-MM')
ORDER BY 月份; -- 按季统计
select to_char(t.CREATE_DATE,'q') 季度,sum(t.PLAN_HOURS) as 数量
from BG_PROJECT_INFO t
WHERE
1=1
group by to_char(t.CREATE_DATE,'q')
ORDER BY 季度 NULLS LAST; --按年统计
select to_char(t.CREATE_DATE,'yyyy') AS 年度,sum(t.PLAN_HOURS) as 数量
from BG_PROJECT_INFO t
WHERE
1=1
group by to_char(t.CREATE_DATE,'yyyy')
ORDER BY 年度; --按照周一到周日为一周算周的开始时间和结束时间(IW)自然周
WITH PARAMS AS (SELECT TRUNC(TO_DATE('2009-01-01','YYYY-MM-DD'),'YYYY') AS SD FROM DUAL)
SELECT LEVEL 周次,
DECODE(SIGN(5-DECODE(TO_CHAR(PM.SD,'D'),'','',TO_CHAR(PM.SD,'D'))),-1,
NEXT_DAY(PM.SD+(LEVEL-1)*7,2),NEXT_DAY(PM.SD+(LEVEL-1)*7-7,2))
当周第一天,
DECODE(SIGN(5-DECODE(TO_CHAR(PM.SD,'D'),'','',TO_CHAR(PM.SD,'D'))),-1,
NEXT_DAY(PM.SD+(LEVEL-1)*7,2),NEXT_DAY(PM.SD+(LEVEL-1)*7-7,2)) + 6
当周最后一天
FROM DUAL D
LEFT JOIN PARAMS PM ON 1=1
CONNECT BY LEVEL<=53
--按照周日到周六为一周算周的开始时间和结束时间(D)
SELECT LEVEL 周次,(TRUNC(TO_DATE('2011-01-01','YYYY-MM-DD'),'YYYY')-7) + (7-TO_CHAR(TRUNC(TO_DATE('2011-01-01','YYYY-MM-DD'),'YYYY'),'D')+1)+(LEVEL-1)*7 当周第一天,
(TRUNC(TO_DATE('2011-01-01','YYYY-MM-DD'),'YYYY')-7) + (7-TO_CHAR(TRUNC(TO_DATE('2011-01-01','YYYY-MM-DD'),'YYYY'),'D')+1)+(LEVEL-1)*7+6 当周最后一天
FROM DUAL CONNECT BY LEVEL<=53
--按照ORACLE标准(WW)
SELECT LEVEL 周次,TO_DATE('2013-01-01','YYYY-MM-DD')+(LEVEL-1)*7 当周第一天,
TO_DATE('2013-01-01','YYYY-MM-DD')+(LEVEL-1)*7+
DECODE((TO_CHAR(TO_DATE('2013-12-31','YYYY-MM-DD'),'DDD')-(LEVEL-1)*7),1,0,2,1,6) 当周最后一天
FROM DUAL CONNECT BY LEVEL<=53 WITH src
AS (SELECT TRUNC (DATE '2018-08-01') fromdate,
TRUNC (DATE '2019-09-01') todate
FROM DUAL),
dataset
AS ( SELECT LEVEL AS seq
FROM DUAL
CONNECT BY LEVEL < (SELECT todate - fromdate FROM src)),
datawithindices
AS (SELECT src.*,
dataset.*,
CASE TO_CHAR (fromdate + seq - 1, 'DY')
WHEN 'SAT' THEN 1
WHEN 'SUN' THEN 2
WHEN 'MON' THEN 3
WHEN 'TUE' THEN 4
WHEN 'WED' THEN 5
WHEN 'THU' THEN 6
WHEN 'FRI' THEN 7
END
indice
FROM src, dataset),
filt
AS (SELECT seq, fromdate + seq - 1 nowaday, indice FROM datawithindices),
alldata
AS (SELECT filt.*, CEIL (indice / 7) + FLOOR (seq / 7) weekno FROM filt)
SELECT 'WEEK '||weekno week_seq, begindate, enddate
FROM (SELECT weekno,
FIRST_VALUE (nowaday)
OVER (PARTITION BY weekno ORDER BY weekno)
begindate,
LAST_VALUE (nowaday)
OVER (PARTITION BY weekno ORDER BY weekno)
enddate,
ROW_NUMBER () OVER (PARTITION BY weekno ORDER BY weekno) rn
FROM alldata)
WHERE rn = 1
SQL ----周
-----周
SELECT NUMS ,
(case when TO_DATE(WEEK_START_DATE, 'yyyy-mm-dd') < TO_DATE('2018-08-01', 'yyyy-mm-dd') then '2018-08-01' else WEEK_START_DATE end) as WEEKS_START_DATE ,
(case when TO_DATE(WEEK_END_DATE, 'yyyy-mm-dd') > TO_DATE('2018-09-15', 'yyyy-mm-dd') then '2018-09-15' else WEEK_END_DATE end) as WEEK_END_DATE from (
SELECT ROWNUM as NUMS,WEEK_START_DATE,WEEK_END_DATE from(
select WEEK_START_DATE, WEEK_END_DATE from (
select
ROWNUM as NUM,
to_char(日期,'yyyy-mm-dd') DATE_ID,
to_char(日期,'yyyy') YEAR_ID,
to_char(日期,'q') QUARTERID_ID,
to_char(日期,'mm') MONTH_ID,
TO_CHAR(trunc(日期, 'd')+ ,'yyyy-MM-dd') as WEEK_START_DATE ,
TO_CHAR(trunc(日期, 'd')+ ,'yyyy-MM-dd') as WEEK_END_DATE,
TO_CHAR(trunc(日期, 'mm'),'yyyy-MM-dd') as MONTH_START_DATE ,
TO_CHAR(last_day(trunc(日期, 'mm')),'yyyy-MM-dd') as MONTH_END_DATE,
TO_CHAR(trunc(日期, 'Q'),'yyyy-MM-dd') as QUARTERID_START_DATE ,
TO_CHAR(add_months(trunc(日期, 'Q'),)-,'yyyy-MM-dd') as QUARTERID_END_DATE,
TO_CHAR(trunc(日期, 'yyyy'),'yyyy-MM-dd') as YEAR_START_DATE ,
TO_CHAR(add_months(trunc(日期, 'yyyy'),)-,'yyyy-MM-dd') as YEAR_END_DATE
from(
select to_date('2018-08-01','yyyy-mm-dd')+(rownum-) 日期
from ALL_OBJECTS
where rownum< and to_date('2018-08-01','yyyy-mm-dd')+(rownum-)<=to_date('2018-09-15','yyyy-mm-dd')
)
) WHERE
=
GROUP BY WEEK_START_DATE,WEEK_END_DATE
ORDER BY WEEK_START_DATE
)
)
SQL ----月
-----月
SELECT NUMS ,
(case when TO_DATE(MONTH_START_DATE, 'yyyy-mm-dd') < TO_DATE('2018-08-01', 'yyyy-mm-dd') then '2018-08-01' else MONTH_START_DATE end) as MONTH_START_DATE ,
(case when TO_DATE(MONTH_END_DATE, 'yyyy-mm-dd') > TO_DATE('2018-09-15', 'yyyy-mm-dd') then '2018-09-15' else MONTH_END_DATE end) as MONTH_END_DATE from (
SELECT ROWNUM as NUMS,MONTH_START_DATE,MONTH_END_DATE from(
select MONTH_START_DATE, MONTH_END_DATE from (
select
ROWNUM as NUM,
to_char(日期,'yyyy-mm-dd') DATE_ID,
to_char(日期,'yyyy') YEAR_ID,
to_char(日期,'q') QUARTERID_ID,
to_char(日期,'mm') MONTH_ID,
TO_CHAR(trunc(日期, 'd')+ ,'yyyy-MM-dd') as WEEK_START_DATE ,
TO_CHAR(trunc(日期, 'd')+ ,'yyyy-MM-dd') as WEEK_END_DATE,
TO_CHAR(trunc(日期, 'mm'),'yyyy-MM-dd') as MONTH_START_DATE ,
TO_CHAR(last_day(trunc(日期, 'mm')),'yyyy-MM-dd') as MONTH_END_DATE,
TO_CHAR(trunc(日期, 'Q'),'yyyy-MM-dd') as QUARTERID_START_DATE ,
TO_CHAR(add_months(trunc(日期, 'Q'),)-,'yyyy-MM-dd') as QUARTERID_END_DATE,
TO_CHAR(trunc(日期, 'yyyy'),'yyyy-MM-dd') as YEAR_START_DATE ,
TO_CHAR(add_months(trunc(日期, 'yyyy'),)-,'yyyy-MM-dd') as YEAR_END_DATE
from(
select to_date('2018-08-01','yyyy-mm-dd')+(rownum-) 日期
from ALL_OBJECTS
where rownum< and to_date('2018-08-01','yyyy-mm-dd')+(rownum-)<=to_date('2018-09-15','yyyy-mm-dd')
)
) WHERE
=
GROUP BY MONTH_START_DATE,MONTH_END_DATE
ORDER BY MONTH_START_DATE
)
)
SQL ----季节
SELECT NUMS ,
(case when TO_DATE(QUARTERID_START_DATE, 'yyyy-mm-dd') < TO_DATE('2018-08-01', 'yyyy-mm-dd') then '2018-08-01' else QUARTERID_START_DATE end) as QUARTERID_START_DATE ,
(case when TO_DATE(QUARTERID_END_DATE, 'yyyy-mm-dd') > TO_DATE('2019-09-15', 'yyyy-mm-dd') then '2019-09-15' else QUARTERID_END_DATE end) as QUARTERID_END_DATE from (
SELECT ROWNUM as NUMS,QUARTERID_START_DATE,QUARTERID_END_DATE from(
select QUARTERID_START_DATE, QUARTERID_END_DATE from (
select
ROWNUM as NUM,
to_char(日期,'yyyy-mm-dd') DATE_ID,
to_char(日期,'yyyy') YEAR_ID,
to_char(日期,'q') QUARTERID_ID,
to_char(日期,'mm') MONTH_ID,
TO_CHAR(trunc(日期, 'd')+ ,'yyyy-MM-dd') as WEEK_START_DATE ,
TO_CHAR(trunc(日期, 'd')+ ,'yyyy-MM-dd') as WEEK_END_DATE,
TO_CHAR(trunc(日期, 'mm'),'yyyy-MM-dd') as MONTH_START_DATE ,
TO_CHAR(last_day(trunc(日期, 'mm')),'yyyy-MM-dd') as MONTH_END_DATE,
TO_CHAR(trunc(日期, 'Q'),'yyyy-MM-dd') as QUARTERID_START_DATE ,
TO_CHAR(add_months(trunc(日期, 'Q'),)-,'yyyy-MM-dd') as QUARTERID_END_DATE,
TO_CHAR(trunc(日期, 'yyyy'),'yyyy-MM-dd') as YEAR_START_DATE ,
TO_CHAR(add_months(trunc(日期, 'yyyy'),)-,'yyyy-MM-dd') as YEAR_END_DATE
from(
select to_date('2018-08-01','yyyy-mm-dd')+(rownum-) 日期
from ALL_OBJECTS
where rownum< and to_date('2018-08-01','yyyy-mm-dd')+(rownum-)<=to_date('2019-09-15','yyyy-mm-dd')
)
) WHERE
=
GROUP BY QUARTERID_START_DATE,QUARTERID_END_DATE
ORDER BY QUARTERID_START_DATE
)
)
SQL ----年
-----年
SELECT NUMS ,
(case when TO_DATE(YEAR_START_DATE, 'yyyy-mm-dd') < TO_DATE('2018-08-01', 'yyyy-mm-dd') then '2018-08-01' else YEAR_START_DATE end) as YEAR_START_DATE ,
(case when TO_DATE(YEAR_END_DATE, 'yyyy-mm-dd') > TO_DATE('2019-09-15', 'yyyy-mm-dd') then '2019-09-15' else YEAR_END_DATE end) as YEAR_END_DATE from (
SELECT ROWNUM as NUMS,YEAR_START_DATE,YEAR_END_DATE from(
select YEAR_START_DATE, YEAR_END_DATE from (
select
ROWNUM as NUM,
to_char(日期,'yyyy-mm-dd') DATE_ID,
to_char(日期,'yyyy') YEAR_ID,
to_char(日期,'q') QUARTERID_ID,
to_char(日期,'mm') MONTH_ID,
TO_CHAR(trunc(日期, 'd')+ ,'yyyy-MM-dd') as WEEK_START_DATE ,
TO_CHAR(trunc(日期, 'd')+ ,'yyyy-MM-dd') as WEEK_END_DATE,
TO_CHAR(trunc(日期, 'mm'),'yyyy-MM-dd') as MONTH_START_DATE ,
TO_CHAR(last_day(trunc(日期, 'mm')),'yyyy-MM-dd') as MONTH_END_DATE,
TO_CHAR(trunc(日期, 'Q'),'yyyy-MM-dd') as QUARTERID_START_DATE ,
TO_CHAR(add_months(trunc(日期, 'Q'),)-,'yyyy-MM-dd') as QUARTERID_END_DATE,
TO_CHAR(trunc(日期, 'yyyy'),'yyyy-MM-dd') as YEAR_START_DATE ,
TO_CHAR(add_months(trunc(日期, 'yyyy'),)-,'yyyy-MM-dd') as YEAR_END_DATE
from(
select to_date('2018-08-01','yyyy-mm-dd')+(rownum-) 日期
from ALL_OBJECTS
where rownum< and to_date('2018-08-01','yyyy-mm-dd')+(rownum-)<=to_date('2019-09-15','yyyy-mm-dd')
)
) WHERE
=
GROUP BY YEAR_START_DATE,YEAR_END_DATE
ORDER BY YEAR_START_DATE
)
)
sql把一段时间分割成周,月,季度,年的时间段的更多相关文章
- PL/SQL Developer 一段时间后变慢,且导致数据库CPU100%的问题(转)
参考: 一段时间不用plsql developer之后重新使用会变得很慢 plsql developer连接数据库导致服务器cpu升高的案例 1.pl/sql dev 变慢的问题,建议设置如下 2. ...
- java把一段时间分成周,月,季度,年的时间段
package com.mq.test.activeMQ; import java.text.DateFormat; import java.text.ParseException; import j ...
- sql 循环某段时间的每一天
create table #t1( 日期 datetime) declare @stime datetime;declare @etime datetime set @stime ='2015-01- ...
- sql 通过某段时间求得改段时间内的工作时长,排除工作日
CREATE FUNCTION Fun_GetTotalHourBySomeTime(@TaskId NVARCHAR(30),@Bu_trupstartDate NVARCHAR(50),@Bu_t ...
- 如何在Sql Server中读取最近一段时间的记录,比如取最近3天的或最近3个月的记录。
如何在Sql Server中读取最近一段时间的记录,比如取最近3天的或最近3个月的记录. 主要用到DATEADD函数,下面是详细语句 取最近3天 select * from 表名where rq> ...
- SQL Server日期和时间的格式
在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...
- ylb:SQL Server中的时间函数
ylbtech-SQL Server:SQL Server-SQL Server中的时间函数 SQL Server中的时间函数. 1,SQL Server中的时间函数 返回顶部 1. 当前系统日期 ...
- js 将一大段时间均分为很多个小时间段
最近写项目,遇到一个将选中时间段平均分割为若干小段,然后根据小段时间在数据库查询求均值的问题,后台大哥犯懒,非说后台做不了,让我分好传给他ヾ(. ̄□ ̄)ツ゜゜゜好气呦,但还要保持微笑,我就是这么懂礼貌 ...
- Sql Server中日期时间格式化为字符串输出
在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...
随机推荐
- 【原创】Linux基础之重定向stdout/stderr
启动进程后查看日志(stdout和stderr) 1 nohup+tail # nohup $cmd > /path/to/file 2>&1 & # tail -f /p ...
- 什么是Web和www
什么是Web和www 通过之前课程的学习,我们已经对计算机网络有了一些了解,这里我主要想说一个点,也是计算机网络中一个很容易被误解的概念,就是什么是Web,它和HTTP.HTML.Internet.i ...
- HTML5中新增加的结构元素、网页元素和全局属性
HTML5新增的结构元素(新增的都是块元素,独占一行) 1) header 定义了文档的头部区域 <header> <h1>网站标题<h1> </header ...
- 1-10000以内的完数(js)
//1-10000以内的完数 //完数:因子之和相加等于这个数 //例如:6的因子为1,2,3:1+2+3=6 // 6 // 28 // 496 // 8128 let sum = 0, i, j; ...
- haproxy实现ssl套接字加密
概述 如果你的应用使用SSL证书,则需要决定如何在负载均衡器上使用它们. 单服务器的简单配置通常是考虑客户端SSL连接如何被接收请求的服务器解码.由于负载均衡器处在客户端和更多服务器之间,SSL连接解 ...
- SpringBoot面试题
详见:https://www.cnblogs.com/3xmq/p/springboot.html https://blog.csdn.net/yuzongtao/article/details/84 ...
- Python3多线程爬取meizitu的图片
python环境:python3 运行环境:win10和linux都可以,其他系统没测 1 安装依赖 pip install requests pip install lxml pip install ...
- Ubuntu18.04安装 NVIDIA驱动
Ubuntu18.04安装 NVIDIA驱动 参考自博客:https://blog.csdn.net/jsjason1/article/details/88086904 我确定这篇文章是否很有必要,我 ...
- 基础简单DP
状态比较容易表示,转移方程比较好想,问题比较基本常见 递推.背包.LIS(最长递增序列),LCS(最长公共子序列) HDU 2048 数塔 由上往下推 状态数太多(100!) 可以由下往上推: d ...
- 数据驱动——ddt
1: pip3 install ddt 2: @ddt 装饰 @data((2,3),(4,5)) 支持列表,元祖,字典 @unpack 解压数据 1 import unittest 2 from ...