所以我会先解释整个问题,然后再为我解释SQL编码的问题在哪里。
我有一组数据是通过仓库的选择列表,我实际上是在尝试重新组织这些数据以遵循每个过道中的一个方向。
例如,这是一个选择列表
tripid stopeq AISLE bin
216 1 光伏 71
216 下午 2 点 64
216 3 PL 47
216 下午 4 点 36
216 5 PL 32
216 6 PL 88
216 7 帕杰 49
216 8 帕杰 29
216 9 PJ 20
216 10 PJ 19
216 11 PI 22
216 12 PI 45
216 13 PN 33
216 14 PN 28
由于过道只能走一条路,有些是上升的,有些是下降的
在这种情况下,我想重新组织这个表,使通道 PJ 上升(选择 7、8、9、10),所以我希望它通过选择列表并根据通道上升或下降重新排序选择序列。对于这个例子,我将只关注一个过道,所以我正在寻找一个循环遍历表的查询,并使用列 PJ 重新组织记录,以便以相反的顺序进行。如同
216 7 PJ 19
216 8 便士 20
216 9 帕杰 29
216 10 PJ 49
但我现在只是想影响那些行。到目前为止,我已经创建了一个游标,其中包含一个 CTE。像这样的东西。
declare inner_cursor cursor scroll
for select aisle from table_input
open inner_cursor
fetch next from inner_cursor into @aisle
while @@fetch_status = 0
begin
if @aisle in ('PJ')
begin
with C as
(select stopseq, 0 + row_number() over (order by bin desc) as newtripstop
from SIM_Input_reschedule
)
update C
set tripstopseq = newtripstop
end
但这只会按 Bin 编号对整个列表进行排序,所以我尝试通过添加来为过道添加约束
begin
with C as
(select tripstopseq, 0 + row_number() over (order by bin asc) as newtripstop, aisle
from SIM_Input_reschedule
where AISLE = @aisle
)
update C
set tripstopseq = newtripstop
end
但这不会影响表中的任何行。任何帮助表示赞赏。我可能对 CTE 采取了完全错误的方法,所以如果你想出更好的方法就说吧。