gpt4 book ai didi

sql-server - 如何根据sql中的另一列获取一列中递增的值

转载 作者:行者123 更新时间:2023-12-02 12:41:16 26 4
gpt4 key购买 nike

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

enter image description here

如何在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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com