oracle nvl()函数在使用中出现的问题
看一条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()函数在使用中出现的问题的更多相关文章
- oracle nvl()函数
oracle的nvl()函数作用是当第一个值不为null时,返回第一个值,否则返回第二个值. 当第一个值为一个运算表达式时,那么第二个的值被限定为只能是NUMBER类型或者能隐式转换为NUMBER类型 ...
- Oracle NVL 函数 nvl nvl2
Oracle中函数以前介绍的字符串处理,日期函数,数学函数,以及转换函数等等,还有一类函数是通用函数.主要有:NVL,NVL2,NULLIF,COALESCE,这几个函数用在各个类型上都可以. 下面简 ...
- Oracle nvl()函数处理null值
首先我先说一下什么是Oracle的函数,曾经有一位大牛,让我说说熟悉的oracle函数,我当时竟一头雾水,心想“什么oracle函数啊,不就是那些SQL语句吗“,当时我竟然说出了select之类的回答 ...
- oracle nvl和nvl2的区别
一直用oracle nvl函数,最近发现还有一个nvl2函数: nvl(a,b) 如果a不为null 则返回a,如果a为null则返回b; nvl2(a,b,c) ,如果a不为null 则返回b,如果 ...
- nvl函数 oracle
Oracle中函数以前介绍的字符串处理,日期函数,数学函数,以及转换函数等等,还有一类函数是通用函数.主要有:NVL,NVL2,NULLIF,COALESCE,这几个函数用在各个类型上都可以. 下面简 ...
- Oracle中的NVL函数
Oracle中函数以前介绍的字符串处理,日期函数,数学函数,以及转换函数等等,还有一类函数是通用函数.主要有:NVL,NVL2,NULLIF,COALESCE,这几个函数用在各个类型上都可以. 下面简 ...
- Oracle nvl(),nvl2()函数介绍
NVL函数 Oracle/PLSQL中的一个函数. 格式为: NVL( string1, replace_with) 功能:如果string1为NULL,则NVL函数返回replace_with的值, ...
- oracle的nvl函数的使用解析
Oracle的Nvl函数 nvl( ) 函数 从两个表达式返回一个非null 值. 语法 NVL(eExpression1, eExpression2) 参数 eExpression1, eExpre ...
- Oracle的nvl函数和nvl2函数
一.基本语法 介绍一下oracle的nvl函数和nvl2函数. nvl函数 nvl函数基本语法为nvl(E1,E2),意思是E1为null就返回E2,不为null就返回E1. nvl2函数 nvl2函 ...
随机推荐
- A Bug's Life(hdu1829种类并查集)
题意:有一群虫子,现在给你一些关系,判断这些关心有没有错 思路:向量种类并查集,下面讲一下向量的种类并查集 本题的各个集合的关心有两种0同性,1异性,怎么判断有错, 1.先判断他们是否在一个集合,即父 ...
- 准备开发一个基于canvas的图表库,记录一些东西(一)
开源的图表库已经有很多了,这里从头写个自己的,主要还是 提高自己js的水平,增加复杂代码组织的经验 首先写一个画图的库,供以后画图表使用.经过2天的开发,算是能拿出点东西了,虽然功能还很弱,但是有了一 ...
- eclipse下编译openfire3.9.1源码
[一].下载源码 打开网址:http://www.igniterealtime.org/downloads/source.jsp 选择目前最新版本 openfire_src_3_9_1.zip 下载. ...
- template of class
class template will call the constructor of its member object before constructor itself......
- jdk-map-hashMap
关于java中的HashMap,我们在项目中经常使用到,但是我们的场景是否使用对了呢? 下面分为四个部分来阐述我的HashMap的理解 1.为什么要使用hashMap? 在项目中,需求的实现需要使用到 ...
- asp.net core VS goang web[修正篇]
先前写过一篇文章:http://www.cnblogs.com/gengzhe/p/5557789.html,也是asp.net core和golang web的对比,热心的园友提出了几点问题,如下: ...
- WPC文件修改还原pin进度
原文地址:http://wenku.baidu.com/link?url=KZRR6VtW_Yn59iEbrWYz15jOH6hSVgjyow8nvwHfQr3eQgvCcc9IgYCT-fWwVWf ...
- android百度定位
package com.aihunqin.test; import android.app.Activity; import android.os.Bundle; import android.wid ...
- Android菜单详解(一)——理解android中的Menu
前言 今天看了pro android 3中menu这一章,对Android的整个menu体系有了进一步的了解,故整理下笔记与大家分享. PS:强烈推荐<Pro Android 3>,是我至 ...
- uboot之board.c源码分析
/lib_arm/board.c 主要完成了一些初始化的操作,最重要的是有start_armboot函数 _armboot_start地址为多少?? /* * * U-Boot code: 00F00 ...