按顺序更新查询

问题描述 投票:0回答:1

我在尝试使用更新查询时遇到问题:

select      
    lc.name as locations,
    lc.sequence
from        
    dbo.LocationContainers lc

当前此查询显示此结果:

locations       sequence
-------------------------
014-001-010-01  10
014-001-010-02  10
014-001-010-03  10
014-001-010-04  10
014-001-010-05  10
014-001-010-06  10
014-001-010-07  10
014-001-010-08  10
014-001-010-09  10
014-001-010-10  10
014-001-020-01  10
014-001-020-02  10
014-001-020-03  10
014-001-020-04  10
014-001-020-05  10
014-001-020-06  10
014-001-020-07  10
014-001-020-08  10
014-001-020-09  10
014-001-020-10  10
014-001-030-01  10
014-001-030-02  10
014-001-030-03  10
014-001-030-04  10
014-001-030-05  10
014-001-030-06  10
014-001-030-07  10
014-001-030-08  10
014-001-030-09  10
014-001-030-10  10
014-030-010-01  10
014-030-010-02  10
014-030-010-03  10
014-030-010-04  10
014-030-010-05  10
014-030-010-06  10
014-030-010-07  10
014-030-010-08  10
014-030-010-09  10
014-030-010-10  10
014-030-020-01  10
014-030-020-02  10
014-030-020-03  10
014-030-020-04  10
014-030-020-05  10
014-030-020-06  10
014-030-020-07  10
014-030-020-08  10
014-030-020-09  10
014-030-020-10  10
014-030-030-01  10
014-030-030-02  10
014-030-030-03  10
014-030-030-04  10
014-030-030-05  10
014-030-030-06  10
014-030-030-07  10
014-030-030-08  10
014-030-030-09  10
014-030-030-10  10

但我需要更改的是序列从位置 014-001-010-01 序列 10 开始,到 014-030-030-10 到 610 结束

所以基本上从 014-001-010-01 = 10 然后 014-030-010-01 20 等等然后 014-001-010-02 30 然后 014-030-010-02 40 等等 10

locations       sequence
------------------------
014-001-010-01  10
014-001-010-02  30
014-001-010-03  50
014-001-010-04  70
014-001-010-05  90
014-001-010-06  110
014-001-010-07  130
014-001-010-08  150
014-001-010-09  170
014-001-010-10  190
014-001-020-01  210
014-001-020-02  230
014-001-020-03  250
014-001-020-04  270
014-001-020-05  290
014-001-020-06  310
014-001-020-07  330
014-001-020-08  350
014-001-020-09  370
014-001-020-10  390
014-001-030-01  410
014-001-030-02  430
014-001-030-03  450
014-001-030-04  470
014-001-030-05  490
014-001-030-06  510
014-001-030-07  530
014-001-030-08  550
014-001-030-09  570
014-001-030-10  600
014-030-010-01  20
014-030-010-02  40
014-030-010-03  60
014-030-010-04  80
014-030-010-05  100
014-030-010-06  120
014-030-010-07  140
014-030-010-08  160
014-030-010-09  180
014-030-010-10  200
014-030-020-01  220
014-030-020-02  240
014-030-020-03  260
014-030-020-04  280
014-030-020-05  300
014-030-020-06  320
014-030-020-07  340
014-030-020-08  360
014-030-020-09  380
014-030-020-10  400
014-030-030-01  420
014-030-030-02  440
014-030-030-03  460
014-030-030-04  480
014-030-030-05  500
014-030-030-06  520
014-030-030-07  540
014-030-030-08  560
014-030-030-09  580
014-030-030-10  610
sql sql-server sql-update sql-server-2016
1个回答
0
投票

有可能
你的

l.name
的模板为
abc-def-ghi-jk

014-001-010-01

获取名称的每个部分,并在 row_number() 分区和 order 子句中按正确的顺序使用。

参见示例

select 
   row_number()over(order by abc,ghi,jk,def)*10 sequenceNew
  ,row_number()over(order by abc,ghi,jk,def)*10 -sequence dif
  ,lo.* 
from(select *
  ,substring(l.name,1,3)  abc
  ,substring(l.name,9,3)  ghi
  ,substring(l.name,13,2) jk
  ,substring(l.name,5,3)  def
from locations l
) lo
order by sequence;

小提琴

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.