1..decode的用法
·含义解释:
decode(条件,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值)
1.1比较大小
select decode(sign(变量1-变量2),-1,变量1,变量2) from dual; --取较小值
sign()函数根据某个值是0、正数还是负数,分别返回0、1、-1
例如:
变量1=10,变量2=20
则sign(变量1-变量2)返回-1,decode解码结果为“变量1”,达到了取较小值的目的。
1.2空值转换
decode(列表达式,null,转换值,默认值)
用于将列中的null值转换为对应的值,当转化值是一条select时(当select 为空时屏蔽了decode函数)
1.3行转列
现有一个商品销售表sale,表结构为:
month char(6) --月份
sell number(10,2) --月销售金额
现有数据为:
200001 1000
200002 1100
200003 1200
200004 1300
200005 1400
200006 1500
200007 1600
200101 1100
200202 1200
200301 1300
想要转化为以下结构的数据:
year char(4) --年份
month1 number(10,2) --1月销售金额
month2 number(10,2) --2月销售金额
month3 number(10,2) --3月销售金额
month4 number(10,2) --4月销售金额
month5 number(10,2) --5月销售金额
month6 number(10,2) --6月销售金额
month7 number(10,2) --7月销售金额
month8 number(10,2) --8月销售金额
month9 number(10,2) --9月销售金额
month10 number(10,2) --10月销售金额
month11 number(10,2) --11月销售金额
month12 number(10,2) --12月销售金额
结构转化的SQL语句为:
create or replace view
v_sale(year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12)
as
select
substrb(month,1,4),
sum(decode(substrb(month,5,2),'01',sell,0)),
sum(decode(substrb(month,5,2),'02',sell,0)),
sum(decode(substrb(month,5,2),'03',sell,0)),
sum(decode(substrb(month,5,2),'04',sell,0)),
sum(decode(substrb(month,5,2),'05',sell,0)),
sum(decode(substrb(month,5,2),'06',sell,0)),
sum(decode(substrb(month,5,2),'07',sell,0)),
sum(decode(substrb(month,5,2),'08',sell,0)),
sum(decode(substrb(month,5,2),'09',sell,0)),
sum(decode(substrb(month,5,2),'10',sell,0)),
sum(decode(substrb(month,5,2),'11',sell,0)),
sum(decode(substrb(month,5,2),'12',sell,0))
from sale
group by substrb(month,1,4);
分享到:
相关推荐
关于oracle decode函数的用法
decode函数.docx
主要作用:将查询结果翻译成其他值(即以其他形式表现...Select decode(columnname,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值) From talbename Where … 其中columnname为要选择的table中所定义的column,
1 DECODE 中的if-then-else逻辑 在逻辑编程中,经常用到If – Then –Else 进行逻辑判断。在DECODE的语法中,实际上就是这样的逻辑处理过程。
DECODE函数相当于一条件语句(IF).它将输入数值与函数中的参数列表相比较,根据输入值返回一个对应值。
decode函数.pdf
gzip124 源代码中decode函数说明
decode函数[归类].pdf
decode函数借鉴.pdf
Decode函数的语法结构如下: 代码如下:decode (expression, search_1, result_1)decode (expression, search_1, result_1, search_2, result_2)decode (expression, search_1, result_1, search_2, result_2, …., ...
decode(条件,值1,返回值1,值2,返回值2,…值n,返回值n,缺省值) 该函数的含义如下: IF 条件=值1 THEN RETURN(翻译值1) ELSIF 条件=值2 THEN RETURN(翻译值2) ...... ELSIF 条件=值n THEN RETURN(翻译值n) ...
昨天同事遇到一个奇怪的问题,就是以下代码,无法通过JSON校验,也无法通过PHP的json_decode函数解析。 复制代码 代码如下: [ { ”title”: “”, “pinyin”: “” } ] 可能聪明的你已经猜到其中包含有不...
主要介绍了php自定义urlencode,urldecode函数,实例分析了php字符串转码的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
主要介绍了oracle中decode函数的使用方法,需要的朋友可以参考下
这个是 utf-8版本的 js实现 php的 urlencode() 和 urldecode()两个函数的功能。 在传送cookie的时候 在 php端实现 url编码 但要用 js来解码cookie的时候 就出现汉字不能不能识别的问题 这个 js 很好的解决
主要介绍了PHP中遇到BOM、编码导致json_decode函数无法解析问题,json无法正常解析的同学可以看一下,是不是看不见的BOM编码导致的问题,需要的朋友可以参考下
Oracle DECODE函数功能很强,下面就为您详细介绍Oracle DECODE函数的用法,希望可以让您对Oracle DECODE函数有更多的了解。 Oracle DECODE函数 Oracle DECODE函数是Oracle公司独家提供的功能,它是一个功能很强的...
Oracle中Decode()函数使用技巧
主要介绍了Oracle用decode函数或CASE-WHEN实现自定义排序功能,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
在数据库开发的过程当中,有很多时候需要将行转换成列或者将列转换成行来显示数据,而往往我们在建立表结构时不能根据显示的要求来保存数据,于是乎只能在保存数据之后做一些必要的操作(比方说:建立视图等)来达到...