以前在一张 表里面显示的 S1 S2 S3 三个字段, 现在拆分到了 一个表里面的 3 条记录。
为了兼容之前的版本, 同事写出视图采用 ORL CASE WHEN 。。。 END 的结构:
select case when ts.s_order='1' then ts.s_date else null END date1,
case when ts.s_order='2' then ts.s_date else null END date2,
case when ts.s_order='3' then ts.s_date else null END date3,
case when ts.s_order='1' then ts.pilot_id else null END pilot1,
case when ts.s_order='2' then ts.pilot_id else null END pilot2,
case when ts.s_order='3' then ts.pilot_id else null END pilot3,
case when ts.s_order='1' then ts.s_no else null END s_no1,
case when ts.s_order='2' then ts.s_no else null END s_no2,
case when ts.s_order='3' then ts.s_no else null END s_no3,
ts.suite_id
from sim_training_sim ts
WHERE ts.suite_id = 105698
查询出的结果:
然后,再将这些结果合并成一条记录, 就符合上一期的表结构要求了
select max(case when ts.s_order='1' then ts.s_date else null end) date1,
max(case when ts.s_order='2' then ts.s_date else null end) date2,
max(case when ts.s_order='3' then ts.s_date else null end) date3,
max(case when ts.s_order='1' then ts.pilot_id else null end) pilot1,
max(case when ts.s_order='2' then ts.pilot_id else null end ) pilot2,
max( case when ts.s_order='3' then ts.pilot_id else null end ) pilot3,
max(case when ts.s_order='1' then ts.s_no else null end) s_no1,
max(case when ts.s_order='2' then ts.s_no else null end) s_no2,
max( case when ts.s_order='3' then ts.s_no else null end) s_no3,
ts.suite_id
from sim_training_sim ts
group by ts.suite_id
使用了 GROUP BY 来将结果合并, 但是查询的字段 必须是在函数中的, 所以使用 MAX 函数构造 结果: