教你輕松掌握一個縱表轉(zhuǎn)橫表的SQL

字號:

縱表轉(zhuǎn)橫表的"SQL"示例:
    縱表結構:
    fname ftype fvalue
    小喬 zaocan 10
    小喬 zhongcan 20
    小喬 wancan 5
    轉(zhuǎn)換后的表結構:
    fname zaocan_value zhongcan_value wancan_value
    小喬 10 20 5
    縱表轉(zhuǎn)橫表SQL示例:
    select Fname, sum(case Ftype when 'zaocan'
     then Fvalue else 0 end) as zaocan_value,
    sum(case Ftype when 'zhongcan'
    then Fvalue else 0 end) as zhongcan_value,
    sum(case Ftype when 'wancan' then Fvalue else 0 end)
    as wancan_value from case_table group by Fname