mysql - 尝试创建一个 "Row Number"列,该列在新年伊始以及有新的声明投手时再次从 1 重新开始

标签 mysql reset calculated-columns row-number

我正在努力确定 MySQL 代码以在表“starting_pitcher_stats”中创建 Row_Number 列,我想从 1 开始,但然后在新的一年开始时以及当有新的数据时从 1 重新启动投手。我使用以下代码创建 Row_Number 列:

理想情况下,表格应如下所示:

    Starting_Pitcher park_factor  std_PF  Row_Number  Game_Date  Game_Number
    aased001          108         108     1           1977-07-26     0
    aased001          94          101     2           1977-07-31     0
    aased001          100         100.66  3           1977-08-06     0
    aased001          108         102.5   4           1977-08-11     0
    aased001          108         103.66  5           1977-08-16     0
    aased001          96          102.33  6           1977-08-21     0
    aased001          108         103.14  7           1977-08-26     0
    aased001          108         103.75  8           1977-08-31     0
    aased001          104         103.77  9           1977-09-05     1
    aased001          108         104.2   10          1977-09-10     0
    aased001          92          103.09  11          1977-09-16     0
    aased001          106         103.33  12          1977-09-22     0
    aased001          108         103.69  13          1977-09-27     1
    aased001          96          96      1           1978-04-11     0
    aased001          100         13.06   2           1978-04-16     0
    aased001          100         18.5    3           1978-04-21     0
    aased001          96          23.05   4           1978-04-28     0

...现在,表格示例如下所示:

    Starting_Pitcher park_factor  std_PF  Row_Number  Game_Date  Game_Number
    aased001          108         108     1           1977-07-26     0
    aased001          94          101     2           1977-07-31     0
    aased001          100         100.66  3           1977-08-06     0
    aased001          108         102.5   4           1977-08-11     0
    aased001          108         103.66  5           1977-08-16     0
    aased001          96          102.33  6           1977-08-21     0
    aased001          108         103.14  7           1977-08-26     0
    aased001          108         103.75  8           1977-08-31     0
    aased001          104         103.77  9           1977-09-05     1
    aased001          108         104.2   10          1977-09-10     0
    aased001          92          103.09  11          1977-09-16     0
    aased001          106         103.33  12          1977-09-22     0
    aased001          108         103.69  13          1977-09-27     1
    aased001          96          96      14          1978-04-11     0
    aased001          100         13.06   15          1978-04-16     0
    aased001          100         18.5    16          1978-04-21     0
    aased001          96          23.05   17          1978-04-28     0

我曾经使用以下代码来创建它:

ALTER TABLE starting_pitcher_stats ADD Row_Number int(11) DEFAULT '0' NOT NULL;
    SELECT @n:=0, Row_Number, Starting_Pitcher, lg_ID, YEAR_ID, Game_Date, Game_Number FROM starting_pitcher_stats;
    UPDATE starting_pitcher_stats SET Row_Number = @n := @n + 1

当我使用以下代码使 Row_Number 列在新年开始时从 1 重新开始并且当有新投手时,它不起作用:

ALTER TABLE starting_pitcher_stats ADD ROW_NUMBER1 int(11) DEFAULT '0' NOT NULL;
SELECT @n:=0, Row_Number, Starting_Pitcher, lg_ID, YEAR_ID, Game_Date, Game_Number FROM starting_pitcher_stats;
UPDATE starting_pitcher_stats IF std_PF=park_factor THEN SET Row_Number=1 ELSE SET Row_Number1 = @n := @n + 1

我收到以下错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF std_PF=park_factor THEN SET Row_Number=1 ELSE SET Row_Number' at line 1

是否可以设置此 Row_Number 列,使其从 1 开始,即使我通过另一列(如 Game_Date 列)对其重新排序(或分组)?

有人可以帮忙吗?

提前谢谢您。 李

更新:戈登,这是我收到的错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 1, 1)
                         )
               ) as rn
        from starting_p' at line 4

我确信所需的编辑是显而易见的,但我的经验不足以发现它。

是的,有一个唯一的 ID“GAME_ID”,其中封装了“YEAR_ID”、“Game_Date”、“Game_Number”和该比赛的主队“Park_ID”。感谢您提醒我关于 double_headers...“Game_Number”是我从可用的“GAME_ID”字段派生的一个字段,指的是双头游戏的游戏“1”或游戏“2”(如果有的话)或游戏“0”(如果是单场游戏)。听起来使用 GAME_ID 会比通过 Year_ID、Game_Date 和 Game Number 分别加入更高效?

