日期:2014-05-17  浏览次数:20982 次

这个SQL(sybase)在ORACLE下怎么写?
原来在SYBASE下有这样的SQL。

select  
        a.id,  
        b.id,   b.cd,   b.up,  
        c.b_cd,
        d.grp_cd,   d.rate_cd,   d.age
from  
        A   a,   B   b,   C   c,   D   d  
where  
        a.name   =   'testName '  
        and   (a.sale_fromdt   <=   '20070501 '   and   a.sale_todt   > =   '20070501 ')  
        and   substring(b.cd,1,2)   =*   a.b_cd  
        and   b.ntype   =   'D '  
        and   b.up   =   '00 '  
        and   c.b_cd   =*   a.b_cd  
        and   d.grp_cd   +   d.rate_cd   =*   c.grp_cd   +   c.rate_cd

当转到ORACLE后,我对这条SQL进行了修改如下:

select  
        a.id,  
        b.id,   b.cd,   b.up,  
        c.b_cd,
        d.grp_cd,   d.rate_cd,   d.age
from  
        A   a,   B   b,   C   c,   D   d  
where  
        a.name   =   'testName '  
        and   (a.sale_fromdt   <=   '20070501 '   and   a.sale_todt   > =   '20070501 ')  
        and   substr(b.cd(+),1,2)   =   a.b_cd  
        and   b.ntype(+)   =   'D '  
        and   b.up(+)   =   '00 '  
        and   c.b_cd(+)   =   a.b_cd  
        and   d.grp_cd(+)   ||   d.rate_cd(+)   =   c.grp_cd(+)   ||   c.rate_cd(+)

执行报错。请问,该如何修改。
请大家帮帮忙,我的SQL水平不是太好。

------解决方案--------------------
试试外连接:

select
a.id,
b.id, b.cd, b.up,
c.b_cd,
d.grp_cd, d.rate_cd, d.age
from
A a
left join
B b
on
a.b_cd = substr(b.cd,1,2) and b.ntype = 'D ' and b.up = '00 '
left join
C c
on
a.b_cd = c.b_cd
left join
D d
on
d.grp_cd||d.rate_cd=c.grp_cd||c.rate_cd
where
a.name = 'testName '
and
(a.sale_fromdt <= '20070501 ' and a.sale_todt > = '20070501 ')
------解决方案--------------------
我有一点不理解楼主,你的sql稍微改一点不就得了。
select
a.id,
b.id, b.cd, b.up,
c.b_cd,
d.grp_cd, d.rate_cd, d.age
from
A a, B b, C c, D d
where
a.name = 'testName '
and (a.sale_fromdt <= '20070501 ' and a.sale_todt > = '20070501 ')
and substr(b.cd(+),1,2) = a.b_cd
and b.ntype(+) = 'D '
and b.up(+) = '00 '
and c.b_cd(+) = a.b_cd
and d.grp_cd(+) || d.rate_cd(+) = c.grp_cd || c.rate_cd

只是把c.grp_cd(+) || c.rate_cd(+)后面的(+