计算MySQL中多年的运行或条纹

时间:2012-05-31 19:33:39

标签: mysql

我正在尝试计算MySQL数据库中的连胜条件。我创建了下表来存储赢/输数据:

"year"  "team_id"   "week"  "result"
"2007"  "1"         "1"     "W"
"2007"  "1"         "2"     "L"
"2007"  "1"         "3"     "W"
"2007"  "1"         "4"     "W"
"2007"  "1"         "5"     "W"
"2007"  "1"         "6"     "W"
"2007"  "1"         "7"     "W"
"2007"  "1"         "8"     "W"
"2007"  "1"         "9"     "W"
"2007"  "1"         "10"    "L"
. . .

这些数据跨越12个团队,为期4年,每年13-16周。

要计算中奖条件,我使用以下查询:

SELECT
    team_id,
    result,
    year,
    MIN(week) as StartDate, 
    MAX(week) as EndDate, 
    COUNT(*) as Games
FROM (
    SELECT
        year,
        week,
        team_id,
        result,
        (   SELECT
                COUNT(*) 
            FROM
                win_loss_temp wl2 
            WHERE
                wl1.team_id = wl2.team_id
                and wl2.result <> wl1.result
                and wl2.year <= wl1.year
                AND wl2.week <= wl1.week) as rungroup
    FROM
        win_loss_temp wl1) A
WHERE result = 'W'
GROUP BY year, team_id, result, rungroup
ORDER BY Games desc
LIMIT 15;

这给出了以下结果:

team_id    result    year    StartDate    EndDate    Games
----------------------------------------------------------
5          W         2007    1            12         12
1          W         2007    3            9          7
5          W         2008    2            7          6
. . .

这是正确的/预期的输出......在一个给定的年份内。

我的问题是跨越多年。假设一支球队在3连胜中完成了2007年的比赛,然后赢得了2008年的前4场比赛。这应该被记录为总共7连胜(7胜没有记录亏损)。但到目前为止,我无法弄清楚如何修改上述查询以适应跨越年限。

我甚至尝试在表格中创建一个与周连接的新字段(例如2007年第13周的2007.13),但是没有成功使用该字段而不是分别使用年份和周。给出了时髦的结果。

谢谢。

2 个答案:

答案 0 :(得分:0)

SELECT
    team_id,
    result,
    year,
    MIN(CONCAT(year,week)) as StartDate, 
    MAX(CONCAT(year,week)) as EndDate, 
    COUNT(*) as Games

答案 1 :(得分:0)

重新审视这个以修改我以前不完整的(直到现在我都不知道)的答案。感谢完全解决我的问题的this external resource,我能够调整查询以完全满足我的特定需求。

首先,我创建了一个定义为run_groups的视图:

select
    `GR`.`team_id` AS `team_id`,
    ((`GR`.`year` * 100) + `GR`.`week`) AS `GameDate`,
    `GR`.`result` AS `Result`, (
        select
            count(0)
        from
            `jaddl`.`game_results` `G`
        where
            ((`G`.`result` <> `GR`.`result`) and
            (`G`.`team_id` = `GR`.`team_id`) and
            (`G`.`playoffs` = `GR`.`playoffs`) and
            (((`G`.`year` * 100) + `G`.`week`) <= ((`GR`.`year` * 100) + `GR`.`week`)))) AS `RunGroup`
from
    `jaddl`.`game_results` `GR`
where
    (`GR`.`playoffs` = 0)

然后我可以利用该视图创建另一个视图,这可以为我提供最终结果,我可以根据数据挖掘目的查询...我的最终游戏。

select
    `run_groups`.`team_id` AS `team_id`,
    `run_groups`.`Result` AS `Result`,
    min(`run_groups`.`GameDate`) AS `StartDate`,
    max(`run_groups`.`GameDate`) AS `EndDate`,
    count(0) AS `Games`
from
    `jaddl`.`run_groups`
group by `run_groups`.`team_id`, `run_groups`.`Result`, `run_groups`.`RunGroup`
order by count(0) desc, `run_groups`.`Result` desc, min(`run_groups`.`GameDate`)

这样输出数据:

team_id  Result  StartDate  EndDate  Games  
      1      -1     201507   201606     13  
      7      -1     201603   201702     13  
      5       1     200701   200712     12  
      1      -1     201202   201213     12  
      2       1     200908   201005     11  
     12       1     201209   201305     10  
      5       1     201401   201410     10  
      4      -1     200813   200908      9  
     11      -1     201112   201207      9  

result 1是胜利,-1是一种损失。)瞧!我需要查询所有与条纹相关的统计信息的数据集。向@eggyal致敬,他建议使用year*100+week,而不是将前导零作为字符串连接数年和数周。