简体   繁体   中英

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. I just want to get first and last date of every week of year 2015.

Sample D_date tabe attached. 在此处输入图片说明

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.

Since you have precomputed values already stored in the table, you could directly use MIN and MAX as aggregate functions along with GROUP 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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