1

我有这个代码,它给我总结了每部电影的售票座位:

SELECT mName, SUM(soldSeats)
FROM movie, show, prog
WHERE movie.movieID = prog.movieID
AND prog.showID = show.showID
GROUP BY mName 

问题是要找到售出座位数最多的电影,我试图添加这个,但只得到没有行:

HAVING SUM(soldseats) = (SELECT MAX(SUM(soldseats)) FROM show
GROUP BY solgteplasser)

有人有建议吗?以下是表格的外观:活动总和

4

1 回答 1

2
Select test.* 
from 
(Select movie.mName, SUM(show.soldSeats) as soldseatsum
FROM movie, show, prog
WHere movie.movieId = prog.movieID
AND prog.showID = show.showID
Group by movie.mName
Order by soldseatsum DESC) test
Where rownum<=1

通过这种方式,您可以按 SUM 降序排列您的选择,并且您仅使用 Rownum 选择第一行,这是具有最高 SUm 的行。

编辑:另外,如果你的 SUM 列中有 Null,请确保在 ORder By 之后添加一个 NULLS LAST,如下所示:

.....
.....
Group by movie.mName
    Order by soldseatsum DESC NULLS LAST) test
    Where rownum<=1
于 2012-11-15T13:13:43.057 回答