- 已关闭**,此问题需要details or clarity。目前不接受答复。
- 想改善这个问题吗?* * 通过editing this post添加详细信息并澄清问题。
8天前关闭
Improve this question
我使用的是Oracle SQL数据库。
尝试在sql中将行转置为16个新列。
总共有200个不同的设施代码,但My one limit_id最多可以有16个设施代码。因此,需要创建16个新列。
我正在尝试使用sql case when语句,但由于220个不同的facilty_code而无法实现结果,并且需要在16列中容纳。
SQL代码:
SELECT
lim_id,
lim_ver_num,
max(
case when lim_id = lim_id
and lim_ver_num = lim_ver_num then 'facility_code' else '' end
) as facility_code1,
max(
case when lim_id = lim_id
and lim_ver_num = lim_ver_num then 'facility_code' else '' end
) as facility_code2,
max(
case when lim_id = lim_id
and lim_ver_num = lim_ver_num then 'facility_code' else '' end
) as facility_code3,
max(
case when lim_id = lim_id
and lim_ver_num = lim_ver_num then 'facility_code' else '' end
) as facility_code4,
max(
case when lim_id = lim_id
and lim_ver_num = lim_ver_num then 'facility_code' else '' end
) as facility_code5,
max(
case when lim_id = lim_id
and lim_ver_num = lim_ver_num then 'facility_code' else '' end
) as facility_code6,
max(
case when lim_id = lim_id
and lim_ver_num = lim_ver_num then 'facility_code' else '' end
) as facility_code7,
max(
case when lim_id = lim_id
and lim_ver_num = lim_ver_num then 'facility_code' else '' end
) as facility_code8,
max(
case when lim_id = lim_id
and lim_ver_num = lim_ver_num then 'facility_code' else '' end
) as facility_code9,
max(
case when lim_id = lim_id
and lim_ver_num = lim_ver_num then 'facility_code' else '' end
) as facility_code10,
max(
case when lim_id = lim_id
and lim_ver_num = lim_ver_num then 'facility_code' else '' end
) as facility_code11,
max(
case when lim_id = lim_id
and lim_ver_num = lim_ver_num then 'facility_code' else '' end
) as facility_code12,
max(
case when lim_id = lim_id
and lim_ver_num = lim_ver_num then 'facility_code' else '' end
) as facility_code13,
max(
case when lim_id = lim_id
and lim_ver_num = lim_ver_num then 'facility_code' else '' end
) as facility_code14,
max(
case when lim_id = lim_id
and lim_ver_num = lim_ver_num then 'facility_code' else '' end
) as facility_code15,
max(
case when lim_id = lim_id
and lim_ver_num = lim_ver_num then 'facility_code' else '' end
) as facility_code16
FROM
GROUP BY
lim_id,
lim_ver_num;
- -当前表格格式如下
Limit_ID Version_numer Facility_code 12345 1 AA 12345 1 BB 12345 1 CC 12345 1 DD 12345 1 EE
12345 1 FF 12345 1 GG 12345 1 HH 12345 1 II 12345 1 JJ 12345 1 KK 12345 1 LL 12345 1 MM 12345 1 NN 12345 1 OO 12345 1 PP 6789 2 RR 6789 2 SS 6789 2 TT 6789 2 UU 6789 2 VV 6789 2 WW 6789 2 XX - -我想使用oracle sql转换为以下格式,Limit_ID Version_numer code1 code2 code3 code4 code5 code6 code7 code8 code9 code10 code11 code12 code13 code14 code15 code16
12345 1 AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO PP
6789 2 RR SS TT UU VV WW XX
1条答案
按热度按时间yi0zb3m41#
看起来您可以在LIMIT_ID(和/或VERSION_NUMBER)中创建行号并使用它进行透视。
回复:
| 限值ID|版本号|代码_1| CODE_2| CODE_3|代码_4|代码_5|代码_6|代码_7|代码_8|代码_9|代码_10|代码_11|代码_12|代码_13|代码_14|代码_15|代码_16|
| - -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|
| 一二三四五|1| AA| BB| CC| DD| EE| FF| GG| HH|二|JJ| KK| LL| MM| NN| OO| PP|
| 六七八九|2| QQ| RR| SS| TT| UU|验证与确认|WW| XX| YY| ZZ|零|零|零|零|零|零|