我有下表的记录。对于每一个TranNo,我都必须在预期结果列中增加1。可能吗?

sql-server - 如何基于sql中的另一列获取列中的递增值-LMLPHP

如何在sqlserver中获得此结果?

我无法获得适当的结果

 <Expected Result>
1
1
2
3
4
5
6
7
8


我试过了:

SELECT BatchNo,
       TranNo,
       WorkSource
FROM table1 WITH (NOLOCK)
WHERE BatchNo IN ('0000000420', '0000000421', '0000000422')
  AND PROCESSDATE = '20190206'
GROUP BY WorkSource,
         BATCHNO,
         TranNo;

最佳答案

你可以试试这个

create table #temp (WorkSource char(3), BatchNo char(3), TranNo int)
insert into #temp values
('012', '001', 1),
('012', '001', 1),
('012', '001', 2),
('012', '001', 3),
('012', '002', 1),
('012', '002', 2),
('012', '003', 3),
('013', '005', 1),
('013', '005', 2)

SELECT *, Dense_Rank() OVER(ORDER BY BatchNo, TranNo) AS Row_Number
FROM #temp

关于sql-server - 如何基于sql中的另一列获取列中的递增值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54571598/

10-13 00:52