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函 ...
随机推荐
- crtmpserver组网方案
A Powerful Live Streaming Setup 搭建强大的直播系统 Recently we had a project requiring live streaming setup, ...
- Css3执行后显示最后一针
-webkit-animation-fill-mode: both; animation-fill-mode: both;
- 比file_get_contents稳定的curl_get_contents
相信使用过file_get_contents函数的朋友都知道,当获取的$url访问不了时,会导致页面漫长的等待,甚至还能导致PHP进程占用CPU达100%,因此这个函数就诞生了 分享一个实际在用的函数 ...
- bat判断某个目录是否存在
使用批处理判断某个目录是否存在的语句. if not exist xxx_folder_name md xxx_folder_name
- php异步调用方法实现示例
php 异步调用方法 客户端与服务器端是通过HTTP协议进行连接通讯,客户端发起请求,服务器端接收到请求后执行处理,并返回处理结果. 有时服务器需要执行很耗时的操作,这个操作的结果并不需要返回 ...
- iOS 数据库操作(使用FMDB)
iOS 数据库操作(使用FMDB) iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.Plausibl ...
- 简单DOS命令实现局域网Windows远程关机
1秒内重启局域网内计算机名为ppgsvr-pc的用户电脑shutdown -r -m \\ComputerName -t 1 1秒内关闭局域网内ppgsvr用户电脑shutdown -s -m \\C ...
- Decimal Basic 学习笔记(1)
定义变量 LET a 输入变量值 INPUT a INPUT a,b 运算结果绝对值小于1前面的0省略,科学计数 PRINT语句 数值直接写,字符串用“” 通过 分号: 和 逗号,来分隔显示两个项目 ...
- 通过MYSQL命令行直接建数据库
因为同事要求,在进行MYSQLDUMP的导入库时,加一个日期标签, BUT,MYSQLDUMP的前提是数据库中要存在数据库名, 于是通过-e 参数执行命令. 这让我想起了前不久请教同事,如果AWK传变 ...
- 交叉编译环境以及开发板上-/bin/sh: ./hello: not found(使用arm-linux-gcc -static -o 来进行静态编译)
目标板是S3C2440.至于交叉编译环境的搭建就不多说了,网上很多教程. 搭建好了交叉编译环境后,第一件事就是传说中的”Hello,World!”. 一. 主机编译环节 我使用的系统是ubuntu10 ...