は、私は次のような構造を持つテーブルで日付のクラスタを見つけたい:私はこのクラスタ内の最初の日から7(またはその他の間隔)日以内の日付のクラスタを見つけたいSQLパーpostgreSQLで日付クラスタを見つける方法は?
patientID | dt
---------- | ----------
1 | 2016-05-03
1 | 2016-05-05
1 | 2016-05-07
2 | 2016-07-12
2 | 2016-07-15
2 | 2016-07-18
1 | 2016-07-13
1 | 2016-07-14
。通常、2つの連続したクラスタ間のギャップは7以上であるため、この問題は無視できます。私の最初のアプローチは機能せず、理由はないが、(集計でグループすることはできません)が、私は私の問題を解決するためにどのようにさっぱりだが、それは明らかだ
patientID | min(dt) | max(dt) | count(dt)
---------- | ---------- | ---------- | ---------
1 | 2016-05-03 | 2016-05-07 | 3
2 | 2016-07-12 | 2016-07-18 | 3
1 | 2016-07-13 | 2016-07-14 | 2
:
select t0.patientID, min(t0.dt), max(t0.dt), count(*)
from tbl t0
join tbl t1 on t0.patientID=t1.patientID and t1.dt - t0.dt between 1 and 7
group by t0.patientID, min(t0.dt);
'generate_series(start :: date、end :: date、 '7 days' :: interval)'に参加したくないですか? –