创建 365 天视图

Create View with 365 days(创建 365 天视图)
本文介绍了创建 365 天视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建包含一年中所有天数的 View.view 应填写从 1 月 1 日到 12 月 31 日的日期.我如何在 Oracle 中执行此操作?

How to Create a View with all days in year. view should fill with dates from JAN-01 to Dec-31. How can I do this in Oracle ?

如果当前年份有 365 天,view 应该有 365 行带日期.如果当前年份有 366 天,view 应该有 366 行带日期.我希望 view 有一个 DATE 类型的列.

If current year have 365 days,view should have 365 rows with dates. if current year have 366 days,view should have 366 rows with dates. I want the view to have a single column of type DATE.

推荐答案

这个简单的视图可以做到:

This simple view will do it:

create or replace view year_days as
select trunc(sysdate, 'YYYY') + (level-1) as the_day
from dual
connect by level <= to_number(to_char(last_day(add_months(trunc(sysdate, 'YYYY'),11)), 'DDD'))
/

像这样:

SQL> select * from year_days;

THE_DAY
---------
01-JAN-11
02-JAN-11
03-JAN-11
04-JAN-11
05-JAN-11
06-JAN-11
07-JAN-11
08-JAN-11
09-JAN-11
10-JAN-11
11-JAN-11

...

20-DEC-11
21-DEC-11
22-DEC-11
23-DEC-11
24-DEC-11
25-DEC-11
26-DEC-11
27-DEC-11
28-DEC-11
29-DEC-11
30-DEC-11
31-DEC-11

365 rows selected.

SQL> 

日期是通过应用几个 Oracle 日期函数生成的:

The date is generated by applying several Oracle date functions:

  • trunc(sysdate, 'yyyy') 为我们提供当年的一月一日
  • add_months(x, 11) 给我们十二月的第一天
  • last_day(x) 给我们十二月三十日
  • to_char(x, 'DDD') 为我们提供了 12 月 31 日的数字,今年是 365,明年是 366.
  • 最后一个数字提供了行生成器的上限CONNECT BY LEVEL <= X
  • trunc(sysdate, 'yyyy') gives us the first of January for the current year
  • add_months(x, 11) gives us the first of December
  • last_day(x) gives us the thirty-first of December
  • to_char(x, 'DDD') gives us the number of the thirty-first of December, 365 this year and 366 next.
  • This last figure provides the upper bound for the row generator CONNECT BY LEVEL <= X

这篇关于创建 365 天视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

SQL to Generate Periodic Snapshots from Transactions Table(用于从事务表生成定期快照的SQL)
MyBatis support for multiple databases(MyBatis支持多个数据库)
Oracle 12c SQL: Missing column Headers in result(Oracle 12c SQL:结果中缺少列标题)
SQL query to find the number of customers who shopped for 3 consecutive days in month of January 2020(查询2020年1月连续购物3天的客户数量)
How to get top 10 data weekly (This week, Previous week, Last month, 2 months ago, 3 month ago)(如何每周获取前十大数据(本周、前一周、上个月、2个月前、3个月前))
Select the latest record for an Id per day - Oracle pl sql(选择每天ID的最新记录-Oracle pl SQL)