简体   繁体   English

在Teradata(SQL)中,根据给定的一年中的一周来查找一周的开始和结束日期

[英]Finding Start and end Date for a week given the week of a year in Teradata (SQL)

I am doing data conversion and i need to get the start and end dates of a given week. 我正在进行数据转换,我需要获取给定星期的开始和结束日期。

My source has the date stored in a single int column 'year_wk' . 我的源将日期存储在单个int'year_wk'

Examples for this column would be 本专栏的示例为

201201 - meaning the first week in 2012 201005 - meaning the fifth week in 2010 201201-表示2012年的第一周201005-表示2010年的第五周

I would like to get start and end dates for a given week in a standard mm/dd/yyyy format. 我想以标准的mm/dd/yyyy格式获取给定星期的开始和结束日期。

So 201201 would give me something like 1/1/2012 and 1/7/2012. 所以201201会给我类似2012年1月1日和2012年1月7日的信息。

It won't be pretty and may not be terribly efficient but you can leverage the view SYS_CALENDAR.CALENDAR . 它不会很漂亮,也可能效率不高,但是您可以利用SYS_CALENDAR.CALENDAR视图。 I purposely use SELECT * in the example so you can see all of the attributes available to you with the SYS_CALENDAR.CALENDAR view that is a default database on all Teradata installations: 我故意在示例中使用SELECT * ,以便通过SYS_CALENDAR.CALENDAR视图查看所有可用的属性,该视图是所有Teradata安装中的默认数据库:

SELECT *
  FROM Sys_Calendar."CALENDAR"
 WHERE Year_of_Calendar = CAST(SUBSTRING('201201' FROM 1 FOR 4) AS INTEGER)
   AND Week_of_Year = CAST(SUBSTRING('201201' FROM 5 FOR 2) AS INTEGER)
UNION
SELECT *
  FROM  Sys_Calendar."CALENDAR"
 WHERE Year_of_Calendar = CAST(SUBSTRING('201005' FROM 1 FOR 4) AS INTEGER)
   AND Week_of_Year = CAST(SUBSTRING('201005' FROM 5 FOR 2) AS INTEGER);

I'm not familiar with Teradata but here's the Oracle ISO week query with week start and end dates for the whole year of 2013. This should work in any SQL. 我对Teradata并不熟悉,但是这是Oracle ISO周查询,其中包含2013年全年的起始日期和结束日期。这在任何SQL中均适用。 For non-iso week change week formats to WW - forgot to rename mydate to start_date in query: 对于非等周更改,将周格式更改为WW-忘记在查询中将mydate重命名为start_date:

SELECT mydate                               -- 1/1/2013 --
     , TRUNC(mydate, 'iw')                  wk_starts  
     , TRUNC(mydate, 'iw') + 7 - 1/86400    wk_ends
     , TO_NUMBER (TO_CHAR (mydate, 'IW'))   ISO_wk#_iw  
     , TRUNC(mydate, 'w')                   week_start_date
     , TRUNC(mydate, 'w') + 7 - 1/86400 AS  week_end_date
 FROM
 (
  SELECT TRUNC(SYSDATE, 'YEAR')-1 + LEVEL AS mydate  -- 11/1/2013 --
    FROM dual
  CONNECT BY LEVEL <= 
  (-- First day of next year - first day of curr year --
   SELECT TRUNC(ADD_MONTHS (SYSDATE, 12), 'Y')-TRUNC(SYSDATE, 'Y') "Num of Days"   
     FROM dual
  )
)
/

SQL>

START_DATE  WK_STARTS   WK_ENDS                ISO_WK#
------------------------------------------------------------------
1/1/2013    12/31/2012  1/6/2013 11:59:59 PM    1
1/2/2013    12/31/2012  1/6/2013 11:59:59 PM    1
...
...
1/7/2013    1/7/2013    1/13/2013 11:59:59 PM   2
1/8/2013    1/7/2013    1/13/2013 11:59:59 PM   2
...
...
1/14/2013   1/14/2013   1/20/2013 11:59:59 PM   3
1/15/2013   1/14/2013   1/20/2013 11:59:59 PM   3

Week numbers calendar: http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php 周数日历: http : //www.epochconverter.com/date-and-time/weeknumbers-by-year.php

Here is some SQL code that will build a table of starting and ending dates for a given year.. 这是一些SQL代码,这些代码将构建给定年份的开始日期和结束日期表。

If you build this table for all years in question, the rest should be accomplished via a JOIN statement 如果您针对所有有问题的年份构建此表,则其余的应通过JOIN语句完成

-- Create weekrange table
Create table Weekrange
(YearNo INT,
 WeekNo INT,
 StartDay DATETIME,
 EndDay DATETIME)
-- Populate first row
insert into WeekRange
select 2013,1, DateAdd(d,-DATEPART(dw,'1/1/2013'),'1/1/2013')+1 as FirstDay,null    -- Compute first day
update Weekrange set EndDay = DATEADD(D,6,StartDay)                                 -- Determine last day

-- Add next 52 rows
declare @id INT
set @id=2
while @id <=53
begin
    insert into Weekrange (YearNo,WeekNo) values (2013,@id)
    set @id=@id+1
    update Weekrange 
    set StartDay = DATEADD(D,1,xx.EndDay),
        EndDay = DATEADD(D,7,xx.EndDay)
    from 
    (select * from WeekRange ) xx
    where xx.weekno=weekrange.WeekNo-1
end 


select * from Weekrange 

Trying to improve my SQL, This would be a simple soultion: 尝试改善我的SQL,这将是一个简单的灵魂:

 SELECT CALENDAR_DATE,DAY_OF_WEEK
 FROM SYS_CALENDAR.CALENDAR TERADATA
 WHERE SUBSTR(CAST((CAST(CALENDAR_DATE AS DATE FORMAT 'MMDDYYYY')) AS VARCHAR(8) ),5,4) = '2010'
AND WEEK_OF_YEAR='05' 
AND DAY_OF_WEEK IN (1,7)
ORDER BY CALENDAR_DATE
SELECT TD_WEEK_END (date) - 6

the TD_WEEK_END (date) is the end of the weekend. TD_WEEK_END (date)是周末结束。 Simply subtract 6 from date to have the beginning of the week. 只需从日期中减去6,即可获得一周的开始时间。

NOTE: This answer set will always return Sunday since Sunday is the first day of the week. 注意:由于星期日是一周的第一天,因此此答案集将始终返回星期日。

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

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