简体   繁体   English

如何从预定义的日期表(Oracle SQL)中获取每周的最后一个日期和第一个日期

[英]How to get last and first date of every week from predefined date table (oracle SQL)

I have Table D_date in which all dates of a year, week number,quarter number etc attributes are defined. 我有表D_date ,其中定义了一年,星期几,季度号等所有日期的属性。 I just want to get first and last date of every week of year 2015. 我只想获取2015年每个星期的第一个和最后一个日期。

Sample D_date tabe attached. 附有示例D_date选项。 在此处输入图片说明

It is simple min/max if I understand you right 如果我理解正确,这就是简单的最小/最大

SELECT calendar_year_nbr, week, min(actual_date),max(actual_date)
FROM D_date
GROUP BY calendar_year_nbr, week

I just want to get first and last date of every week of year 2015. 我只想获取2015年每个星期的第一个和最后一个日期。

Since you have precomputed values already stored in the table, you could directly use MIN and MAX as aggregate functions along with GROUP BY . 由于您已经在表中存储了预先计算的值,因此可以将MINMAXGROUP BY一起直接用作聚合函数。

For example, 例如,

SELECT MIN(actual_date) min_date,
      MAX(actual_date) max_date,
      calendar_week_nbr
FROM d_date
WHERE calendar_year_nbr = 2015
GROUP BY calendar_week_nbr
ORDER BY min_date;

Another way is to use ROWNUM() OVER() analytic function. 另一种方法是使用ROWNUM()OVER()分析函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM