看一条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. createwindow

    WNDCLASS wndclass; wndclass.hbrBackground=(HBRUSH)getstockobject(WHITE_BRUSH); wndclass.hCursor=Load ...

  2. TreeView无刷新获取text及value

    前台代码: <html xmlns="http://www.w3.org/1999/xhtml"><head id="Head1" runat ...

  3. ECSTORE 关于前台页面DIALOG的调用

    在需调用dialog的html页面中插入本段代码. link='<{link app=test ctl=site_test act=abc}>'; var dialog = new Dia ...

  4. JQUERY1.9学习笔记 之基本过滤器(六) 页眉选择器

    页眉选择器jQuery( ":header" ) 描述:选择页眉的所有标签,如 h1,h2, h3 等. <!DOCTYPE html><html lang=&q ...

  5. java中ExecutorService接口

    一.声明 public interface ExecutorService extends Executor 位于java.util.concurrent包下 所有超级接口:Executor 所有已知 ...

  6. Python3 如何优雅地使用正则表达式(详解五)

    非捕获组命名组 精心设计的正则表达式可能会划分很多组,这些组不仅可以匹配相关的子串,还能够对正则表达式本身进行分组和结构化.在复杂的正则表达式中,由于有太多的组,因此通过组的序号来跟踪和使用会变得困难 ...

  7. 魔法方法:构造和析构 - 零基础入门学习Python041

    魔法方法:构造和析构 让编程改变世界 Change the world by program 构造和析构 什么是魔法方法呢?我们来系统总结下: - 魔法方法总是被双下划线包围,例如__init__ - ...

  8. 怎么看时序图--nand flash的读操作详解(转载)

    出处:http://blog.chinaunix.net/uid-28852942-id-3992727.html这篇文章不是介绍 nand flash的物理结构和关于nand flash的一些基本知 ...

  9. 剑指offer之O(1)算法删除指针所指向的节点

    题目如图: 1.把要删除pToBeDeleted的节点的后面节点覆盖点要删除的节点pToBeDeleted 2.要考虑如果删除的节点是最后一个节点怎么办 3.要考虑如果总共只有一个节点,删除的是头结点 ...

  10. spark1.1.0源码阅读-dagscheduler and stage

    1. rdd action ->sparkContext.runJob->dagscheduler.runJob def runJob[T, U: ClassTag]( rdd: RDD[ ...