简体   繁体   English

行的顺序编号

[英]sequential numbering of rows

I have to customize a SQL view to be able to perform a system integration test. 我必须自定义SQL视图才能执行系统集成测试。 One table keeps track of the transactions and the LINE ITEMS per transactions (for example, transaction 2 has three items, so there are three consecutive rows corresponding to the same transaction number) What I need to accomplish is to get a column where I keep track of the number of items for THAT transaction, always starting from 1 for each transaction. 一个表跟踪事务和每个事务的LINE ITEMS(例如,事务2有三个项目,因此有三个连续的行对应于同一个事务编号)我需要完成的是获取一个我跟踪的列对于每笔交易,该交易的项目数量始终为1。

For example, the first column is TransactionNumber and the second is DesiredOutput : 例如,第一列是TransactionNumber ,第二列是DesiredOutput

1                    1
1                    2
2                    1
2                    2
2                    3
3                    1
4                    1
4                    2

ETC... 等等...

I know how to number consecutive rows on the WHOLE table, but I cannot find any reference to this numbering that depends on a value on another row. 我知道如何在WHOLE表上对连续的行进行编号,但我找不到对这个编号的任何引用,这取决于另一行的值。

For SQL Server 2005+ and Oracle you can use the following: 对于SQL Server 2005+和Oracle,您可以使用以下内容:

SELECT TransNumber, ROW_NUMBER() OVER(PARTITION BY Transnumber ORDER BY Something) [DESIRED COLUMN OUTPUT]
FROM YourTable

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM