SQL 行轉列問題

有report表:它裏面 id  name   state    customerid 四個字段,id爲主鍵 ,customerid爲外鍵
state 0, 1,2   //  未使用  更新  刪除

根據customerid寫一條sql (注意是一條)生成表的結構如下:

customerid  state0   state1  state2
001          11       212      333

002          15       545      3



select customid,
    count(case status when 0 then status else null end) as status-0,
    count(case status when 1 then status else null end) as status-1,
    count(case status when 2 then status else null end) as status-2
    from custom group by customid.


發佈了140 篇原創文章 · 獲贊 8 · 訪問量 28萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章