一、原始数据
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/7ed522b3fa304b5bbdbc3e0db074a7a5.png)
二、行转列
第一步
SELECT
CASE WHEN SUBSTRING( aaa.cost_code, 1, 3 ) = '100' THEN aaa.budget_cost_budget ELSE 0 END b1,
CASE WHEN SUBSTRING( aaa.cost_code, 1, 3 ) = '300' THEN aaa.budget_cost_budget ELSE 0 END b2,
CASE WHEN SUBSTRING( aaa.cost_code, 1, 3 ) = '200' THEN aaa.budget_cost_budget ELSE 0 END b3,
CASE WHEN SUBSTRING( aaa.cost_code, 1, 3 ) = '400' THEN aaa.budget_cost_budget ELSE 0 END b4,
CASE WHEN SUBSTRING( aaa.cost_code, 1, 3 ) = '500' THEN aaa.budget_cost_budget ELSE 0 END b5
FROM
ffff_info aaa
WHERE
aaa.budget_dh = 1
AND aaa.project_id = '04af0af9-c96d-4744-92d5-3967975b9f6e'
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/032aa97c3273476bb52ae28ab7e4e128.png)
第二步
SELECT
max( CASE WHEN SUBSTRING( aaa.cost_code, 1, 3 ) = '100' THEN aaa.budget_cost_budget ELSE 0 END ) b1,
max( CASE WHEN SUBSTRING( aaa.cost_code, 1, 3 ) = '300' THEN aaa.budget_cost_budget ELSE 0 END ) b2,
max( CASE WHEN SUBSTRING( aaa.cost_code, 1, 3 ) = '200' THEN aaa.budget_cost_budget ELSE 0 END ) b3,
max( CASE WHEN SUBSTRING( aaa.cost_code, 1, 3 ) = '400' THEN aaa.budget_cost_budget ELSE 0 END ) b4,
max( CASE WHEN SUBSTRING( aaa.cost_code, 1, 3 ) = '500' THEN aaa.budget_cost_budget ELSE 0 END ) b5
FROM
ffff_info aaa
WHERE
aaa.budget_dh = 1
AND aaa.project_id = '04af0af9-c96d-4744-92d5-3967975b9f6e'
GROUP BY
aaa.project_id
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/06119a2666e94b0fae213e8271e31164.png)
三、转换结果
![](https://img-blog.csdnimg.cn/direct/68d1db5a53794837a37972fc69af8a00.png)