将相同的ID添加到相同的行。

huangapple go评论63阅读模式
英文:

Adding same ID to same rows

问题

在SQL postgres中,我遇到了一个问题。我正在处理这种类型的表格:

将相同的ID添加到相同的行。

正如您所看到的,我有一些具有相同的年份、月份和日期的行,这种情况经常发生。对于这些具有相同年份、月份、日期但不同count和dmg列的行,我想要为它们分配相同的编号id,就像您在下一张图片中看到的那样:

将相同的ID添加到相同的行。

希望有人能够帮助我。提前感谢!

英文:

I have a problem in SQL postgres. I'm starting with this kind of table:

将相同的ID添加到相同的行。

As you can see, I have some rows with same year, month, day, which can often occur. For each of them the columns count and dmg are different. I want to associate for each of these rows with same year,month,day but different count and dmg, the same number id, as you can see in the next img:

将相同的ID添加到相同的行。

wish someone could help me. thanks in advance!

答案1

得分: 2

你可以使用 dense_rank()

select dense_rank() over (order by year, month, day) as id,
       t.*
from t;
英文:

You can use dense_rank():

select dense_rank() over (order by year, month, day) as id,
       t.*
from t;

huangapple
  • 本文由 发表于 2020年1月3日 19:07:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定