【问题标题】:PostgresSQL: How to count up values in a row, then reset count when row value changesPostgresQL:如何对一行中的值进行计数,然后在行值更改时重置计数
【发布时间】:2020-09-22 17:55:20
【问题描述】:

所以我有下面的表格,我想改变表格以显示第二个表格,我在其中计算 ID 的数量,然后在我得到不同的 ID 值时重置计数。

当前表

想要的表

【问题讨论】:

    标签: sql postgresql select window-functions


    【解决方案1】:

    只需使用row_number():

    select t.*, row_number() over(partition by id order by case_number) cnt
    from mytable t
    

    【讨论】:

      猜你喜欢
      • 2020-01-06
      • 1970-01-01
      • 1970-01-01
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      • 2012-08-23
      • 1970-01-01
      • 2021-10-04
      相关资源
      最近更新 更多