2017-09-11 2 views
1

私はKM範囲のテーブルと "オーバーライド"テーブルに問題があります。上書きの開始と終了は、表T1の範囲内にすることができます。例:postgresで範囲をオーバーライド

T1 
from to  option 
-1.4 1.7  A 
1.7  4.2  B 
4.2  4.6  A 
4.6  5.3  B 

Override 
T2 
1.2  4.5  C 

問題は、T1から1.7〜4.2行目ですが、この行は「削除」する必要があります。 私の最後のバージョンでは、3行以上ではなく、2行間のオーバーライドしか処理できません。どのように修正できるかわかりません。

dbfiddle上の私の最後のバージョン: http://dbfiddle.uk/?rdbms=postgres_9.6&fiddle=bc71d293c112729fe8d3b077b377ea92

は、それは次のようになります。

Result 

from to  option 
-1.4 1.2  A 
1.2  4.5  C 
4.5  4.6  A 
4.6  5.3  B 
+0

use-は何である必要があります。ここで私は、t2からとt1と重なり、そしてちょうどT2を追加するにはT1から「削除します」場合?つまり、新しい範囲を再計算する必要が本当にありますか?与えられた値が属する範囲(オーバーライドを含む)に基づいて、指定された値のオプションを取得するだけでいいですか? – Adam

+0

元のテーブルは1キロメートルと1つのオプションしかないので、この時点でこのオプションは有効です。しかし、上書きはfrom-toで行われているので、正しい選択肢がないので、4.5にギャップがあります。 –

答えて

1

私は右の質問を得たかわかりません。

t=# select t1.* from t1 
left outer join t2 on t2.fromkm < t1.fromkm and t2.tokm > t1.tokm 
where t2.tokm is null 
union all 
select * from t2 
t-# order by fromkm; 
fromkm | tokm | option | comment 
--------+------+--------+------------ 
    -1.4 | 1.7 | A  | normal 
    1.2 | 4.5 | C  | override 
    4.2 | 4.6 | A  | normal 
    4.6 | 5.3 | B  | normal 
(4 rows) 
+0

はい、これは役に立ちます。私は "help1"を減らし、私の交換部品で正しい結果を得られます。http://dbfiddle.uk/?rdbms=postgres_9.6&fiddle=d504576dc3521e9435c3ecdf899fa268 –

0

のでヴァオツンからの助けを借りて、完全なコードが

with help1 as (
select t1.* from t1 
left outer join t2 on t2.fromkm < t1.fromkm and t2.tokm > t1.tokm 
where t2.tokm is null 
union all 
select * from t2 
order by fromkm) 

,nummer as (
select row_number() over (order by fromkm) as lfdnr,fromkm,tokm,option,comment from help1) 

select 
case when a.fromkm<c.tokm and c.comment='override' then c.tokm else a.fromkm end as fromnew, 
case when a.tokm>b.fromkm and a.comment!='override' then b.fromkm else a.tokm end as tonew, 
a.option,a.comment from nummer a 
left join nummer b on a.lfdnr+1=b.lfdnr 
left join nummer c on a.lfdnr=c.lfdnr+1 

order by a.fromkm;