I have 3 tables as follow.
DATA(entity_id, crv_name, data_cnt_id)
PROCESSED_DATA(entity_id, crv_name, run_id)
RUNS(run_id, data_cnt_id)
Both
DATA
and PROCESSED_DATA
have similar columns, except for the last one. PROCESSED_DATA
also has significantly more rows than DATA
.DATA
is about old crv_name values, and PROCESSED_DATA
contains new crv_name values.
I am trying to write a query which would return something like this, for a specific run (identified with
RUN_ID
in PROCESSED_DATA
and RUNS
and with DATA_CNT_ID
in DATA
) :+----------------------------------------------------------------+
| PROCESSED_DATA.ENTITY_ID PROCESSED_DATA.CRV_NAME DATA.CRV_NAME |
+----------------------------------------------------------------+
| entity123 123_new_name 123_old_name |
| entity456 456_new_name 456_old_name |
| entity789 789_new_name null |
+----------------------------------------------------------------+
However, I can't get that last row (ie an entity which doesn't have an old crv_name, and therefore is not present in the table
DATA
) with my query below :select pd.entity_id, pd.crv_name, d.crv_name
from processed_data pd
join data d on d.entity_id = pd.entity_id
join runs r on r.run_id = pd.run_id and r.data_cnt_id = d.data_cnt_id
where r.run_id = 7
Could you help me to improve my query ?
You need to outer join the records:
select pd.ntt_id, pd.crv_name, d.crv_name
from processed_data pd
left join data d on d.entity_id = pd.entity_id
left join runs r on r.run_id = pd.run_id and r.data_cnt_id = d.data_cnt_id and r.run_id = 7;
In case there must be a run 7:
select pd.ntt_id, pd.crv_name, d.crv_name
from processed_data pd
inner join runs r on r.run_id = pd.run_id and r.run_id = 7
left join data d on d.entity_id = pd.entity_id and d.data_cnt_id = r.data_cnt_id;
0 comments:
Post a Comment