以下是表格示例的屏幕截图,这次包括 GAME_ID 和 YEAR_ID 列:

enter image description here

我仍在尝试理解所有代码...“@sy”中的“sy”是否必须在某处定义?

我 预先感谢您的帮助。 李

更新: 这是我在尝试通过删除括号并更改某些字段的名称以匹配我的表来编辑的代码时收到的错误:

Incorrect integer value: 'aased001:1977:1:1' for column 'row_number' at row 1

这是我使用的产生上述错误的最新代码。我希望我没有无意中使代码超出了它应该完成的目标:

UPDATE starting_pitcher_stats JOIN
           (select starting_pitcher_stats.*, 
                   (@rn := if(@sy = concat_ws(':', Starting_Pitcher, YEAR_ID), @rn + 1,
                              @sy := concat_ws(':', Starting_Pitcher, YEAR_ID, 1, 1)
                             )
                   ) as rn
            from starting_pitcher_stats
            cross join
                 (select @rn := 0, @sy := '') AS params
            order by Starting_Pitcher, YEAR_ID, Game_Date, Game_Number
           ) as b
           on b.Starting_Pitcher = starting_pitcher_stats.Starting_Pitcher AND
              b.YEAR_ID = starting_pitcher_stats.YEAR_ID AND
              b.Game_Date = starting_pitcher_stats.Game_Date AND
              b.Game_Number=starting_pitcher_stats.Game_Number
        set starting_pitcher_stats.row_number= b.rn

更新:这是没有错误的代码:

UPDATE starting_pitcher_stats JOIN
       (select starting_pitcher_stats.*,
               (@rn := if(@sy = concat_ws(':', Starting_Pitcher, YEAR_ID), @rn + 1,
                          if(@sy := concat_ws(':', Starting_Pitcher, YEAR_ID), 1, 1)
                         )
               ) as rn
        from starting_pitcher_stats CROSS JOIN
             (select @rn := 0, @sy := '') params
        order by Starting_Pitcher, YEAR_ID, Game_Date, Game_Number
       ) sp2
       on sp2.Starting_Pitcher = starting_pitcher_stats.Starting_Pitcher AND
          sp2.YEAR_ID = starting_pitcher_stats.YEAR_ID AND
          sp2.Game_Date = starting_pitcher_stats.Game_Date AND
          sp2.Game_Number=starting_pitcher_stats.Game_Number
    set starting_pitcher_stats.row_number = sp2.rn;

最佳答案

更新中进行枚举有点麻烦。但这是可能的。

在您的情况下,这可能是使用带有子查询的 JOIN 最简单的方法:

update starting_pitcher sp JOIN
       (select sp.*,
               (@rn := if(@sy = concat_ws(':', starting_pitcher, year), @rn + 1,
                          if(@sy := concat_ws(':', starting_pitcher, year), 1, 1)
                         )
               ) as rn
        from starting_pitcher_stats cross join
             (select @rn := 0, @sy := '') params
        order by starting_pitcher, year, game_date
       ) sp2
       on sp2.starting_pitcher = sp.starting_pitcher and
          sp2.year = sp.year and
          sp2.game_date = sp.game_date
    set sp.row_number = sp2.rn;

注意:这使用三列(starting_pitcher、year、game_date)作为join。如果表中有唯一的 ID,那就更好了。特别是,您的示例没有双标题。因此,您可能想要添加其他字段。 on 条件只是将一行与子查询中的同一行进行匹配。

您可以通过单独运行子查询来查看发生了什么。

关于mysql - 尝试创建一个 "Row Number"列,该列在新年伊始以及有新的声明投手时再次从 1 重新开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37240034/

相关文章:

php - 在php中获取mysql导出下载url

sql - 合并行号/主键相同的列

带有限制的mysql左连接子查询在父选择上给出具有空值的字段

php - Zend 按字段选择分组

php - MySQL 从多个表中选择

sql-server - T-sql 在字段更改时重置行号

submit - 提交后清除 Redux-Form 字段

android - 在 Android 中停止 ObjectAnimators 的 AnimatorSet

calculated-columns - Google Data Studio 中的总和/详细程度

MySQL:求和子查询列