decode函数的用法

  1. 含义解释:   DECODE(条件,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值)   
  2.     
  3.   该函数的含义如下:   
  4.   IF   条件=值1   THEN   
  5.       RETURN(翻译值1)   
  6.   ELSIF   条件=值2   THEN   
  7.       RETURN(翻译值2)   
  8.       ......   
  9.   ELSIF   条件=值n   THEN   
  10.       RETURN(翻译值n)   
  11.     
  12.   ELSE   
  13.       RETURN(缺省值)   
  14.   END   IF   
  15.         
  16.     使用方法:   1、比较大小   
  17.   select   decode(sign(变量1-变量2),-1,变量1,变量2)   from   dual;   --取较小值   
  18.   sign()函数根据某个值是0、正数还是负数,分别返回0、1、-1   
  19.     
  20.   例如:   
  21.   变量1=10,变量2=20   
  22.   则sign(变量1-变量2)返回-1,decode解码结果为“变量1”,达到了取较小值的目的。   
  23.     
  24.     
  25.   2、表、视图结构转化   
  26.   现有一个商品销售表sale,表结构为:   
  27.   month      char(6)        --月份   
  28.   sell    number(10,2)   --月销售金额   
  29.     
  30.   现有数据为:   
  31.   200001  1000   
  32.   200002  1100   
  33.   200003  1200   
  34.   200004  1300   
  35.   200005  1400   
  36.   200006  1500   
  37.   200007  1600   
  38.   200101  1100   
  39.   200202  1200   
  40.   200301  1300   
  41.     
  42.   想要转化为以下结构的数据:   
  43.   year   char(4)        --年份   
  44.   month1  number(10,2)   --1月销售金额   
  45.   month2  number(10,2)   --2月销售金额   
  46.   month3  number(10,2)   --3月销售金额   
  47.   month4  number(10,2)   --4月销售金额   
  48.   month5  number(10,2)   --5月销售金额   
  49.   month6  number(10,2)   --6月销售金额   
  50.   month7  number(10,2)   --7月销售金额   
  51.   month8  number(10,2)   --8月销售金额   
  52.   month9  number(10,2)   --9月销售金额   
  53.   month10  number(10,2)   --10月销售金额   
  54.   month11  number(10,2)   --11月销售金额   
  55.   month12  number(10,2)   --12月销售金额   
  56.     
  57.   结构转化的SQL语句为:   
  58.   create   or   replace   view   
  59.   v_sale(year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12)   
  60.   as   
  61.       select   
  62.       substrb(month,1,4),   
  63.       sum(decode(substrb(month,5,2),01,sell,0)),   
  64.       sum(decode(substrb(month,5,2),02,sell,0)),   
  65.       sum(decode(substrb(month,5,2),03,sell,0)),   
  66.       sum(decode(substrb(month,5,2),04,sell,0)),   
  67.       sum(decode(substrb(month,5,2),05,sell,0)),   
  68.       sum(decode(substrb(month,5,2),06,sell,0)),   
  69.       sum(decode(substrb(month,5,2),07,sell,0)),   
  70.       sum(decode(substrb(month,5,2),08,sell,0)),   
  71.       sum(decode(substrb(month,5,2),09,sell,0)),   
  72.       sum(decode(substrb(month,5,2),10,sell,0)),   
  73.       sum(decode(substrb(month,5,2),11,sell,0)),   
  74.       sum(decode(substrb(month,5,2),12,sell,0))   
  75.       from   sale   
  76.       group   by   substrb(month,1,4);   
    1. select v_dept_code as 机构代码,
    2.        sum(decode(n_type, 1, n_money_left,0)) as 长款金额,
    3.        sum(decode(n_type, 1, 1,0)) as 长款笔数,
    4.        sum(decode(n_type, 2, n_money_left,0)) as 短款金额,
    5.        sum(decode(n_type, 2, 1,0)) as 短款笔数
    6.   from tb_cdk_fund_danger_info t
    7.  where D_CONFIRM_DATE between sysdate - 1 and sysdate
    8.  group by v_dept_code
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值