0
私はテーブルid
(一意ではない)、start_date
、およびend_date
のテーブルfoo
を持っています。私は、特定のid
を持つすべての行に対して、[start_date
,end_date
]の範囲が重複しないようにしたい。問題の行のend_date
を変更して、この処理を実行します。範囲を重複しないように更新するにはどうすればよいですか?
old
は、複数の他のレコードと重複する場合、私は
update foo old
set end_date = new.start_date
from foo new
where old.start_date < new.start_date and old.end_date > new.start_date
and old.id = new.id and new.id = 8675309;
しかし、これは動作しませんしています。私の最高の試みは
update foo old
set end_date = (select coalesce(min(new.start_date), old.end_date)
from foo new
where old.start_date < new.start_date and old.end_date > new.start_date
and new.id = old.id)
where old.id = 8675309;
これは動作しますが、それは不必要にすべての行を更新し、ハックのように感じています。これを行う最善の方法は何ですか?