看一条sql

select q.*, r.goods_name
from (select nvl(t.goods_code, s.goods_code) goods_code,
t.buy_open_price,
t.buy_position_price,
t.buy_position_amount,
s.sale_open_price,
s.sale_position_price,
s.sale_position_amount,
(nvl(t.buy_position_amount, 0) -
nvl(s.sale_position_amount, 0)) position_amount
from (select opo.goods_code,
opo.position_bs,
sum(opo.open_price * opo.position_amount) /
sum(opo.position_amount) buy_open_price,
sum(opo.position_price * opo.position_amount) /
sum(opo.position_amount) buy_position_price,
sum(opo.position_amount) buy_position_amount
from otc_trade_position_open opo
where opo.position_bs = ''
and opo.account_type = ''
and position_bs = '2'
group by opo.goods_code, opo.position_bs) t
full outer join (select opo.goods_code,
opo.position_bs,
sum(opo.open_price * opo.position_amount) /
sum(opo.position_amount) sale_open_price,
sum(opo.position_price *
opo.position_amount) /
sum(opo.position_amount) sale_position_price,
sum(opo.position_amount) sale_position_amount
from otc_trade_position_open opo
where opo.position_bs = ''
and opo.account_type = ''
and position_bs = '2'
group by opo.goods_code, opo.position_bs) s
on (t.goods_code = s.goods_code)) q
left outer join otc_goods r
on (q.goods_code = r.goods_code)

结果如下


再看下一个sql

select q.*, r.goods_name
from (select nvl(t.goods_code, s.goods_code) goods_code,
t.buy_open_price,
t.buy_position_price,
t.buy_position_amount,
s.sale_open_price,
s.sale_position_price,
s.sale_position_amount,
(nvl(t.buy_position_amount, 0) -
nvl(s.sale_position_amount, 0)) position_amount
from (select opo.goods_code,
opo.position_bs,
sum(opo.open_price * opo.position_amount) /
sum(opo.position_amount) buy_open_price,
sum(opo.position_price * opo.position_amount) /
sum(opo.position_amount) buy_position_price,
sum(opo.position_amount) buy_position_amount
from otc_trade_position_open opo
where opo.position_bs = ''
and opo.account_type = ''
and position_bs = '1'
group by opo.goods_code, opo.position_bs) t
full outer join (select opo.goods_code,
opo.position_bs,
sum(opo.open_price * opo.position_amount) /
sum(opo.position_amount) sale_open_price,
sum(opo.position_price *
opo.position_amount) /
sum(opo.position_amount) sale_position_price,
sum(opo.position_amount) sale_position_amount
from otc_trade_position_open opo
where opo.position_bs = ''
and opo.account_type = ''
and position_bs = '1'
group by opo.goods_code, opo.position_bs) s
on (t.goods_code = s.goods_code)) q
left outer join otc_goods r
on (q.goods_code = r.goods_code)


查询方法一样,就是条件变了一下,可是联合查询的商品名称却在上面一个里面查询不出来,而下面一个里面显示正常。分析原因,nvl函数的问题,nvl(t.goods_code, s.goods_code) 前面一个查询语句里面,goods_code是取的s.goods_code,后面一个取的是t.goods_code.虽然值一样,但是前面一个联合查询却查询不出来,有点诡异。


解决方法:换成nvl2, 将上面代码里面的 nvl(t.goods_code,s.goods_code)换成nvl2(t.goods_code, t.goods_code, s.goods_code)上面存在的问题即可解决。

