ARTICLE · INTELLIGENCE

战地情报 · 详情页

来自尧图项目组的一线实战观察与深度解析

数据库列转行实现

数据库列转行实现

昨天遇到一个需求,给的文档内容是机构,职能说明,工时信息,如图:

因为需要做成配置表数据进行后续计算,且需要用机构+职能说明进行关联映射,所以需要进行一定程度的转置。

转置完成效果如图:

实现逻辑如下:

--数据转置逻辑(纯原创)
with a as(
select '人力资源' as 职能说明,0.2 as 安徽,null as 北京,0.3 as 甘肃,0.6 as 广东,0.3 as 贵州,0.3 as 河北,
0.4 as 河南,2 as 江苏,0.4 as 宁波,0.5 as 青岛,0.6 as 山东,0.4 as 山西,0.1 as 陕西,
1.1 as 上海,0.7 as 深圳,0.4 as 四川,0.25 as 云南,0.7 as 浙江,1.85 as 重庆
union all select '薪酬管理',0.3,null,0.4,0.5,0.4,0.4,0.3,0.6,0.4,0.7,0.6,0.4,0.5,0.55,0.8,0.4,0.25,0.4,2.05
union all select '绩效考核',1.1,null,0.6,0.4,0.2,0.3,0.2,0.6,0.4,0.7,0.6,0.4,0.7,0.6,0.4,0.4,0.1,0.6,0.65
union all select '教育培训',0.2,null,0.2,0.2,0.1,0.2,0.1,0.3,0.2,0.2,0.2,0.3,0.1,0.1,0.1,0.3,0.2,0.1,1.85
union all select '综合管理',0.7,null,0.2,0.5,0.2,0.5,0.3,1,1,1,1,0.3,1,0.55,0.8,0.8,1.3,0.8,3.85
union all select '后勤管理',0.6,null,0.2,0.6,0.2,0.5,0.2,1,0.7,0.7,1,0.3,0.8,0.7,0.8,0.6,0.3,0.4,3.6
union all select '集中采购',0.2,null,0.2,0.2,0.2,0.1,0.1,0.3,0.3,0.5,0.6,0.3,0.3,0.35,0.4,0.1,0,0.1,0.6
union all select '督查督办',0.2,null,0.1,0.1,0.1,0.2,0.1,0.4,null,0.1,0.5,null,0.4,0.1,0,0.2,0.1,0.2,0.85
union all select '文秘',0.3,null,0.4,0.4,0.2,0.2,0.2,0.4,0.4,0.4,0.6,0.3,0.8,0.35,0.5,0.5,0.3,0.5,0.35
union all select '宣传',0.2,null,0.4,0.5,0.1,0.3,0.1,0.4,0.2,0.2,0.3,0.3,0.3,0.1,0.5,0.3,0.2,0.2,1.35
union all select '风险合规',null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null
union all select '党建',null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null
),
b as (
select regexp_split_to_table('1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19',',') aa,a.*
from a
),
c as (
select aa,
case
when aa = '1' then '安徽'
when aa = '2' then '北京'
when aa = '3' then '甘肃'
when aa = '4' then '广东'
when aa = '5' then '贵州'
when aa = '6' then '河北'
when aa = '7' then '河南'
when aa = '8' then '江苏'
when aa = '9' then '宁波'
when aa = '10' then '青岛'
when aa = '11' then '山东'
when aa = '12' then '山西'
when aa = '13' then '陕西'
when aa = '14' then '上海'
when aa = '15' then '深圳'
when aa = '16' then '四川'
when aa = '17' then '云南'
when aa = '18' then '浙江'
when aa = '19' then '重庆'
end 机构,职能说明,
case
when 职能说明='人力资源' then '01'
when 职能说明='薪酬管理' then '02'
when 职能说明='绩效考核' then '03'
when 职能说明='教育培训' then '04'
when 职能说明='综合管理' then '05'
when 职能说明='后勤管理' then '06'
when 职能说明='集中采购' then '07'
when 职能说明='督查督办' then '08'
when 职能说明='文秘' then '09'
when 职能说明='宣传' then '10'
when 职能说明='风险合规' then '11'
when 职能说明='党建' then '12'
end rr,
case
when aa = '1' then
case
when 职能说明='人力资源' then 安徽::numeric(16,2)
when 职能说明='薪酬管理' then 安徽::numeric(16,2)
when 职能说明='绩效考核' then 安徽::numeric(16,2)
when 职能说明='教育培训' then 安徽::numeric(16,2)
when 职能说明='综合管理' then 安徽::numeric(16,2)
when 职能说明='后勤管理' then 安徽::numeric(16,2)
when 职能说明='集中采购' then 安徽::numeric(16,2)
when 职能说明='督查督办' then 安徽::numeric(16,2)
when 职能说明='文秘' then 安徽::numeric(16,2)
when 职能说明='宣传' then 安徽::numeric(16,2)
when 职能说明='风险合规' then 安徽::numeric(16,2)
when 职能说明='党建' then 安徽::numeric(16,2)
end
when aa = '2' then
case
when 职能说明='人力资源' then 北京::numeric(16,2)
when 职能说明='薪酬管理' then 北京::numeric(16,2)
when 职能说明='绩效考核' then 北京::numeric(16,2)
when 职能说明='教育培训' then 北京::numeric(16,2)
when 职能说明='综合管理' then 北京::numeric(16,2)
when 职能说明='后勤管理' then 北京::numeric(16,2)
when 职能说明='集中采购' then 北京::numeric(16,2)
when 职能说明='督查督办' then 北京::numeric(16,2)
when 职能说明='文秘' then 北京::numeric(16,2)
when 职能说明='宣传' then 北京::numeric(16,2)
when 职能说明='风险合规' then 北京::numeric(16,2)
when 职能说明='党建' then 北京::numeric(16,2)
end
when aa = '3' then
case
when 职能说明='人力资源' then 甘肃::numeric(16,2)
when 职能说明='薪酬管理' then 甘肃::numeric(16,2)
when 职能说明='绩效考核' then 甘肃::numeric(16,2)
when 职能说明='教育培训' then 甘肃::numeric(16,2)
when 职能说明='综合管理' then 甘肃::numeric(16,2)
when 职能说明='后勤管理' then 甘肃::numeric(16,2)
when 职能说明='集中采购' then 甘肃::numeric(16,2)
when 职能说明='督查督办' then 甘肃::numeric(16,2)
when 职能说明='文秘' then 甘肃::numeric(16,2)
when 职能说明='宣传' then 甘肃::numeric(16,2)
when 职能说明='风险合规' then 甘肃::numeric(16,2)
when 职能说明='党建' then 甘肃::numeric(16,2)
end
when aa = '4' then
case
when 职能说明='人力资源' then 广东::numeric(16,2)
when 职能说明='薪酬管理' then 广东::numeric(16,2)
when 职能说明='绩效考核' then 广东::numeric(16,2)
when 职能说明='教育培训' then 广东::numeric(16,2)
when 职能说明='综合管理' then 广东::numeric(16,2)
when 职能说明='后勤管理' then 广东::numeric(16,2)
when 职能说明='集中采购' then 广东::numeric(16,2)
when 职能说明='督查督办' then 广东::numeric(16,2)
when 职能说明='文秘' then 广东::numeric(16,2)
when 职能说明='宣传' then 广东::numeric(16,2)
when 职能说明='风险合规' then 广东::numeric(16,2)
when 职能说明='党建' then 广东::numeric(16,2)
end
when aa = '5' then
case
when 职能说明='人力资源' then 贵州::numeric(16,2)
when 职能说明='薪酬管理' then 贵州::numeric(16,2)
when 职能说明='绩效考核' then 贵州::numeric(16,2)
when 职能说明='教育培训' then 贵州::numeric(16,2)
when 职能说明='综合管理' then 贵州::numeric(16,2)
when 职能说明='后勤管理' then 贵州::numeric(16,2)
when 职能说明='集中采购' then 贵州::numeric(16,2)
when 职能说明='督查督办' then 贵州::numeric(16,2)
when 职能说明='文秘' then 贵州::numeric(16,2)
when 职能说明='宣传' then 贵州::numeric(16,2)
when 职能说明='风险合规' then 贵州::numeric(16,2)
when 职能说明='党建' then 贵州::numeric(16,2)
end
when aa = '6' then
case
when 职能说明='人力资源' then 河北::numeric(16,2)
when 职能说明='薪酬管理' then 河北::numeric(16,2)
when 职能说明='绩效考核' then 河北::numeric(16,2)
when 职能说明='教育培训' then 河北::numeric(16,2)
when 职能说明='综合管理' then 河北::numeric(16,2)
when 职能说明='后勤管理' then 河北::numeric(16,2)
when 职能说明='集中采购' then 河北::numeric(16,2)
when 职能说明='督查督办' then 河北::numeric(16,2)
when 职能说明='文秘' then 河北::numeric(16,2)
when 职能说明='宣传' then 河北::numeric(16,2)
when 职能说明='风险合规' then 河北::numeric(16,2)
when 职能说明='党建' then 河北::numeric(16,2)
end
when aa = '7' then
case
when 职能说明='人力资源' then 河南::numeric(16,2)
when 职能说明='薪酬管理' then 河南::numeric(16,2)
when 职能说明='绩效考核' then 河南::numeric(16,2)
when 职能说明='教育培训' then 河南::numeric(16,2)
when 职能说明='综合管理' then 河南::numeric(16,2)
when 职能说明='后勤管理' then 河南::numeric(16,2)
when 职能说明='集中采购' then 河南::numeric(16,2)
when 职能说明='督查督办' then 河南::numeric(16,2)
when 职能说明='文秘' then 河南::numeric(16,2)
when 职能说明='宣传' then 河南::numeric(16,2)
when 职能说明='风险合规' then 河南::numeric(16,2)
when 职能说明='党建' then 河南::numeric(16,2)
end
when aa = '8' then
case
when 职能说明='人力资源' then 江苏::numeric(16,2)
when 职能说明='薪酬管理' then 江苏::numeric(16,2)
when 职能说明='绩效考核' then 江苏::numeric(16,2)
when 职能说明='教育培训' then 江苏::numeric(16,2)
when 职能说明='综合管理' then 江苏::numeric(16,2)
when 职能说明='后勤管理' then 江苏::numeric(16,2)
when 职能说明='集中采购' then 江苏::numeric(16,2)
when 职能说明='督查督办' then 江苏::numeric(16,2)
when 职能说明='文秘' then 江苏::numeric(16,2)
when 职能说明='宣传' then 江苏::numeric(16,2)
when 职能说明='风险合规' then 江苏::numeric(16,2)
when 职能说明='党建' then 江苏::numeric(16,2)
end
when aa = '9' then
case
when 职能说明='人力资源' then 宁波::numeric(16,2)
when 职能说明='薪酬管理' then 宁波::numeric(16,2)
when 职能说明='绩效考核' then 宁波::numeric(16,2)
when 职能说明='教育培训' then 宁波::numeric(16,2)
when 职能说明='综合管理' then 宁波::numeric(16,2)
when 职能说明='后勤管理' then 宁波::numeric(16,2)
when 职能说明='集中采购' then 宁波::numeric(16,2)
when 职能说明='督查督办' then 宁波::numeric(16,2)
when 职能说明='文秘' then 宁波::numeric(16,2)
when 职能说明='宣传' then 宁波::numeric(16,2)
when 职能说明='风险合规' then 宁波::numeric(16,2)
when 职能说明='党建' then 宁波::numeric(16,2)
end
when aa = '10' then
case
when 职能说明='人力资源' then 青岛::numeric(16,2)
when 职能说明='薪酬管理' then 青岛::numeric(16,2)
when 职能说明='绩效考核' then 青岛::numeric(16,2)
when 职能说明='教育培训' then 青岛::numeric(16,2)
when 职能说明='综合管理' then 青岛::numeric(16,2)
when 职能说明='后勤管理' then 青岛::numeric(16,2)
when 职能说明='集中采购' then 青岛::numeric(16,2)
when 职能说明='督查督办' then 青岛::numeric(16,2)
when 职能说明='文秘' then 青岛::numeric(16,2)
when 职能说明='宣传' then 青岛::numeric(16,2)
when 职能说明='风险合规' then 青岛::numeric(16,2)
when 职能说明='党建' then 青岛::numeric(16,2)
end
when aa = '11' then
case
when 职能说明='人力资源' then 山东::numeric(16,2)
when 职能说明='薪酬管理' then 山东::numeric(16,2)
when 职能说明='绩效考核' then 山东::numeric(16,2)
when 职能说明='教育培训' then 山东::numeric(16,2)
when 职能说明='综合管理' then 山东::numeric(16,2)
when 职能说明='后勤管理' then 山东::numeric(16,2)
when 职能说明='集中采购' then 山东::numeric(16,2)
when 职能说明='督查督办' then 山东::numeric(16,2)
when 职能说明='文秘' then 山东::numeric(16,2)
when 职能说明='宣传' then 山东::numeric(16,2)
when 职能说明='风险合规' then 山东::numeric(16,2)
when 职能说明='党建' then 山东::numeric(16,2)
end
when aa = '12' then
case
when 职能说明='人力资源' then 山西::numeric(16,2)
when 职能说明='薪酬管理' then 山西::numeric(16,2)
when 职能说明='绩效考核' then 山西::numeric(16,2)
when 职能说明='教育培训' then 山西::numeric(16,2)
when 职能说明='综合管理' then 山西::numeric(16,2)
when 职能说明='后勤管理' then 山西::numeric(16,2)
when 职能说明='集中采购' then 山西::numeric(16,2)
when 职能说明='督查督办' then 山西::numeric(16,2)
when 职能说明='文秘' then 山西::numeric(16,2)
when 职能说明='宣传' then 山西::numeric(16,2)
when 职能说明='风险合规' then 山西::numeric(16,2)
when 职能说明='党建' then 山西::numeric(16,2)
end
when aa = '13' then
case
when 职能说明='人力资源' then 陕西::numeric(16,2)
when 职能说明='薪酬管理' then 陕西::numeric(16,2)
when 职能说明='绩效考核' then 陕西::numeric(16,2)
when 职能说明='教育培训' then 陕西::numeric(16,2)
when 职能说明='综合管理' then 陕西::numeric(16,2)
when 职能说明='后勤管理' then 陕西::numeric(16,2)
when 职能说明='集中采购' then 陕西::numeric(16,2)
when 职能说明='督查督办' then 陕西::numeric(16,2)
when 职能说明='文秘' then 陕西::numeric(16,2)
when 职能说明='宣传' then 陕西::numeric(16,2)
when 职能说明='风险合规' then 陕西::numeric(16,2)
when 职能说明='党建' then 陕西::numeric(16,2)
end
when aa = '14' then
case
when 职能说明='人力资源' then 上海::numeric(16,2)
when 职能说明='薪酬管理' then 上海::numeric(16,2)
when 职能说明='绩效考核' then 上海::numeric(16,2)
when 职能说明='教育培训' then 上海::numeric(16,2)
when 职能说明='综合管理' then 上海::numeric(16,2)
when 职能说明='后勤管理' then 上海::numeric(16,2)
when 职能说明='集中采购' then 上海::numeric(16,2)
when 职能说明='督查督办' then 上海::numeric(16,2)
when 职能说明='文秘' then 上海::numeric(16,2)
when 职能说明='宣传' then 上海::numeric(16,2)
when 职能说明='风险合规' then 上海::numeric(16,2)
when 职能说明='党建' then 上海::numeric(16,2)
end
when aa = '15' then
case
when 职能说明='人力资源' then 深圳::numeric(16,2)
when 职能说明='薪酬管理' then 深圳::numeric(16,2)
when 职能说明='绩效考核' then 深圳::numeric(16,2)
when 职能说明='教育培训' then 深圳::numeric(16,2)
when 职能说明='综合管理' then 深圳::numeric(16,2)
when 职能说明='后勤管理' then 深圳::numeric(16,2)
when 职能说明='集中采购' then 深圳::numeric(16,2)
when 职能说明='督查督办' then 深圳::numeric(16,2)
when 职能说明='文秘' then 深圳::numeric(16,2)
when 职能说明='宣传' then 深圳::numeric(16,2)
when 职能说明='风险合规' then 深圳::numeric(16,2)
when 职能说明='党建' then 深圳::numeric(16,2)
end
when aa = '16' then
case
when 职能说明='人力资源' then 四川::numeric(16,2)
when 职能说明='薪酬管理' then 四川::numeric(16,2)
when 职能说明='绩效考核' then 四川::numeric(16,2)
when 职能说明='教育培训' then 四川::numeric(16,2)
when 职能说明='综合管理' then 四川::numeric(16,2)
when 职能说明='后勤管理' then 四川::numeric(16,2)
when 职能说明='集中采购' then 四川::numeric(16,2)
when 职能说明='督查督办' then 四川::numeric(16,2)
when 职能说明='文秘' then 四川::numeric(16,2)
when 职能说明='宣传' then 四川::numeric(16,2)
when 职能说明='风险合规' then 四川::numeric(16,2)
when 职能说明='党建' then 四川::numeric(16,2)
end
when aa = '17' then
case
when 职能说明='人力资源' then 云南::numeric(16,2)
when 职能说明='薪酬管理' then 云南::numeric(16,2)
when 职能说明='绩效考核' then 云南::numeric(16,2)
when 职能说明='教育培训' then 云南::numeric(16,2)
when 职能说明='综合管理' then 云南::numeric(16,2)
when 职能说明='后勤管理' then 云南::numeric(16,2)
when 职能说明='集中采购' then 云南::numeric(16,2)
when 职能说明='督查督办' then 云南::numeric(16,2)
when 职能说明='文秘' then 云南::numeric(16,2)
when 职能说明='宣传' then 云南::numeric(16,2)
when 职能说明='风险合规' then 云南::numeric(16,2)
when 职能说明='党建' then 云南::numeric(16,2)
end
when aa = '18' then
case
when 职能说明='人力资源' then 浙江::numeric(16,2)
when 职能说明='薪酬管理' then 浙江::numeric(16,2)
when 职能说明='绩效考核' then 浙江::numeric(16,2)
when 职能说明='教育培训' then 浙江::numeric(16,2)
when 职能说明='综合管理' then 浙江::numeric(16,2)
when 职能说明='后勤管理' then 浙江::numeric(16,2)
when 职能说明='集中采购' then 浙江::numeric(16,2)
when 职能说明='督查督办' then 浙江::numeric(16,2)
when 职能说明='文秘' then 浙江::numeric(16,2)
when 职能说明='宣传' then 浙江::numeric(16,2)
when 职能说明='风险合规' then 浙江::numeric(16,2)
when 职能说明='党建' then 浙江::numeric(16,2)
end
when aa = '19' then
case
when 职能说明='人力资源' then 重庆::numeric(16,2)
when 职能说明='薪酬管理' then 重庆::numeric(16,2)
when 职能说明='绩效考核' then 重庆::numeric(16,2)
when 职能说明='教育培训' then 重庆::numeric(16,2)
when 职能说明='综合管理' then 重庆::numeric(16,2)
when 职能说明='后勤管理' then 重庆::numeric(16,2)
when 职能说明='集中采购' then 重庆::numeric(16,2)
when 职能说明='督查督办' then 重庆::numeric(16,2)
when 职能说明='文秘' then 重庆::numeric(16,2)
when 职能说明='宣传' then 重庆::numeric(16,2)
when 职能说明='风险合规' then 重庆::numeric(16,2)
when 职能说明='党建' then 重庆::numeric(16,2)
end
end 工时
from b
)
select 机构,职能说明,工时 from c order by aa::int,rr

RELATED READING

延伸阅读

更多一线实战笔记与深度复盘,助您持续精进