I have a working SQL statement which shows the following:
- Player
- Champion
- Role
- Total Kill/Death Ratio
- Total Kill+Assist/Death Ratio
- Total Win %
- Difference between Ratio from one month ago
- Difference between KA Ratio from one month ago
- Difference between win % from one month ago
However, I don't think it's quite as optimized as it could be:
with CTERatio as (
select tt.playerid, tt.champid, tt.roleid,
sum(tt.words) as kills,
sum(tt.words2) as deaths,
sum(tt.words3) as assists,
sum(tt.words)/sum(tt.words2) as ratio,
(sum(tt.words)+sum(tt.words3))/sum(tt.words2) as kdar,
avg(tt.complete) as win
from schema.records tt
group by tt.playerid, tt.champid, tt.roleid
having count(*) > 2
order by ratio desc
),
hist as (
select tt.playerid, tt.champid, tt.roleid,
sum(tt.words) as kills,
sum(tt.words2) as deaths,
sum(tt.words3) as assists,
sum(tt.words)/sum(tt.words2) as ratio,
(sum(tt.words)+sum(tt.words3))/sum(tt.words2) as kdar,
avg(tt.complete) as win
from schema.records tt
where tt.sys_time < date_trunc('day', NOW() - interval '1 month')
group by tt.playerid, tt.champid, tt.roleid
having count(*) > 2
order by ratio desc
)
select c.playerid as player, c.champid as champion, c.roleid as rolee,
round(c.ratio, 2) as ratio, round(c.kdar, 2) as kdar, round(c.win, 2) as win,
round(c.ratio - h.ratio, 2) as ratiochange,
round(c.kdar - h.kdar, 2) as kdarchange,
round(c.win - h.win, 2) as winchange
from CTERatio c, hist h
where c.playerid = h.playerid
and c.champid = h.champid
and c.roleid = h.roleid
order by c.ratio desc
Is it possible I can incorporate using CASE instead of WITH? Such as:
avg(c.kills) - avg
(
case when tt.sys_time < date_trunc('day', NOW() - interval '1 month')
then h.words
end
)