1
我試圖通過最大的時間標記的量的重量爲每個辦公桌Mysql的查詢 - 與由組和max時間戳
計算每個播放器中的量的加權平均值例如爲下列加權平均表:
desk | player | amount | weight | timestamp
-----------------------------------
---- 1 --- | ---- 1 --- | ---------2------|-----10-------|13:00
---- 1 --- | ---- 1 --- | ---------1------|-----15-------|14:00
---- 2 --- | ---- 1 --- | ---------5------|-----20-------|13:00
---- 2 --- | ---- 1 --- | ---------3------|-----5-------|15:00
---- 3 --- | ---- 2 --- | ---------5------|-----6-------|13:00
---- 3 --- | ---- 2 --- | ---------2------|-----30-------|14:00
---- 4 --- | ---- 2 --- | ---------1------|-----10-------|15:00
---- 4 --- | ---- 2 --- | ---------6------|-----3-------|13:00
的結果應該是:
**player | weigtedAmount**
---1---| 1.5 ((15/20)*1 + (5/20)*3)
---2---| 1.75 ((30/40)*2 + (10/40)*1)
我嘗試下面的查詢,但我認爲我缺少一些時間戳= MAX(時間戳)地方:
select player,SUM(lates_data.amount * (lates_data.weight/ lates_data.sumWeight))
from (SELECT *
FROM (select player as plr, SUM(weight) as sumWeight
from playersDb.player_stats
group by plr
) t1 INNER JOIN
(select desk, player, amount, weight
from playersDb.player_stats
group by desk, player
) t2
ON t1.plr= t2.player
) lates_data
group by player
任何想法?
拉茲