简体   繁体   English

创建自定义周字段 SQL -- Oracle 12c

[英]Create Custom Week Of Field SQL -- Oracle 12c

This is for a query in Oracle 12c这是针对 Oracle 12c 中的查询

I'm trying to create a "Week Of" field that will use the date field present in the record to assign that record a Week Of date that reflects the start of a week.我正在尝试创建一个“Week Of”字段,该字段将使用记录中存在的日期字段为该记录分配一个反映一周开始的 Week Of 日期。 I want the start of weeks to be Sundays and the end of weeks to be Saturdays.我希望星期的开始是星期日,星期的结束是星期六。

To give an example of what I mean:举例说明我的意思:

metric_date        metric        value        week_of
01-oct-20          sales         45           27-sep-20
05-oct-20          sales         15           04-oct-20

Where the week of field is always the most recent sunday date.字段的周始终是最近的星期日日期。 I have tried to modify this SQL Server code to work in Oracle, as it was written for a similar purpose:我试图修改此 SQL Server 代码以在 Oracle 中工作,因为它是为类似目的而编写的:

SELECT DATEADD(DAY, -DATEDIFF(DAY, 0, [Order Date]) % 7, [Order Date]) AS [Week of]
FROM dbo.your_table_name

Here is my attempt at replicating the code in Oracle:这是我在 Oracle 中复制代码的尝试:

SELECT distinct
metric_date,
mod(-(to_date('01-jan-1900','dd-mon-yyyy') - trunc(metric_date)),7) + metric_date as week_of

FROM dbo.table

But it does not return the results I want.但它没有返回我想要的结果。 Are there any obvious differences you can see that I am not accounting for in my replicated code?您是否可以看到我在复制代码中没有考虑到的任何明显差异?

You could use next_day() :你可以使用next_day()

select t.*, next_day(metric_date - 7, 'sunday')
from mytable t

Note that the second argument to next_day() is language-dependent.请注意, next_day()的第二个参数取决于语言。 If your database is configured in another language than English, use the local translation of Sunday.如果您的数据库配置为英语以外的其他语言,请使用 Sunday 的本地翻译。

You can use TRUNC with the IW option (which is independent of the language) to truncate the date to Monday and then shift the values by 1 day:您可以使用带有IW选项(独立于语言)的TRUNC将日期截断为星期一,然后将值移动 1 天:

SELECT metric_date,
       TRUNC( metric_date + INTERVAL '1' DAY, 'IW' ) - INTERVAL '1' DAY
         AS sunday_of_week
FROM   table_name

Which, for the sample data:其中,对于样本数据:

CREATE TABLE table_name ( metric_date ) AS
SELECT DATE '2020-10-01' + LEVEL - 1
FROM   DUAL
CONNECT BY LEVEL <= 14;

Outputs:输出:

\nMETRIC_DATE | METRIC_DATE | SUNDAY_OF_WEEK SUNDAY_OF_WEEK\n:---------- | :---------- | :------------- :-------------\n01-OCT-20 | 01-OCT-20 | 27-SEP-20 27-SEP-20     \n02-OCT-20 | 02-OCT-20 | 27-SEP-20 27-SEP-20     \n03-OCT-20 | 03-OCT-20 | 27-SEP-20 27-SEP-20     \n04-OCT-20 | 04-OCT-20 | 04-OCT-20 04-OCT-20     \n05-OCT-20 | 05-OCT-20 | 04-OCT-20 04-OCT-20     \n06-OCT-20 | 06-OCT-20 | 04-OCT-20 04-OCT-20     \n07-OCT-20 | 07-OCT-20 | 04-OCT-20 04-OCT-20     \n08-OCT-20 | 08-OCT-20 | 04-OCT-20 04-OCT-20     \n09-OCT-20 | 09-OCT-20 | 04-OCT-20 04-OCT-20     \n10-OCT-20 | 20 年 10 月 10 日 | 04-OCT-20 04-OCT-20     \n11-OCT-20 | 20 年 10 月 11 日 | 11-OCT-20 20 年 10 月 11 日     \n12-OCT-20 | 20 年 10 月 12 日 | 11-OCT-20 20 年 10 月 11 日     \n13-OCT-20 | 20 年 10 月 13 日 | 11-OCT-20 20 年 10 月 11 日     \n14-OCT-20 | 20 年 10 月 14 日 | 11-OCT-20 20 年 10 月 11 日     \n

db<>fiddle here db<> 在这里摆弄

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

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