SQL-case語句和decode函數

數據庫表:sql

select * from rec order by rst,game_time;數據庫

ID GAME_TIME RST
2 01-1月 -11 F
6 01-1月 -11 F
3 02-1月 -11 F
9 02-1月 -11 F
7 03-1月 -11 F
1 01-1月 -11 W
4 01-1月 -11 W
8 01-1月 -11 W
5 02-1月 -11 W

要求結果:函數

比賽日期 結果 結果統計
02-1月 -11 失敗 2
03-1月 -11 失敗 1
02-1月 -11 勝利 1
01-1月 -11 失敗 2
01-1月 -11 勝利 3

寫出SQL1:decode函數code

select  
     game_time as 比賽日期,  
     decode(rst,'F','失敗','W','勝利','無結果') as 結果,  
     count(rst) as 結果統計  
 from rec  
 group by game_time,rst;

SQL2:case語句:table

select   
game_time as 比賽日期,  
(case rst  when 'W' then '勝利'  
                when 'F' then '失敗'  
                else '無結果'  
end)結果,  
count(rst) as 結果統計  
from rec  
group by game_time,rst;
記錄下:
  1. decode函數用法:

    decode(表達式1,條件1,結果1,條件2,結果2);class

  2. case 語句:
    case 表達式
    when 表達式1 then ....
    when 表達式2 then ....
    else ......
    end 表達式

---[2013-07-02]---select

相關文章
相關標籤/搜索