2

イベントのIDとを含むテーブルがあります。Date各行は 1 つの日付です。連続する日付範囲を決定し、出力を統合して表示しようとしていますID,StartDate,EndDate

ID      Date
200236  2011-01-02 00:00:00.000
200236  2011-01-03 00:00:00.000
200236  2011-01-05 00:00:00.000
200236  2011-01-06 00:00:00.000
200236  2011-01-07 00:00:00.000
200236  2011-01-08 00:00:00.000
200236  2011-01-09 00:00:00.000
200236  2011-01-10 00:00:00.000
200236  2011-01-11 00:00:00.000
200236  2011-01-12 00:00:00.000
200236  2011-01-13 00:00:00.000
200236  2011-01-15 00:00:00.000
200236  2011-01-16 00:00:00.000
200236  2011-01-17 00:00:00.000

出力は次のようになります。

ID       StartDate    EndDate
200236   2011-01-02   2011-01-03
200236   2011-01-05   2011-01-13
200236   2011-01-15   2011-01-17

SQL Server 2000 でこれを処理する方法について何か考えはありますか?

4

5 に答える 5

1

SQL Server 2008 でこれと同様のことを行ったところです。次の翻訳は SQL Server 2000 で機能すると思います。

-- Create table variable
DECLARE @StartTable TABLE
(
  rowid INT IDENTITY(1,1) NOT NULL,
  userid int,
  startDate date
)

Insert Into @StartTable(userid, startDate)
--This finds the start dates by finding unmatched values
SELECT t1.ID, t1.[Date]
FROM Example As t1
LEFT OUTER JOIN Example As t2 ON t1.ID=t2.ID 
   And DateAdd(day, 1, t2.[Date]) = t1.[Date]
WHERE t2.[Date] Is NULL
ORDER BY t1.ID, t1.[Date]

-- Create table variable
DECLARE @EndTable TABLE
(
  rowid INT IDENTITY(1,1) NOT NULL,
  userid int,
  endDate date
)

Insert Into @EndTable(userid, endDate)
--This finds the end dates by getting unmatched values 
SELECT t1.ID, t1.[Date]
FROM Example As t1
LEFT OUTER JOIN Example As t2 ON t1.ID=t2.ID
   And DateAdd(day, -1, t2.[Date]) = t1.[Date]
WHERE t2.[Date] IS NULL
ORDER BY t1.ID, t1.[Date]

Select eT.userid, startDate, endDate 
From @EndTable eT
INNER JOIN @StartTable sT On eT.userid = sT.userid 
AND eT.rowid = sT.rowid;

ご覧のとおり、[Date] 列の日付の直前または直後の日付でテーブルを自己結合することにより、開始用と終了用の 2 つのテーブル変数を作成しました。これは、開始テーブルに対して前の日付を持たない (したがって、これらは期間の最初になる) レコードと、次の日付がない (したがって、これらは最後の日付になる) レコードのみを選択していることを意味します。ピリオド) を終了テーブルに使用します。

これらがテーブル変数に挿入されると、ID 列のために順番に番号が付けられます。次に、2 つのテーブル変数を結合します。順序付けされているため、開始日と終了日は常に適切に一致する必要があります。

この解決策は、1 日に 1 つの ID につき多くて 1 つのレコードしか持たず、時間ではなく日数にのみ関心があるため、私にとってはうまくいきます。カーソルまたはループ。それがあなたにとってもうまくいくことを願っています。

于 2011-05-09T12:10:36.600 に答える
1
SELECT ...
FROM   ...
WHERE  date_column BETWEEN '2011-01-02' AND '2011-01-15'

多分?参照

または、日付が <= 現在の日付である MAX を使用してサブクエリを実行し、次のレコードをリンクすることもできます。

SELECT id, date, (SELECT MAX(date) FROM mytable WHERE date <= mytable.date) AS nextDate
FROM   mytable

または使用:

SELECT TOP 1 date
FROM         mytable
WHERE        date <= mytable.date AND id <> mytable.id
ORDER BY     date

サブクエリとして、現在のレコードの次の日付を取得します。

于 2011-05-05T16:57:54.017 に答える
0

実行できるアプローチの1つは、シーケンスの次の日付を示すフィールドを追加することです。(現在のテーブルに追加するか、一時テーブルを使用して、基になるデータを一時テーブルに保存してから、シーケンスの次の日付を更新します)。

開始データ構造は次のようになります。

ID, PerfDate, NextDate
200236, 2011-01-02, 2011-01-03
200236, 2011-01-03, 2011-01-04
etc.

次に、一連の相関サブクエリを使用して、データを目的の出力にロールアップできます。

