How to group by week of the year dates or day dates that start on Sundays in oracle 10?

白昼怎懂夜的黑 提交于 2019-12-12 01:39:43

问题


Given a table:

day_date (form of yyyy-mm-dd) column2 column3

I want to group the data by weeks, and the start of each week goes from Sunday to Saturday, but I want to display the day_date.

I have looked around all over, and I'm rusty on my SQL.

Thanks!


回答1:


Assuming that day_date is a datetime field and that you want to display the start of the week as the grouping field, try a query of the form:

select TRUNC(day_date)-TO_NUMBER(TO_CHAR(day_date,'D'))+1 week_start_date,
       MAX(column2), MAX(column3)
from yourtable
group by TRUNC(day_date)-TO_NUMBER(TO_CHAR(day_date,'D'))+1;

If day_date is a text field representing a date in the format yyyy-mm-dd, you will need to replace day_date in the query above with to_date(day_date,'yyyy-mm-dd') throughout.



来源:https://stackoverflow.com/questions/3524643/how-to-group-by-week-of-the-year-dates-or-day-dates-that-start-on-sundays-in-ora

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!