英文:
Adding same ID to same rows
问题
在SQL postgres中,我遇到了一个问题。我正在处理这种类型的表格:
正如您所看到的,我有一些具有相同的年份、月份和日期的行,这种情况经常发生。对于这些具有相同年份、月份、日期但不同count和dmg列的行,我想要为它们分配相同的编号id,就像您在下一张图片中看到的那样:
希望有人能够帮助我。提前感谢!
英文:
I have a problem in SQL postgres. I'm starting with this kind of table:
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:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论