背景
数据库
PostgreSQL
表
tbl_mr
hos | xb | lyfs | ylfkfs |
---|---|---|---|
广东医院 | 1 | 3 | 01 |
广东医院 | 1 | 4 | 04 |
浙江医院 | 0 | 3 | 07 |
浙江医院 | 0 | 2 | 09 |
… | … | … | … |
tbl_emr
hos | xb | lyfs | ylfkfs |
---|---|---|---|
广东医院 | 9999 | 9 | 03 |
广东医院 | 1 | 4 | 08 |
浙江医院 | 9 | 5 | 07 |
浙江医院 | 0 | 2 | 14 |
… | … | … | … |
其中 xb 字段为 numeric 类型,其他为 varchar 类型
需求
- 由于三个字段的编码可能有错误,想要映射成标准的编码,如果一条一条记录去更改映射不可能(数据量很大),故需要统计出每个字段的值域范围,做好映射后再批量修改到记录里;
- 两张表是不同源的数据,需要一个字段标识出;
目标
type | field | range |
---|---|---|
mr | xb | 0 |
mr | xb | 1 |
… | … | … |
emr | lyfs | 2 |
emr | lyfs | 3 |
… | … | … |
SQL
with temp3 as (
(with temp1 as ((select distinct 'xb' field, xb::varchar range
from tbl_mr)
union all
(select distinct 'lyfs' field, lyfs range
from tbl_mr)
union all
(select distinct 'ylfkfs' field, ylfkfs range
from tbl_mr))
select 'mr' type, *
from temp1
)
union all
(with temp2 as (
(select distinct 'xb' field, xb::varchar range
from tbl_emr)
union all
(select distinct 'lyfs' field, lyfs range
from tbl_emr)
union all
(select distinct 'ylfkfs' field, ylfkfs range
from tbl_emr)
)
select 'emr' type, *
from temp2
))
select * from temp3
group by type, field, range
order by type, field, range;