
select SUNDAY,SATURDAY from
(select
sundaythe_week,decode(sign(sundaythe_day-saturdaythe_day),-1,sundaythe_day,sundaythe_day-7)
sunday,saturdaythe_day saturday from
(select to_char(wwm,'WW') the_week,to_char(wwm,'D') the_daynum,wwm the_day from (select
trunc(sysdate, 'MM')+rownum-1 as wwm from user_objects where rownum < 366) where
to_char(wwm,'D')=1 ) sunday,
(select to_char(wwm,'WW') the_week,to_char(wwm,'D') the_daynum,wwm the_day from (select
trunc(sysdate, 'MM')+rownum-1 as wwm from user_objects where rownum < 366) where
to_char(wwm,'D')=7 ) saturday
where sundaythe_week=saturdaythe_week) a
where the_week=32
具体你的周是怎么确定的?
select to_char(to_date('20090101','yyyymmdd')+rownum+7,'iw') column_name from dual connect by rownum<=to_date('20111201','yyyymmdd')-to_date('20090101','yyyymmdd')
思路:
1 用以上语句生成月份表(具体参数自己调一下)
2 然后和你要统计的表进行外连接,不存在的就是null ,用nvl(null,0) 表示统计数据为0
参考:
oracle中如何获取中文的周数
默认oracle中,通过
select to_char(sysdate,'ww'),to_char(sysdate,'yyyy') from dual可以获取到ORACLE的周数,但是这个给的周数以及年数是按照每年的第一天为第一周的算法,和我们平常的计算方法不一样,平常的算法是按照星期来计算的,经过查找资料,to_char提供了新的参数:如下:
select to_char(sysdate,'iw'),to_char(sysdate,'iyyy') from dual
这个是按照IW = Week of year (1-52 or 1-53) based on the ISO standard
通常的ww是按照WW = Week of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year
比如2008年12月31日,按照不同的查询方式的查询结果如下:
select to_char(to_date('2008-12,31', 'yyyy-mm-dd'), 'ww') ww,
to_char(to_date('2008-12,31', 'yyyy-mm-dd'), 'yyyy') yyyy
from dual
查询结果
序号 周 年度
1 53 2008
select to_char(to_date('2008-12,31', 'yyyy-mm-dd'), 'iw') iw,
to_char(to_date('2008-12,31', 'yyyy-mm-dd'), 'iyyy') iyyy
from dual
思路:先获取年份和周数,然后获取该年第一天是周几, 然后获取该年第一周的开始时间,然后加上(周数-1)7,例子
with tmp as(select '1502' as a
from dual
union all
select '1503' as a
from dual
union all
select '1402' as a
from dual
union all
select '0701' as a from dual)
select a,
to_char(years - w + (weeks - 1) 7,'yyyymmdd') as begin_week
from (select a,
years,
weeks,
--因为oracle 的周是从周日开始,星期一是2
to_char(years, 'd') - 2 as w
from (select a,
to_date('20' || substr(a, 1, 2) || '-01-01',
'yyyy-mm-dd') as years,
substr(a, -2, 2) as weeks
from tmp))
其实最简单的理解 是每月的第三个周五必然在 15号到21号之间 1号是周五时15号就是第三个周五,如果1号是周六 那21号就是第三个周五。
所以
SELECT CASE
WHEN TO_CHAR(TRUNC(SYSDATE,'MON')+15,'DAY')= '星期五' THEN TRUNC(SYSDATE,'MON')+15
WHEN TO_CHAR(TRUNC(SYSDATE,'MON')+16,'DAY')= '星期五' THEN TRUNC(SYSDATE,'MON')+16
WHEN TO_CHAR(TRUNC(SYSDATE,'MON')+17,'DAY')= '星期五' THEN TRUNC(SYSDATE,'MON')+17
WHEN TO_CHAR(TRUNC(SYSDATE,'MON')+18,'DAY')= '星期五' THEN TRUNC(SYSDATE,'MON')+18
WHEN TO_CHAR(TRUNC(SYSDATE,'MON')+19,'DAY')= '星期五' THEN TRUNC(SYSDATE,'MON')+19
WHEN TO_CHAR(TRUNC(SYSDATE,'MON')+20,'DAY')= '星期五' THEN TRUNC(SYSDATE,'MON')+20
WHEN TO_CHAR(TRUNC(SYSDATE,'MON')+21,'DAY')= '星期五' THEN TRUNC(SYSDATE,'MON')+21
END A
FROM
DUAL
这样虽然麻烦 但是不依靠任何表。而且计算简单,容易理解。
select
from (select to_date('2013-01-01', 'yyyy-mm-dd') + rownum - 1 days
from dba_objects) a
where extract(year from days) = 2013
and to_char(days, 'WW') = 22
and to_char(days, 'day') in ('星期一', '星期二', '星期三', '星期四', '星期五')
年份:2013,第几周:22
使用sql语句查询日期在一周内的数据 select from ShopOrder where datediff(week,ordTime,getdate()-1)=0 //查询当天日期在一周年的数据 select from ShopOrder where datediff(day,ordTime,getdate()-1)=0 //查询当天的所有数据 SELECT F
以上就是关于oracle中 我给出一个周数 用什么函数可以取出这周的开始日期和结束日期全部的内容,包括:oracle中 我给出一个周数 用什么函数可以取出这周的开始日期和结束日期、oracle,给定一段时间,按时间(周)统计数据记录条数、oracle 中 如何将年周(例如1502)转换为年月日(20150105)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)