我有3张桌子
Table Ploeg with
PloegID | Name |
5 | name1|
2 | name2|
表格与
ID | PloegID | Timestamp
1 | 5 | xxxxxx
2 | 2 | xxxxxx
3 | 5 | xxxxxx
4 | 5 | xxxxxx
5 | 2 | xxxxxx
表格平均值
ID | PloegID | Speed
1 | 5 | 4
2 | 2 | 3
3 | 5 | 6
4 | 5 | 2
5 | 2 | 3
我需要的是一张带
的表格PloegID | Average of Speed for each ploegID | Name | Count of Ploegid in table Round.
5 | 6 | name1 | 3
2 | 3 | name2 | 2
我有这个代码,但它的轮次错误
SELECT g.ploegid, AVG(g.speed) as average,l.name ,count(r.ploegid) AS rondes
FROM averages as g
left join Ploeg as l on l.id = g.ploegid
left join Round as r on r.ploegid = l.ploegid
GROUP BY ploegid
ORDER BY g.ploegid;
速度是正确的,但计数没有。
答案 0 :(得分:0)
每个表中可能有多行与ploegid
匹配。因此,你得到的笛卡尔产品正在抛弃计算。我还建议您使用left join
启动Ploeg
s,因为这似乎是驱动表:
SELECT l.ploegid, g.average, l.name, r.rondes
FROM Ploeg l left join
(select ploegid, avg(g.speed) as average
from averages g
group by ploegid
) g
on l.id = g.ploegid left join
(select ploegid, count(*) as rondes
from Round r
group by ploegid
) r
on r.ploegid = l.ploegid
ORDER BY l.ploegid;