2

わかりました、ここでトリッキーなものを入手しました...私のデータが次のようになっている場合:

表1

ID  Date_Created 
1   1/1/2009
2   1/3/2009
3   1/5/2009
4   1/10/2009
5   1/15/2009
6   1/16/2009

互いに2日離れているレコードを取得するにはどうすればよいですか?最終結果セットは、行1〜3、および5〜6になります。ありがとう!

4

7 に答える 7

1
SELECT l.*
FROM Table1 l
INNER JOIN Table1 r ON DATEDIFF(d, l.Date_Created, r.Date_Created) = 2
      AND r.Date_Created = (SELECT TOP 1 * FROM Table1 WHERE Date_Created > l.Date_Created ORDER BY Date_Create)
于 2009-07-15T21:50:31.387 に答える
0

これは機能しますか?

select t1.id, t2.id 
  from table1 t1 
  join table1 t2 
    on t2.date_created - t1.date_created <= 2
于 2009-07-15T21:49:33.200 に答える
0

-これはあなたに何を与えますか?

DISTINCT t1.id、t1.date_created、t2.id、t2.date_created from table1 t1、table1 t2を選択します。ここで、datediff(dd、t1.date_created、t2.date_created)= 2 AND t1.id!= t2.id ORDER BY t1 .id;

于 2009-07-15T21:50:34.460 に答える
0

私はそれを行うためにプログラミングコードを使用することを提案するかもしれません。行のグループ(個別のグループ)を収集する必要があります。単一のクエリを使用してこれを解決できるとは思いません(1セットの行だけが返されます)。

于 2009-07-15T21:51:31.790 に答える
0
select distinct t1.*
from Table1 t1
inner join Table1 t2 
    on abs(cast(t1.Date_Created - t2.Date_Created as float)) between 1 and 2
于 2009-07-15T21:54:56.637 に答える
0

'N'日以内の行を取得したい場合は、次のことを試してください。

select t1.date_created, t2.date_created 
from table1 t1, table1 t2 
where t1.id <> t2.id and 
      t2.date_created-t1.date_created between 0 and N;

たとえば、あなたが言ったように、2日以内の行を取得したい場合は、以下を使用できます。

select t1.date_created,t2.date_created 
from table1 t1, table1.t2 
where t1.id <> t2.id and 
      t2.date_created-t1.date_created between 0 and 2;

これがお役に立てば幸いです。

よろしく、スリクリシュナ。

于 2009-07-16T00:06:48.273 に答える
0

カーソルが最速になりますが、これを実行するSELECTクエリを次に示します。2日ではなく「最大N」日離れている場合は、テーブル2を0からN-1までの整数のテーブルに置き換える必要があることに注意してください(効率が低下します)。

何が必要かは完全には明確ではありませんが、全部で少なくとも2行を含み、連続する行が最大2日離れている行の範囲が必要だと思います。IDとともに日付が増える場合、これは機能するはずです。

with Two as (
  select 0 as offset union all select 1 
), r2(ID, Date_Created_o, dr) as (
  select
    ID, Date_Created+offset,
    Date_Created + offset - dense_rank() over (
      order by Date_Created+offset
    ) from r cross join Two
)
  select
    min(ID) as start, max(ID) as finish
  from r2
  group by dr
  having min(ID) < max(ID)
  order by dr;
于 2009-07-16T04:50:55.617 に答える