我需要按天计算排名中每个玩家的属性值。
我有下表:
player_id | date | level
----------------------------
pl1 |2018-01-01| 3
pl1 |2018-01-02| 3
pl1 |2018-01-03| 4
pl1 |2018-01-05| 4
pl1 |2018-01-06| 4
pl1 |2018-01-08| 5
pl2 |2018-01-05| 1
我需要得到下一个结果:
player_id | level_by_date
-----------------------------
pl1 | (3,3,4,4,4,4,4,5)
pl2 | (0,0,0,0,1,1,1,1)
我尝试过下一步,但失败了
SELECT
player_id,
groupArray(max(module_level))
FROM d_Modules
WHERE
date>='2018-01-01' AND date<=arrayMap(i -> (toDate('2018-12-31') + toIntervalDay(i)), range(toUInt64((toDate('2018-12-31') - toDate('2018-01-01')) + 1)))
我该怎么办?
答案 0 :(得分:2)
查询看起来像这样:
SELECT
player_id,
arrayMap(date -> (date, 0), arrayFilter(date -> (arrayExists(i -> (i.1 = date), origin_level_by_date) = 0), dateRange)) AS missed_level_by_date,
arraySort(x -> x.1, arrayConcat(origin_level_by_date, missed_level_by_date)) AS level_by_date,
arrayMap(x -> x.2, level_by_date) AS result
FROM
(
SELECT
player_id,
groupArray((toDate(date), level)) AS origin_level_by_date
FROM d_Modules
WHERE (date >= '2018-01-01 00:00:00') AND (date < '2019-01-01 00:00:00')
GROUP BY player_id
)
CROSS JOIN
(
SELECT
min(toDate(date)) AS min,
max(toDate(date)) AS max,
arrayMap(i -> (min + i), range(toUInt32((max - min) + 1))) AS dateRange
FROM d_Modules
WHERE (date >= '2018-01-01 00:00:00') AND (date < '2019-01-01 00:00:00')
)
ORDER BY player_id ASC
FORMAT Vertical
结果与要求的结果略有不同,因为对于错过的日子,level-value为零:
┌─player_id─┬─result────────────┐
│ pl1 │ [3,3,4,0,4,4,0,5] │
│ pl2 │ [0,0,0,0,1,0,0,0] │
└───────────┴───────────────────┘