【问题标题】:trying to create a "Row Number" column that restarts from 1 again at the start of a new year and when there is new stating pitcher试图创建一个“行号”列,在新的一年开始时再次从 1 重新开始,并且有新的投手时
【发布时间】:2016-05-15 15:20:45
【问题描述】:

我正在努力确定 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 列:

我仍在尝试理解所有代码...“@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;

【问题讨论】:

  • 使用插入触发器?在插入时选择给定起始投手和给定年份的所有行。如果结果为空,则插入1,否则,插入最大行数+1。
  • 糟糕,误会了。无论如何,仍然尝试插入触发器。但是,只需选择最后一行。如果年份和投手不同,则插入 1。如果它们相同,则插入所述行号 + 1。

标签: mysql reset calculated-columns row-number


【解决方案1】:

update 中进行枚举有点痛苦。但这是可能的。

在您的情况下,这可能是使用带有子查询的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 条件只是将一行匹配到子查询中的同一行。

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

【讨论】:

  • 嗨,戈登,感谢您对此的帮助。我按原样尝试了代码,但我收到了一个添加到 OP 的错误。
  • 我认为这只是括号过多。
  • 我确实删除了一个括号并更改了一些字段的名称以匹配我的表,现在它运行直到我收到另一个错误。请查看我使用的错误和代码,我添加了 OP。
  • @LeeZee 。 . .哎呀。应该有第二个if(),我刚刚添加了。
  • 戈登,谢谢!效果很好。我在 OP 中添加了上面没有错误的代码,表和列的名称已更改(并添加)以适合我的表。我还删除了别名“sp”,而是每次都包含表的全名,因为由于某种原因,我不断收到错误消息,上面写着“未知表 sp”。
猜你喜欢
  • 2021-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多