从小到大排序
rank smallest to largest
我有以下示例数据:
declare @table table
(
Tbl char(2)
, TxDate date
, AutoIdx int
, Debit float
, Credit float
, Order_No varchar(50)
, ExtOrderNum varchar(50)
, Reference varchar(50)
, Reference2 varchar(50)
, Description varchar(100)
, AuditNumber varchar(50)
)
insert into @table
(
Tbl
, TxDate
, AutoIdx
, Debit
, Credit
, Order_No
, ExtOrderNum
, Reference
, Reference2
, Description
, AuditNumber
)
values
('GL','2020-03-18',877224,0,2306.9,'PO07673','blue/Prov','60','GRV8399','Purchase Order','26382.0001')
, ('AP','2020-03-18',265586,0,2306.9,'PO07673','blue/Prov','60','GRV8399','Purchase Order','26382.0001')
, ('AP','2020-03-18',265607,0,2306.9,'PO07673','blue/Prov','60','GRV8455','Purchase Order','26391.0001')
, ('GL','2020-03-18',877518,0,2306.9,'PO07673','blue/Prov','60','GRV8455','Purchase Order','26391.0001')
, ('GL','2020-03-18',877530,2.57,0,'PO07673','60',60,'GRV8481','Accounts Payable','26391.0002')
, ('GL','2020-03-18',877525,0,23008.37,'PO07673','60',60,'GRV8481','Purchase Order','26391.0002')
, ('AP','2020-03-18',265608,0,23008.37,'PO07673','60',60,'GRV8481','Purchase Order','26391.0002')
当我运行这个查询时:
select
rank() over (order by TxDate,AuditNumber) rnk
, *
from @table
我得到以下结果:
我正在尝试根据每个不同的数字生成一个新数字AuditNumber
,但是它不是从 1 到 3,而是 1,3,5。
我试过 partition by
和 row_number
等,但我只得到数字 1。
我错过了什么?
我的预期结果:
使用 DENSE_RANK
,对于每组相同值的记录,排名计数器总是增加 1:
SELECT
*, DENSE_RANK() OVER (ORDER BY TxDate, AuditNumber) rnk
FROM @table;
我有以下示例数据:
declare @table table
(
Tbl char(2)
, TxDate date
, AutoIdx int
, Debit float
, Credit float
, Order_No varchar(50)
, ExtOrderNum varchar(50)
, Reference varchar(50)
, Reference2 varchar(50)
, Description varchar(100)
, AuditNumber varchar(50)
)
insert into @table
(
Tbl
, TxDate
, AutoIdx
, Debit
, Credit
, Order_No
, ExtOrderNum
, Reference
, Reference2
, Description
, AuditNumber
)
values
('GL','2020-03-18',877224,0,2306.9,'PO07673','blue/Prov','60','GRV8399','Purchase Order','26382.0001')
, ('AP','2020-03-18',265586,0,2306.9,'PO07673','blue/Prov','60','GRV8399','Purchase Order','26382.0001')
, ('AP','2020-03-18',265607,0,2306.9,'PO07673','blue/Prov','60','GRV8455','Purchase Order','26391.0001')
, ('GL','2020-03-18',877518,0,2306.9,'PO07673','blue/Prov','60','GRV8455','Purchase Order','26391.0001')
, ('GL','2020-03-18',877530,2.57,0,'PO07673','60',60,'GRV8481','Accounts Payable','26391.0002')
, ('GL','2020-03-18',877525,0,23008.37,'PO07673','60',60,'GRV8481','Purchase Order','26391.0002')
, ('AP','2020-03-18',265608,0,23008.37,'PO07673','60',60,'GRV8481','Purchase Order','26391.0002')
当我运行这个查询时:
select
rank() over (order by TxDate,AuditNumber) rnk
, *
from @table
我得到以下结果:
我正在尝试根据每个不同的数字生成一个新数字AuditNumber
,但是它不是从 1 到 3,而是 1,3,5。
我试过 partition by
和 row_number
等,但我只得到数字 1。
我错过了什么?
我的预期结果:
使用 DENSE_RANK
,对于每组相同值的记录,排名计数器总是增加 1:
SELECT
*, DENSE_RANK() OVER (ORDER BY TxDate, AuditNumber) rnk
FROM @table;