使用group by和lookup表

时间:2012-03-04 20:19:31

标签: sql oracle group-by

我有3个表,我想从中创建一个查询:

参赛者:
CompetitionID
BandID

BandResult
...
...

ResultPoints:
bandResult

频段:
BandID
BandName

斜体=外键,粗体=主键

这就是我目前所拥有的:

SELECT E.bandID, SUM(RP.points)
  FROM entrants E, resultPoints RP
 WHERE E.bandResult = RP.bandResult
 GROUP BY E.bandID
 ORDER BY SUM(RP.points) DESC;

我想也包括bandName但是当我通过停止工作包括这个组时:

SELECT E.bandID, B.bandName, SUM(RP.points)
  FROM entrants E, resultPoints RP, band B
 WHERE E.bandResult = RP.bandResult
   AND B.bandID = E.bandID
 GROUP BY E.bandID
 ORDER BY SUM(RP.points) DESC;

关于如何使这项工作的任何想法?

2 个答案:

答案 0 :(得分:1)

尝试将bandName添加到group by子句中。

答案 1 :(得分:1)

试试这个:

SELECT E.bandID, B.bandName, SUM(RP.points)
FROM entrants E, resultPoints RP, band B
WHERE E.bandResult = RP.bandResult
AND B.bandID = E.bandID
GROUP BY E.bandID, B.bandname
ORDER BY SUM(RP.points) DESC;