I have a table like this:
| No. | ReferenceNumber | TransactionTime | Amount | Source |
| ------------ | ------------ | ------------ | ------------ | ------------ |
| 1 | 3099 | N/A | -1125 | A |
| 2 | 3099 | N/A | -1125 | B |
| 3 | 3100 | N/A | -375 | B |
| 4 | 3101 | 05:00 | -375 | A |
| 5 | 3101 | 05:00 | 2000 | B |
| 6 | 3101 | 06:00 | 2000 | A |
| 7 | 3102 | 06:00 | 2500 | B |
How to determine a number (in the No.
column) sequentially for the above data using SQL Server? I appreciate for the help.
I've tried this T-SQL code:
SELECT
ROW_NUMBER() OVER (ORDER BY ReferenceNumber asc) No.,
ReferenceNumber, TransactionTime, Amount, Source
FROM
aTable
I got the result like this:
No. | ReferenceNumber | TransactionTime | Amount | Source |
---|---|---|---|---|
1 | 3099 | N/A | -1125 | A |
2 | 3099 | N/A | -375 | B |
3 | 3100 | N/A | -1125 | B |
4 | 3101 | 05:00 | -375 | A |
5 | 3101 | 05:00 | 2000 | B |
6 | 3101 | 06:00 | 2500 | B |
7 | 3102 | 06:00 | 2000 | A |
But, it's not the expectation like the above table.
2条答案
按热度按时间yhxst69z1#
Consider this approach
rekjcdws2#
Consider this:
Here we've just added some addition columns to the
ROW_NUMBER
ORDER
.