Oracle Select语句出错

时间:2016-10-20 08:30:38

标签: sql oracle

我在表格中有一个字段BillNumber,可以重复帐单编号,我想获取某个帐单的最后一行,我使用的这个语句在Sql Server上运行正常但在Oracle数据库上没有:

select MB.* 
from SADAD.MAINBILLDETAILS AS MB 
   inner join (select BILLNUMBER, Max(CREATEDON) AS LastDate 
               from SADAD.MAINBILLDETAILS 
               group by BILLNUMBER) As LastBill On MB.BILLNUMBER = LastBill.BILLNUMBER AND MB.CREATEDON = LastBill.LastDate 
where not ISREJECTED Is Null 
  and SubStr(MB.BILLNUMBER,1,3) ='301' 
  and CREATEDON Between To_Date('2016/07/19', 'YYYY/MM/DD') 
  and To_Date('2016/10/19', 'YYYY/MM/DD') 
order by CREATEDON

错误讯息:

  

ORA-00933:命令未正确结束

2 个答案:

答案 0 :(得分:2)

您在子查询As LastBill的别名中出错 - 删除as。正确的版本应该是

Select MB.*  
  From SADAD.MAINBILLDETAILS  MB 
 Inner Join (Select BILLNUMBER, Max(CREATEDON) AS LastDate 
             From SADAD.MAINBILLDETAILS 
            Group By BILLNUMBER) LastBill On MB.BILLNUMBER = LastBill.BILLNUMBER AND MB.CREATEDON = LastBill.LastDate 
Where Not ISREJECTED Is Null 
  And SubStr(MB.BILLNUMBER,1,3) ='301' 
  And CREATEDON Between To_Date('2016/07/19', 'YYYY/MM/DD') And To_Date('2016/10/19', 'YYYY/MM/DD') 
Order By CREATEDON

答案 1 :(得分:0)

奇怪的是,Oracle不接受表别名的关键字 AS ,仅用于列别名。

select * from dual as d;
ORA-00933: SQL command not properly ended
00933. 00000 -  "SQL command not properly ended"
*Cause:    
*Action:
Error at Line: 1 Column: 20
select * from dual d;
DUMMY
-----
X