SQL增量计数重复值? [英] SQL Increment count on repeat values?

查看:48
本文介绍了SQL增量计数重复值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道如何问这个问题,我不是在寻找列中值的总数,而是想递增地计算重复值.例如:

Not sure how to ask this question, I'm not looking for the total count of a value in a column, rather I want to incrementally count repeat values. For example:

如果我的表是这样的:

1, ken
2, ken
3, adam
4, ken
5, adam
6, dan

我想在选择时添加一列标签重复的增量编号如下:

I want to add a column during my select that tags duplicates with an incremental number like this:

1, ken, 1
2, ken, 2
3, adam, 1
4, ken, 3
5, adam, 2
6, dan, 1

推荐答案

您可以通过 ROW_NUMBER() 在第二列上带有 PARTITION,按第一列排序:

You can do this via ROW_NUMBER() with a PARTITION on your second column, ordering by the first:

Select  Col1, 
        Col2, 
        Row_Number() Over (Partition By Col2 Order By Col1 Asc) As Col3
From    YourTable

这篇关于SQL增量计数重复值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