简体   繁体   English

计算列中值的出现并添加到SQL 2008 R2中的新列

[英]Count occurence of a value in a column and add to new column in SQL 2008 R2

I have a table like so 我有这样的桌子

DBName         RunID          SegNo
FM1            42              1
FM2            47              2
FM2            47              3

I would like the number of time the RunID is occured in a new column. 我想要在新列中出现RunID的时间。 The result will be as below 结果如下

DBName         RunID          SegNo         Position
FM1            42              1              1  
FM2            47              2              1
FM2            47              3              2

Thanks for your help. 谢谢你的帮助。

You can use ROW_NUMBER : 您可以使用ROW_NUMBER

SELECT DBName, RunID, SegNo,
       Position = ROW_NUMBER() OVER (PARTITION BY RunID ORDER BY DBName)
FROM dbo.Table1

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在SQL Server 2008 r2中添加列值? - How to add column value in SQL Server 2008 r2? 如何在SQL Server 2008 R2中添加具有其他列值作为默认值的列? - How to add a column which has another column value as default value in SQL Server 2008 R2? SQL Server 2008 R2,为另一列的每个不同值选择一个列的一个值 - SQL server 2008 R2, select one value of a column for each distinct value of another column 不同列上的不同值的最大列值SQL Server 2008 R2 - MAX Column Value for Distinct Value on Different column SQL Server 2008 R2 SQL - 计算一列值在另一列中出现的次数 - SQL - Count the occurence of one column value in another SQL SERVER 2008 R2在另一列中查找列词 - SQL SERVER 2008 R2 find column words in another column SQL Server 2008 R2:查找column1中存在column2值的行 - SQL Server 2008 R2: Find rows where column2 value present in column1 在WHERE子句中使用CASE? SQL Server 2008 R2根据值按不同的列筛选查询 - CASE in WHERE clause? Filter query by different column depending a value SQL Server 2008 R2 根据另一列的最大值选择一个不同的行SQL Server 2008 R2 - Select a distinct row based on the max value of another column SQL Server 2008 R2 通过另一个表中的列更新SQL Server 2008 R2中的表,并且还需要求和值 - Update a table in SQL Server 2008 R2 by a column in another table and also need a sum value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM