16

SQL Server 2005 を使用して、日付範囲に存在するすべての日付を取得する必要があります

4

7 に答える 7

56

どうぞ:

DECLARE @DateFrom smalldatetime, @DateTo smalldatetime;
SET @DateFrom='20000101';
SET @DateTo='20081231';
-------------------------------
WITH T(date)
AS
( 
SELECT @DateFrom 
UNION ALL
SELECT DateAdd(day,1,T.date) FROM T WHERE T.date < @DateTo
)
SELECT date FROM T OPTION (MAXRECURSION 32767);
于 2008-11-07T12:09:06.587 に答える
8

テーブルに日付があり、単に2つの日付の間でそれらを選択したい場合は、使用できます

select * from yourTable where yourDate between date1 and date2

何もないところから日付を生成したい場合は、ループを使用するか、一時テーブルに日付を入力してから選択することができます。

于 2008-11-07T09:23:56.213 に答える
1

日付生成のOracleバージョンは次のとおりです。

SELECT TO_DATE ('01-OCT-2008') + ROWNUM - 1 g_date
  FROM all_objects
 WHERE ROWNUM <= 15

all_objects の代わりに、必要な範囲をカバーするのに十分な行を持つ任意のテーブルにすることができます。

于 2008-11-07T16:09:43.480 に答える
0

もう少し複雑ですが、おそらくより柔軟なのは、連続した数字のセットを含むテーブルを利用することです。これにより、間隔が異なる複数の日付範囲が可能になります。

/* holds a sequential set of number ie 0 to max */
/* where max is the total number of rows expected */
declare @Numbers table ( Number int  )

declare @max int 
declare @cnt int

set @cnt = 0
/* this value could be limited if you knew the total rows expected */
set @max = 999 

/* we are building the NUMBERS table on the fly */
/* but this could be a proper table in the database */
/* created at the point of first deployment */
while (@cnt <= @max)
begin
      insert into @Numbers select @cnt
      set @cnt = @cnt + 1
end

/* EXAMPLE of creating dates with different intervals */

declare @DateRanges table ( 
   StartDateTime datetime, EndDateTime datetime, Interval int )

/* example set of date ranges */
insert into @DateRanges
select '01 Jan 2009', '10 Jan 2009', 1 /* 1 day interval */
union select '01 Feb 2009', '10 Feb 2009', 2 /* 2 day interval */

/* heres the important bit generate the dates */
select
      StartDateTime
from
(
      select
            d.StartDateTime as RangeStart,
            d.EndDateTime as RangeEnd,
            dateadd(DAY, d.Interval * n.Number, d.StartDateTime) as StartDateTime
      from 
            @DateRanges d, @Numbers n
) as dates
where
      StartDateTime between RangeStart and RangeEnd
order by StartDateTime

私は実際にこれのバリエーションを使用して、日付をタイムスロットに分割します(さまざまな間隔で、通常は5分です)。私の@numbersテーブルには最大288が含まれています。これは、24時間で使用できる5分のスロットの総数です。

/* EXAMPLE of creating times with different intervals */

delete from @DateRanges 

/* example set of date ranges */
insert into @DateRanges
select '01 Jan 2009 09:00:00', '01 Jan 2009 12:00:00', 30 /* 30 minutes interval */
union select '02 Feb 2009 09:00:00', '02 Feb 2009 10:00:00', 5 /* 5 minutes interval */

/* heres the import bit generate the times */
select
      StartDateTime,
      EndDateTime
from
(
      select
            d.StartDateTime as RangeStart,
            d.EndDateTime as RangeEnd,
            dateadd(MINUTE, d.Interval * n.Number, d.StartDateTime) as StartDateTime,
            dateadd(MINUTE, d.Interval * (n.Number + 1) , StartDateTime) as EndDateTime
      from 
            @DateRanges d, @Numbers n
) as dates
where
      StartDateTime >= RangeStart and EndDateTime <= RangeEnd
order by StartDateTime
于 2009-08-13T09:38:52.330 に答える
-2

2 つの日付の間のデータベースに存在するすべての日付 (つまり、2008 年の第 3 四半期に顧客が注文した日付) を取得する場合は、次のように記述します。

select distinct(orderPlacedDate) 
from orders 
where orderPlacedDate between '2008-07-01' and 2008-09-30' 
order by orderPlacedDate
于 2008-11-07T12:22:02.097 に答える
-3

日付の範囲を生成するには、テーブル値関数を記述できます。これは、データ ウェアハウスの日付ディメンションを作成する関数です。特別なものを削除することで、かなり簡単に適応させることができます。

編集:これは、日付ディメンション階層なしです。

if object_id ('ods.uf_DateHierarchy') is not null
    drop function ods.uf_DateHierarchy
go

create function ods.uf_DateHierarchy (
       @DateFrom datetime
      ,@DateTo   datetime
) returns @DateHierarchy table (
        DateKey           datetime
) as begin
    declare @today           datetime  
    set @today = @Datefrom

    while @today <= @DateTo begin
        insert @DateHierarchy (DateKey) values (@today)
        set @today = dateadd (dd, 1, @today)
    end

    return
end

go
于 2008-11-07T11:07:13.760 に答える