根据另一个列值将增量种子分配给字段

时间:2022-12-15 09:34:24

Given: DEMO Here

鉴于:DEMO在这里

Goal: I'm trying to provide sort values for rows and columns of a pivot talbe.

目标:我正在尝试为枢轴塔的行和列提供排序值。

OneTable
Id,GroupId,TopId,TopSort,SideId,SideSort


View1 of OneTable
Id,GroupId,TopId,TopSort
------------------------
1,1,101,null
2,1,101,null
3,1,102,null
4,1,102,null

View2 of OneTable
Id,GroupId,SideId,SideSort
------------------------
1,1,201,null
3,1,201,null
2,1,202,null
4,1,202,null

How can I assign a sequence value to TopSort and SideSort per TopId and SideId

如何为TopId和SideId为TopSort和SideSort分配序列值

For example, take View1 above. Should look like the following:

例如,上面的View1。应该如下所示:

View1 of OneTable
Id,GroupId,TopId,TopSort
------------------------
1,1,101,1
2,1,101,1
3,1,102,2
4,1,102,2

1 个解决方案

#1


3  

You can use DENSE_RANK() to achieve the desired results, see my example

您可以使用DENSE_RANK()来获得所需的结果,请参阅我的示例

SELECT 
  Id,
  GroupId,
  TopId,
  DENSE_RANK() OVER (ORDER BY TopId ASC) 
FROM Tracker 
ORDER BY TopId;

#1


3  

You can use DENSE_RANK() to achieve the desired results, see my example

您可以使用DENSE_RANK()来获得所需的结果,请参阅我的示例

SELECT 
  Id,
  GroupId,
  TopId,
  DENSE_RANK() OVER (ORDER BY TopId ASC) 
FROM Tracker 
ORDER BY TopId;