ここはOracleの解決策です。必要に応じて調整します。上部はソリューションの一部ではありません(コードのCOMMENTを参照してください)。私はテストのためだけに使用します。最初のテーブルにもう1つの行を追加して、2番目のテーブルの最後の日付を超えた日付でクエリが正しく動作することを確認しました。また、Oracle の日付は予約語なので、列名として使用しないでください。私はdtに変更しました。
with
your_first_table (id, dt, value) as (
select 1, date '2016-11-01', 1.0 from dual union all
select 1, date '2016-11-03', 1.76 from dual union all
select 1, date '2017-04-11', 2.34 from dual
),
your_second_table (id, class, dt) as (
select 1, 'c1', date '2016-11-01' from dual union all
select 1, 'c2', date '2016-12-02' from dual union all
select 1, 'c3', date '2017-02-03' from dual
)
-- end of test data; SQL query begins below this line
select t1.id, t2.class, t1.dt, t1.value
from your_first_table t1
left outer join
(select id, class, dt as from_dt,
lead(dt) over (partition by id order by dt) as to_dt
from your_second_table
) t2
on t1.id = t2.id
and t1.dt >= t2.from_dt
and (t1.dt < t2.to_dt or t2.to_dt is null)
;
ID CLASS DT VALUE
---- ----- ---------- ----------
1 c1 2016-11-01 1
1 c1 2016-11-03 1.76
1 c3 2017-04-11 2.34
3 rows selected.
どのRDBMSを使用していますか?タグが多すぎます。編集:誰かが今すぐ編集でそれらを削除しました。このコメントは、元の投稿ごとにhttp://stackoverflow.com/revisions/41266206/1 –
DBMSを選んでください。 – Siyual
[formatting]についての有用なもの(http://stackoverflow.com/help/formatting)また、これまでに何を試しましたか? – Aleksej