看一条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. Linux编程基础——GDB(设置断点)(转:TianFang,cnblog: http://www.cnblogs.com/TianFang/archive/2013/01/20/2868889.html)

    启动GDB后,首先就是要设置断点,程序中断后才能调试.在gdb中,断点通常有三种形式: 断点(BreakPoint): 在代码的指定位置中断,这个是我们用得最多的一种.设置断点的命令是break,它通 ...

  2. PHPCMS V9网站更换域名的方法

    网站在发展的过程中,很可能多次的修改域名.那么在phpcms v9中我们要怎么进行设置呢? 请进行以下步骤的修改: 1.修改/caches/configs/system.php里面所有和域名有关的,把 ...

  3. iOS推送 再备

    这是一篇编译的文章,内容均出自Parse.com的iOS开发教程,同时作者还提供了视频讲解.本文将带领开发者一步一步向着iOS推送通知的深处探寻,掌握如何配置iOS推送通知的奥义. 介绍一点点背景资料 ...

  4. css3 之表格隔行分色显示

    <html> <head> <title></title> <style type="text/css"> table{ ...

  5. Javascript Promise 学习(上)

    Promise 就是处理异步的一个规范方法 a();b();alert("a");如果a() 里面有一个ajax 或者settimeout 那么alert("a" ...

  6. Gridview中修改某列的背景色

    Gridview中状态列的值是1,某列的背景是是绿色状态字段是:archivesStatus protected void gvInfo_RowDataBound(object sender, Gri ...

  7. 51单片机C语言学习笔记7:关于.c文件和.h文件

    1)h文件作用 1 方便开发:包含一些文件需要的共同的常量,结构,类型定义,函数,变量申明: 2 提供接口:对一个软件包来说可以提供一个给外界的接口(例如: stdio.h). 2)h文件里应该有什么 ...

  8. 【HDOJ】1493 QQpet exploratory park

    超水的动态规划.最后要对概率求Sigma. #include <cstdio> #include <cstring> #include <cstdlib> #def ...

  9. SDL2.0 学习笔记-1 windows下的第一个测试程序

    SDL全称是Simple DirectMedia Layer,是一个开源的.跨平台(win32,linux,mac)的多媒体开发c语言库. 官方网站 http://www.libsdl.org/ 第一 ...

  10. Android studio 开发中 用git实现批量忽略特定文件的方法

    git实现批量忽略特定文件的方法 在用AndroidStudio开发项目的时候,3个人协同开发,那么用Git同步代码,会将模块中的大量iml文件同步,每次都会提交和更新,一个一个的去忽略他们,显然是最 ...