从日期开始获取期限

时间:2016-06-09 04:56:16

标签: sql oracle11g

我需要一个oracle SQL查询来从日期列获取句点。

以下是条件:

  1. 如果一年中的第一天,即1月1日是星期六,那么第1个时段从上周六的那个星期开始

  2. 每个时期有28天。 28天后,下一个时段开始并继续。

  3. 例如,如果日期是2016年2月2日,则期间应为2。

    谢谢, 米兰

2 个答案:

答案 0 :(得分:0)

此查询将在SQL Server中运行 在以下查询中插入列名和表名。这是一个非常粗略的修复。它需要优化,但也许您可以将它作为基础:

select 
case when 
 datepart(dw, DATEADD(yy, DATEDIFF(yy,0,DATECOLUMNHERE), 0)) = 6 then datepart(dy, DATECOLUMNHERE)/28
else datediff(d,DATEADD(d, -2 - (DATEPART(dw, DATEADD(yy, DATEDIFF(yy,0,DATECOLUMNHERE), 0)) + @@DATEFIRST - 2) % 7,DATEADD(yy, DATEDIFF(yy,0,DATECOLUMNHERE), 0)),DATECOLUMNHERE)/28
 from TABLENAMEHERE

在第一次检查时,我们不会考虑@@ Datefirst

查询

 select 
  case when FirstDayOfYear = 6(Saturday) then return (number of days in date column) / 28
 else 
   (number of days betwen (last saturday before 01-Jan) and date column) /28

答案 1 :(得分:0)

这是Sql server的另一个

CREATE Table #YourTable (
    DateColumn date NOT NULL
);

INSERT INTO #YourTable
VALUES ('20160202')
      ,('20150128')
      ,('20140128')
      ,('20130128')
      ,('20120128')
      ,('20110128')
      ,('20100128')
      ,('20090128')
      ,('20080128');

SELECT *          
FROM #YourTable AS YourTable
     CROSS APPLY (
         VALUES (DATEADD(day, 1 - DATEPART(dayofyear, YourTable.DateColumn), YourTable.DateColumn))
     ) AS CA1(FirstDayOfYear)
     -- Periods starts saturday before first friday of year
     CROSS APPLY (
          VALUES ((20 - @@DATEFIRST - DATEPART(weekday, FirstDayOfYear)) % 7)
     ) AS CA2(DaysUntilFriday)
     CROSS APPLY (
         VALUES (DATEADD(day, DaysUntilFriday - 6, FirstDayOfYear))
     ) AS CA3(PeriodStartDate)
     CROSS APPLY (
         VALUES(DATEDIFF(day, PeriodStartDate, YourTable.DateColumn))
     ) AS CA4(DaysSincePeriodStart)
     CROSS APPLY (
         VALUES ((DaysSincePeriodStart + 0) / 28 + 1)
     ) AS CA5(PeriodNumber)

输出

| DateColumn | FirstDayOfYear | DaysUntilFriday | PeriodStartDate | DaysSincePeriodStart | PeriodNumber | 
|------------|----------------|-----------------|-----------------|----------------------|--------------| 
| 2016-02-02 | 2016-01-01     | 0               | 2015-12-26      | 38                   | 2            | 
| 2015-01-28 | 2015-01-01     | 1               | 2014-12-27      | 32                   | 2            | 
| 2014-01-28 | 2014-01-01     | 2               | 2013-12-28      | 31                   | 2            | 
| 2013-01-28 | 2013-01-01     | 3               | 2012-12-29      | 30                   | 2            | 
| 2012-01-28 | 2012-01-01     | 5               | 2011-12-31      | 28                   | 2            | 
| 2011-01-28 | 2011-01-01     | 6               | 2011-01-01      | 27                   | 1            | 
| 2010-01-28 | 2010-01-01     | 0               | 2009-12-26      | 33                   | 2            | 
| 2009-01-28 | 2009-01-01     | 1               | 2008-12-27      | 32                   | 2            | 
| 2008-01-28 | 2008-01-01     | 3               | 2007-12-29      | 30                   | 2            |