一、原始数据## 二、行转列
第一步
SELECTCASE 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
FROMffff_info aaa
WHEREaaa.budget_dh = 1 AND aaa.project_id = '04af0af9-c96d-4744-92d5-3967975b9f6e'
第二步
SELECTmax( 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
FROMffff_info aaa
WHEREaaa.budget_dh = 1 AND aaa.project_id = '04af0af9-c96d-4744-92d5-3967975b9f6e'
GROUP BYaaa.project_id
三、转换结果