我正在创建一个网页,其中列出了单个团队的各种最长条纹,但在尝试计算所涉及的结果类型组合的条纹时遇到了问题。
数据如下表所示......
date result gf ga type compfull -------------------------------------------------- 1980-08-16 W 3 0 league Division 1 1980-08-19 L 1 2 league Division 1 1980-08-23 W 3 1 league Division 1 1980-08-26 W 2 0 league Division 1 1980-08-30 D 2 2 league Division 1 and so on...
使用以下查询(或类似),我可以确定最长的输赢或甚至是得分的游戏。
SELECT result, type, MIN(date) as StartDate, MAX(date) as EndDate, COUNT(*) as Games
FROM (SELECT result, type, date, compfull, (SELECT COUNT(*)
FROM resultengine R
WHERE R.result <> RE.result
AND R.date <= RE.date) as RunGroup
FROM resultengine RE) A WHERE result='W' GROUP BY result, RunGroup ORDER BY Games
这是基于我找到的优秀资源here。它完全符合我的要求并告诉我我想知道什么 - 如果我改为
,同样适用result='L'
我无法解决的是如何计算不败比赛的最长连胜数,即没有L的最长连胜数。反之亦然。反之亦然。
我尝试了以下查询无效:
SELECT result, type, MIN(date) as StartDate, MAX(date) as EndDate, COUNT(*) as Games
FROM (SELECT result, type, date, compfull, (SELECT COUNT(*)
FROM resultengine R
WHERE R.result <> RE.result
AND R.date <= RE.date) as RunGroup
FROM resultengine RE) A WHERE result!='W' GROUP BY result, RunGroup ORDER BY Games
我也尝试将查询更改为:
WHERE result='W' OR result='D'
同样,这不起作用。两次尝试都反映了用于传递最长条纹Ws或Ls的查询 - 并且我的数据的手动计数告诉我这是不正确的。我无疑在这里遗漏了一些简单的东西,但是如何执行该查询以便它告诉我最长条纹的结果是Ls还是Ws?
答案 0 :(得分:3)
从代码示例中扩展,以下给出了wins / draw的运行。问题在于&#39; WHERE R.result&lt;&gt; RE.result&#39;它总是为任何不同的结果代码分配不同的分组。在这里,我将该条款(和其他一些条款)更改为分组&#39; W&#39;和&#39; D&#39;一起编写一个代码:
SELECT result, TYPE, MIN(DATE) AS StartDate, MAX(DATE) AS EndDate, COUNT(*) AS Games
FROM (SELECT result, TYPE, DATE, compfull, (SELECT COUNT(*)
FROM resultengine R
WHERE IF(R.result IN ('W','D'),1,0) <> IF(RE.result IN ('W','D'),1,0)
AND R.date <= RE.date) AS RunGroup
FROM resultengine RE) A WHERE result IN ('W','D') GROUP BY IF(result IN ('W','D'),1,0), RunGroup ORDER BY Games
答案 1 :(得分:1)
此查询将为您提供单个团队的最大不败序列(该表包含单个团队的数据:
select MAX(final.win_seq_count) from
(Select date,result,gf,ga,type,compfull,
@seq_count:=if(result="L",0,@seq_count:=@seq_count+1) as win_seq_count,
from resultengine,(select @seq_count:=0) t
order by date) final
注意:代码可能包含很小的语法错误。