我有这样的数据库结构:
Table - lodges
LodgeID (PK)
Lodge
etc
Table - scores
ScoreID (PK)
Score
CategoryID
LodgeID (FK)
我试图以以下形式返回结果:
LodgeID,Lodge,Category,该类别中的得分数,该类别中的平均得分
例如,如果我有:
小屋
LodgeID, Lodge
1, Lodge One
2, Lodge Two
分数
ScoreID, Score, CategoryID, LodgeID
1, 3, 101, 1
2, 5, 101, 1
3, 7, 101, 1
4, 10, 102, 2
5, 20, 102, 2
6, 30, 102, 2
7, 40, 102, 2
我想回来:
1, Lodge One, 3, 5
2, Lodge Two, 4, 25
我一直在尝试这样的事情:
SELECT COUNT(ScoreID) as scoreCount, AVG(Score) as AverageScore, Lodge
FROM scores_temp
INNER JOIN lodges_temp ON scores_temp.LodgeID = lodges_temp.LodgeID
SELECT lodges_temp.LodgeID,Lodge,COUNT(ScoreID)为scoreCount,AVG(Score)为AverageScore FROM lodges_temp
INNER JOIN scores_temp
ON lodges_temp.LodgeID = scores_temp.LodgeID
没有任何成功。任何指针都会非常感激。
答案 0 :(得分:1)
试试这个
SELECT COUNT(ScoreID) as scoreCount, AVG(Score) as AverageScore, Lodge
FROM scores_temp
INNER JOIN lodges_temp ON scores_temp.LodgeID = lodges_temp.LodgeID
GROUP BY Lodge
答案 1 :(得分:0)
您缺少group by
条款:
SELECT COUNT(ScoreID) as scoreCount, AVG(Score) as AverageScore, Lodge
FROM scores
INNER JOIN lodges ON scores.LodgeID = lodges.LodgeID
GROUP BY lodge