“ORA-00933:SQL命令未正确结束”使用Group By Clause时出错

时间:2017-04-30 23:24:38

标签: sql oracle

我做了一些查询而且我一直得到相同的“ORA-00933:SQL命令未正确结束”错误。所以这里是:

- 拥有最多流的歌曲艺术家

SELECT ArtistID, NumberofStreams 
FROM Songs 
GROUP BY SongID 
ORDER BY NumberOfStreams DESC 
LIMIT 1;

- 返回最早的专辑及其艺术家

SELECT ArtistName, AlbumName 
FROM Album 
ORDER BY ReleaseDate ASC 
LIMIT 1;  

- 返回歌曲最多的歌手

SELECT ArtistID, COUNT(SongID) 
FROM Songs 
GROUP BY ArtistID 
ORDER By COUNT(SongID) DESC 
LIMIT 1; 

表格设置如下:

歌曲(SongID,ArtistID,SONGNAME,SONGNAME,SongLength)

相册(ALBUMID,ALBUMNAME,ArtistID,ARTISTNAME,NumberOfTracks,RELEASEDATE)

我认为这与我的ORDER BY或LIMIT有关,但我无法弄明白

1 个答案:

答案 0 :(得分:2)

Oracle不支持LIMIT。您可以使用子查询重写这些:

SELECT x.*
FROM (SELECT ArtistID, NumberofStreams 
      FROM Songs 
      GROUP BY SongID 
      ORDER BY NumberOfStreams DESC 
     ) x
WHERE rownum = 1;

Oracle 12c +支持更简单和标准的fetch first 1 row only

      SELECT ArtistID, NumberofStreams 
      FROM Songs 
      GROUP BY SongID 
      ORDER BY NumberOfStreams DESC 
      FETCH FIRST 1 ROW ONLY