昨天SQL优化的续集,不过这次不是优化的问题,而是时间没有值的时候取0用什么函数.
(可能是个极其简单的问题,但本人确实不知道)
select /*+ no_index(ith1) */ ip.part_no, max(ith1.dated) indate,max(ith2.dated) outdate
from ifsapp.inventory_transaction_hist_tab ith1,
ifsapp.inventory_transaction_hist_tab ith2,
ifsapp.inventory_part_tab ip
where ith1.part_no(+) = ip.part_no
and ith2.part_no(+)=ip.part_no
and ith1.contract = ip.contract
and ith2.contract = ip.contract
and ith1.transaction_code = 'OOREC'
and ith2.transaction_code = 'OESHIP'
and ith1.date_applied >= to_date('2007-1-1', 'yyyy-mm-dd')
and ith2.date_applied >= to_date('2007-1-1', 'yyyy-mm-dd')
and ip.contract = 'SKY'
and ip.sk_part_category_db = '1'
group by ip.part_no
问题:
有可能ith1.dated有值时,ith2.dated没有值.反之亦然。上记语句会漏掉其中一个时间没有值的这一部分。
只取出两者都有值的部分.如果是数字,可用NVL(),时间用什么?(排除转换为数字再转换回时间)
当然DECODE也可以,不过肯定会影响性能.
有请
[ 本帖最后由 zhangweicai74 于 2008-5-23 11:13 编辑 ]
(可能是个极其简单的问题,但本人确实不知道)
select /*+ no_index(ith1) */ ip.part_no, max(ith1.dated) indate,max(ith2.dated) outdate
from ifsapp.inventory_transaction_hist_tab ith1,
ifsapp.inventory_transaction_hist_tab ith2,
ifsapp.inventory_part_tab ip
where ith1.part_no(+) = ip.part_no
and ith2.part_no(+)=ip.part_no
and ith1.contract = ip.contract
and ith2.contract = ip.contract
and ith1.transaction_code = 'OOREC'
and ith2.transaction_code = 'OESHIP'
and ith1.date_applied >= to_date('2007-1-1', 'yyyy-mm-dd')
and ith2.date_applied >= to_date('2007-1-1', 'yyyy-mm-dd')
and ip.contract = 'SKY'
and ip.sk_part_category_db = '1'
group by ip.part_no
问题:
有可能ith1.dated有值时,ith2.dated没有值.反之亦然。上记语句会漏掉其中一个时间没有值的这一部分。
只取出两者都有值的部分.如果是数字,可用NVL(),时间用什么?(排除转换为数字再转换回时间)
当然DECODE也可以,不过肯定会影响性能.
有请
![64.gif](https://i-blog.csdnimg.cn/blog_migrate/995c9d7ffdd2f7f2295800c8903b7b58.png)
[ 本帖最后由 zhangweicai74 于 2008-5-23 11:13 编辑 ]
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12391917/viewspace-310109/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/12391917/viewspace-310109/