`

Oracle中Decode()函数使用技巧

阅读更多
decode()
·含义解释:
decode(条件,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值)

该函数的含义如下:
if 条件=值1 then
    return(翻译值1)
elsif 条件=值2 then
    return(翻译值2)
    ......
elsif 条件=值n then
    return(翻译值n)

else
    return(缺省值)
end if
·        使用方法:
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”,达到了取较小值的目的。


2、表、视图结构转化
现有一个商品销售表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()函数使用技巧

    Oracle 中 Decode() 函数使用技巧 Decode() 函数是 Oracle PL/SQL 中功能强大的函数之一,目前只有 Oracle 公司的 SQL 提供了此函数,其他数据库厂商的 SQL 实现还没有此功能。Decode 函数有什么用途呢?下面我们...

    Oracle中Decode()函数使用技巧.doc

    Oracle 中 Decode() 函数使用技巧 Decode 函数是 Oracle PL/SQL 中的一种功能强大的函数,现只有 Oracle 公司的 SQL 提供了此函数,其他数据库厂商的 SQL 实现还没有此功能。 Decode 函数可以用来实现多种逻辑判断...

    关于oracle decode函数的用法

    关于oracle decode函数的用法

    Oracle 中 decode 函数用法

    decode(条件,值1,返回值1,值2,返回值2,…值n,返回值n,缺省值) 该函数的含义如下: IF 条件=值1 THEN  RETURN(翻译值1) ELSIF 条件=值2 THEN  RETURN(翻译值2)  ...... ELSIF 条件=值n THEN  RETURN(翻译值n) ...

    Oracle中Decode()函数的有关用法

    Oracle中Decode()函数的有关用法Oracle中Decode()函数的有关用法

    oracle的decode函数

    DECODE函数相当于一条件语句(IF).它将输入数值与函数中的参数列表相比较,根据输入值返回一个对应值。

    ORACLE 列转行 DECODE函数用法

    在Oracle数据库中,DECODE函数是一个非常实用的工具,它允许你在SQL查询中进行条件判断,将特定的值转换为其他值。DECODE函数在处理列转行的问题时,尤其适用于将多列数据合并到一行中,使得数据展示更加简洁明了。...

    decode函数.docx

    decode 函数也可以与其他函数结合使用,例如 LPAD 函数,可以实现一些复杂的字符串处理,例如: ```sql SELECT LPAD(DECODE(COUNT(记录编号),0,1,MAX(TO_NUMBER(记录编号)+1)),14,'0') 记录编号 FROM tetdmis ``` ...

    oracle中decode函数的使用方法

    Oracle中的`DECODE`函数是一种非常实用的工具,它允许你在SQL查询中执行简单的条件判断,类似于编程语言中的三元运算符或者IF-THEN-ELSE结构。`DECODE`函数的基本语法是: ```sql DECODE(条件, 值1, 返回值1, 值2, ...

    Oracle DECODE函数语法使用介绍

    Oracle DECODE函数功能很强,下面就为您详细介绍Oracle DECODE函数的用法,希望可以让您对Oracle DECODE函数有更多的了解。 Oracle DECODE函数 Oracle DECODE函数是Oracle公司独家提供的功能,它是一个功能很强的...

    oracle中decode函数的使用方法示例

    Oracle中的DECODE函数是一个非常实用的工具,它允许我们在SQL查询中进行条件判断,并返回相应的值。DECODE函数的基本语法如下: ```sql DECODE(value, if1, then1, if2, then2, ..., else) ``` 1. **DECODE用于...

    Oracle中DECODE()函数的使用法

    DECODE()函数,它将输入数值与函数中的参数列表相比较,根据输入值返回一个对应值。区别于SQL的其它函数,DECODE函数还能识别和操作空值。本文详细介绍了DECODE函数的语法。

    Oracle-Decode()函数和CASE语句的比较

    本文讲述了Oracle-Decode()函数和CASE语句的比较。

    decode函数[归类].pdf

    DECODE函数是Oracle PL/SQL中的一个独特特性,它在软件开发,尤其是数据库查询中,提供了一种简洁而高效的方法来进行条件判断和数据转换。这个函数在Oracle SQL中扮演了if-then-else逻辑的角色,允许开发者在SQL查询...

    decode函数借鉴.pdf

    DECODE 函数的应用非常广泛,例如,在 Oracle 系统中就有许多数据字典是使用 DECODE 思想设计的,比如记录会话信息的 V$SESSION 数据字典视图。我们可以使用 DECODE 函数来实现表的转置,例如: select sid, serial...

    Oracle用decode函数或CASE-WHEN实现自定义排序

    1. **DECODE函数** Oracle的`DECODE`函数是一种条件判断函数,它可以用于对数据进行简单的多值比较和返回。在自定义排序场景中,`DECODE`函数可以将不同的字段值映射到对应的排序值。以下是一个例子: ```sql ...

Global site tag (gtag.js) - Google Analytics