`

@Marked-今天的一个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函数

select
     game_time as 比赛日期,
     decode(rst,'F','失败','W','胜利','无结果') as 结果,
     count(rst) as 结果统计
 from rec
 group by game_time,rst;

 SQL2:case语句:

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][default]);

2.  case 语句:

case 表达式

when 表达式1 then ....

when 表达式2 then ....

else  ......

end 表达式

1
2
分享到:
评论
2 楼 dotjar 2014-10-31  
quanwsx 写道
哦 真不错

1 楼 quanwsx 2013-07-02  
哦 真不错

相关推荐

Global site tag (gtag.js) - Google Analytics