ORA-00933:SQL命令在表别名上未正确结束

时间:2019-02-26 18:32:32

标签: sql oracle

在使它起作用时遇到问题。每个查询单独工作,但是当我尝试为每个子查询加上别名以使Join工作时,出现错误:

SQL命令未正确结束。任何建议将不胜感激。

Select C.* 
From 
    (
        (
            Select 
                a.*,
                Row_Number() Over(Partition By referral_id Order By start_date,line) as rn 
            from hcclsc.referral_Bed_Day a
        ) A
        Inner Join (
            Select
                a.referral_id,
                max(rn) as vn
            From (
                Select
                    referral_id,
                    line,
                    bed_day_type_id,
                    start_date,
                    end_date,
                    Row_Number() Over(Partition by referral_id Order By start_date, line) as rn
                From HCCLSC.referral_Bed_Day 
            ) a
            Group by referral_id
        ) B
        On A.referral_ID = B.referral_id and a.rn = b.vn
    ) C

2 个答案:

答案 0 :(得分:1)

您正在将A加入B,但是没有从那些加入的子查询中选择任何内容。

此部分:

Select C.* 
From 
    (
        (

需要指定要从联接中选择的列;如果您希望同时从两个子查询中获取所有信息,则:

Select C.* 
From 
    (
        Select *
        From
            (

但是子查询的额外级别实际上并没有添加任何内容,您可以删除C级别:

Select * 
From 
    (
        Select 
            a.*,
            Row_Number() Over(Partition By referral_id Order By start_date,line) as rn 
        from hcclsc.referral_Bed_Day a
    ) A
    Inner Join (
        Select
            a.referral_id,
            max(rn) as vn
        From (
            Select
                referral_id,
                line,
                bed_day_type_id,
                start_date,
                end_date,
                Row_Number() Over(Partition by referral_id Order By start_date, line) as rn
            From HCCLSC.referral_Bed_Day 
        ) a
        Group by referral_id
    ) B
    On A.referral_ID = B.referral_id and a.rn = b.vn

答案 1 :(得分:0)

使用cte尝试以下操作

with cte as
(
Select
    a.referral_id
    max(rn) as vn
    From
        (Select
        referral_id
        ,line
        ,bed_day_type_id
        ,start_date
        ,end_date
        ,Row_Number() Over(Partition by referral_id Order By start_date, line) as rn
        From
        HCCLSC.referral_Bed_Day
    ) a Group by referral_id
), cte2 as
(
Select
    a.*
    ,Row_Number() Over(Partition By referral_id Order By start_date,line) as rn
    from
    hcclsc.referral_Bed_Day a
) select A.*,B.* from cte A join cte2 B 
 on  A.referral_ID = B.referral_id and A.vn= B.rn