SELECT ID, StartDate, EndDate
FROM (
SELECT DISTINCT ID, PerfDate AS StartDate, 
    (SELECT MIN([PerfDate]) FROM [SourceTable] S3
    WHERE S3.ID = S1.ID
    AND S3.NextDate > S1.PerfDate
    AND ISNULL(
        (SELECT MIN(PerfDate) 
        FROM [SourceTable] AS S4
        WHERE S4.ID = S1.ID 
        AND S4.NextDate > S3.NextDate), S3.NextDate + 1) > S3.NextDate) AS EndDate
FROM [SourceTable] S1
WHERE 
    ISNULL(
        (SELECT MAX(NextDate) 
        FROM [SourceTable] S2 
        WHERE S2.ID = S1.ID 
        AND S2.PerfDate < S1.PerfDate), PerfDate -1) < S1.PerfDate)q
ORDER BY q.ID, q.StartDate
于 2011-05-05T17:39:32.513 に答える
0

このSOの質問はあなたを助けるかもしれません。これも同様の問題だと思うので、RobFarleyの回答に直接リンクしました。

于 2011-05-05T17:07:01.500 に答える
0

これは私が過去に行った方法です。これは 2 段階のプロセスです。

  1. 候補連続期間のセットを構築する
  2. 重複する期間がある場合は、最長の期間を除いてすべて削除します。

これがどのように行われるかを示すスクリプトです。単一の [バグ、醜い] クエリでそれをやってのけることができるかもしれませんが、それをしようとすると頭が痛いです。デバッグがずっと簡単になるので、一時テーブルを使用しています。

drop table #source
create table #source
(
  id    int      not null ,
  dtCol datetime not null ,

  -----------------------------------------------------------------------
  -- ASSUMPTION 1: Each date must be unique for a given ID value.
  -----------------------------------------------------------------------
  unique clustered ( id , dtCol ) ,

  -----------------------------------------------------------------------
  -- ASSUMPTION 2: The datetime column only represents a day.
  -- The value of the time component is always 00:00:00.000
  -----------------------------------------------------------------------
  check ( dtCol = convert(datetime,convert(varchar,dtCol,112),112) ) ,

)
go

insert #source values(1,'jan 1, 2011')
insert #source values(1,'jan 4, 2011')
insert #source values(1,'jan 5, 2011')
insert #source values(2,'jan 1, 2011')
insert #source values(2,'jan 2, 2011')
insert #source values(2,'jan 3, 2011')
insert #source values(2,'jan 5, 2011')
insert #source values(3,'jan 1, 2011')
insert #source values(4,'jan 1, 2011')
insert #source values(4,'jan 2, 2011')
insert #source values(4,'jan 3, 2011')
insert #source values(4,'jan 4, 2011')
go

insert #source values( 200236 , '2011-01-02')
insert #source values( 200236 , '2011-01-03')
insert #source values( 200236 , '2011-01-05')
insert #source values( 200236 , '2011-01-06')
insert #source values( 200236 , '2011-01-07')
insert #source values( 200236 , '2011-01-08')
insert #source values( 200236 , '2011-01-09')
insert #source values( 200236 , '2011-01-10')
insert #source values( 200236 , '2011-01-11')
insert #source values( 200236 , '2011-01-12')
insert #source values( 200236 , '2011-01-13')
insert #source values( 200236 , '2011-01-15')
insert #source values( 200236 , '2011-01-16')
insert #source values( 200236 , '2011-01-17')
go

drop table #candidate_range
go
create table #candidate_range
(
  rowId   int      not null identity(1,1) ,
  id      int      not null ,
  dtFrom  datetime not null ,
  dtThru  datetime not null ,
  length  as 1+datediff(day,dtFrom,dtThru) ,

  primary key nonclustered ( rowID ) ,
  unique clustered (id,dtFrom,dtThru) ,

)
go

--
-- seed the candidate range table with the set of all possible contiguous ranges for each id
--
insert #candidate_range ( id , dtFrom , dtThru )
select id      = tFrom.id    ,
       valFrom = tFrom.dtCol ,
       valThru = tThru.dtCol
from #source tFrom
join #source tThru on tThru.id     = tFrom.id
                  and tThru.dtCol >= tFrom.dtCol
where 1+datediff(day,tFrom.dtCol,tThru.dtCol) = ( select count(*)
                                                  from #source t
                                                  where t.id = tFrom.id
                                                    and t.dtCol between tFrom.dtCol and tThru.dtCol
                                                )
order by 1,2,3
go

--
-- compare the table to itself. If we find overlapping periods,
-- we'll keep the longest such period and delete the shorter overlapping periods.
--
delete t2
from #candidate_range t1
join #candidate_range t2 on t2.id      = t1.id
                        and t2.rowId  != t1.rowID
                        and t2.length <  t1.length
                        and t2.dtFrom <= t1.dtThru
                        and t2.dtThru >= t1.dtFrom
go

それだけです。

于 2011-05-05T23:48:31.640 に答える