sql行轉列學習筆記
阿新 • • 發佈:2021-01-09
前言 2021-01-08 22:24
sql中的行轉列, 使用decode和case when
原表
效果表
(示例1):
select max(decode("type",1,"value")) 姓名,
max(decode("type",2,"value")) 性別,
max(decode("type",3,"value")) 年齡
from "test" group by "t_id"
(示例2):
select max(case "type" when 1 then "value" end) 姓名,
max(case "type" when 2 then "value" end) 性別,
max(case "type" when 3 then "value" end) 年齡
from "test" group by "t_id"