oracle nvl()函数在使用中出现的问题的更多相关文章

  1. oracle nvl()函数

    oracle的nvl()函数作用是当第一个值不为null时,返回第一个值,否则返回第二个值. 当第一个值为一个运算表达式时,那么第二个的值被限定为只能是NUMBER类型或者能隐式转换为NUMBER类型 ...

  2. Oracle NVL 函数 nvl nvl2

    Oracle中函数以前介绍的字符串处理,日期函数,数学函数,以及转换函数等等,还有一类函数是通用函数.主要有:NVL,NVL2,NULLIF,COALESCE,这几个函数用在各个类型上都可以. 下面简 ...

  3. Oracle nvl()函数处理null值

    首先我先说一下什么是Oracle的函数,曾经有一位大牛,让我说说熟悉的oracle函数,我当时竟一头雾水,心想“什么oracle函数啊,不就是那些SQL语句吗“,当时我竟然说出了select之类的回答 ...

  4. oracle nvl和nvl2的区别

    一直用oracle nvl函数,最近发现还有一个nvl2函数: nvl(a,b) 如果a不为null 则返回a,如果a为null则返回b; nvl2(a,b,c) ,如果a不为null 则返回b,如果 ...

  5. nvl函数 oracle

    Oracle中函数以前介绍的字符串处理,日期函数,数学函数,以及转换函数等等,还有一类函数是通用函数.主要有:NVL,NVL2,NULLIF,COALESCE,这几个函数用在各个类型上都可以. 下面简 ...

  6. Oracle中的NVL函数

    Oracle中函数以前介绍的字符串处理,日期函数,数学函数,以及转换函数等等,还有一类函数是通用函数.主要有:NVL,NVL2,NULLIF,COALESCE,这几个函数用在各个类型上都可以. 下面简 ...

  7. Oracle nvl(),nvl2()函数介绍

    NVL函数 Oracle/PLSQL中的一个函数. 格式为: NVL( string1, replace_with) 功能:如果string1为NULL,则NVL函数返回replace_with的值, ...

  8. oracle的nvl函数的使用解析

    Oracle的Nvl函数 nvl( ) 函数 从两个表达式返回一个非null 值. 语法 NVL(eExpression1, eExpression2) 参数 eExpression1, eExpre ...

  9. Oracle的nvl函数和nvl2函数

    一.基本语法 介绍一下oracle的nvl函数和nvl2函数. nvl函数 nvl函数基本语法为nvl(E1,E2),意思是E1为null就返回E2,不为null就返回E1. nvl2函数 nvl2函 ...

随机推荐

  1. MVC模式下xml文件的解析

    第一次写blog,组织不当和出错的地方还请大家多担当哈. java操作xml文件的方式中用的较多的有四种,DOM.SAX.JDOM.DOM4J.除第一种外其余的三种我都有试过,这后三种方案中我选择用S ...

  2. Thinkphp 模版

    1.显示模版 在Home/Controller/MainController.class.php中写一个方法来显示对应的模版 function text() { //变量输出 $this->as ...

  3. Ubuntu 12.04(32位)下PHP环境的搭建(LAMP)

    Ubuntu 12.04 32位 下默认安装为5.3.10  不是以下图文中的5.4 1.首先打开命令行,切换到root身份,获得最新的软件包 su root sudo apt-get install ...

  4. C#中的枚举器(转)

    术语表 Iterator:枚举器(迭代器) 如果你正在创建一个表现和行为都类似于集合的类,允许类的用户使用foreach语句对集合中的成员进行枚举将会是很方便的.这在C# 2.0中比 C# 1.1更容 ...

  5. C++学习笔记1——const

    Const 限定符 1. 等价 //const对象必须初始化//C++中const修饰的变量不能改变//C中const修饰的变量可以通过指针修改 2. ; const int j = i;//变量给常 ...

  6. android弹出式菜单、弹出式对话框、弹出式窗口

    http://www.open-open.com/lib/view/open1389767042601.html http://www.open-open.com/lib/view/open13321 ...

  7. Attach file to database

    D:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA databaseName.mdf databaseName.l ...

  8. JVM虚拟机栈和本地方法栈溢出测试

    弄JAVA,那JVM,JAVA语法,JDK库,JAVAEE,流行框架是一个都不能少,才可以有全局感的. JVM高级特性这书,看得差不多了.慢慢实践. /** * * *VM Args: -Xms20m ...

  9. vector,list和deque区别

    stl提供了三个最基本的容器:vector,list,deque. vector和built-in数组类似,它拥有一段连续的内存空间,并且起始地址不变,因此它能非常好的支持随即存取,即[]操作符,但由 ...

  10. 【HDOJ】1497 Simple Library Management System

    链表. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXM 1001 #def ...