【问题标题】:Increase number by 1 if the row data is same如果行数据相同,则将数字加 1
【发布时间】:2019-01-18 15:04:21
【问题描述】:

我有一张有很多行的表。 表格的格式是这样的

     col1|col2
     ---------
     001 |01|
     001 |01|
     001 |01|
     001 |01|
     001 |02|
     001 |02|
     001 |02|
     001 |02|
     001 |03|
     001 |03|
     001 |03|
     002 |01|
     002 |01|
     002 |01|
     002 |01|

我想在表中添加一个新列,其中 col1 和 col2 的值被检查并在 col3 中插入​​新值

col1|col2|col3
---------------
 001 |01 |1
 001 |01 |2
 001 |01 |3
 001 |01 |4
 001 |02 |1
 001 |02 |2
 001 |02 |3
 001 |02 |4
 001 |03 |1
 001 |03 |2
 001 |03 |3
 002 |01 |1
 002 |01 |2
 002 |01 |3
 002 |01 |4

【问题讨论】:

  • 你的 dbms 是什么?

标签: sql


【解决方案1】:

使用支持大多数 dbms 的 row_number()

select *, row_number() over(partition by col1,col2 order by col2) as col3
 from table_name

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 2019-01-28
    相关资源
    最近更新 更多