假设我有一个表
<表类>
订单id
项目
tbody><<tr>1 1B 53 B 表类>
您可以使用ROW_NUMBER()排序函数并编写如下查询:
create table orders
(
order_id int,
item nvarchar(max)
)
insert into orders values(1,'A');
insert into orders values(1,'B');
insert into orders values(5,'A');
insert into orders values(3,'B');
SELECT order_id, item, ROW_NUMBER() OVER ( PARTITION BY item ORDER BY item ASC, order_id ASC) as [order]
FROM ORDERS
order by item asc, order_id asc
查看排名功能:https://www.sqlshack.com/overview-of-sql-rank-functions/