English 中文(简体)
选择重复条目的编号列表
原标题:Select numbered list of duplicate entries

非常简单的例子:

假设我有这张桌子:

ID         Name
GUID1      John
GUID2      John
GUID3      John
GUID4      John
GUID5      Jane
GUID6      Jane

我想做一个选择,为相同名称的每次出现分配一个计数器(每次从1开始)。即:

ID         Name     Counter
GUID1      John     1
GUID2      John     2
GUID3      John     3
GUID4      John     4
GUID5      Jane     1
GUID6      Jane     2

这样键(Name,Counter)就形成了一个独特的组合。

Thanks
Karl

最佳回答
declare @T table(ID varchar(10), Name varchar(10))
insert into @T values
( GUID1 ,       John ),
( GUID2 ,       John ),
( GUID3 ,       John ),
( GUID4 ,       John ),
( GUID5 ,       Jane ),
( GUID6 ,       Jane )

select
  ID,
  Name,
  row_number() over(partition by Name order by ID) as Counter
from @T
order by ID
问题回答

暂无回答




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签