下面提到的代码可以更加紧凑,我试图将相同机架的电缆长度设置为相同的机架固定,其中长度固定为6mts,然后从一个机架到另一个机架,每个机架增加0.5mt并且剩下的是一些随机长度 数据库r包含以下列
ID RACK_A RACK_B LENGTH
1 RK 1 RK 1
2 RK 2 RK 1
3 RK 1 RK 2
4 MCR DESK PCR
5 CAR INGEST
6 PCR CAR
等约2000个条目
UPDATE MASTER_SCHEDULE
SET CABLE_LENTH =
switch(
RACK_A='RK 1' AND RACK_B='RK 1', 6
, RACK_A='RK 2' AND RACK_B='RK 2', 6
, RACK_A='RK 3' AND RACK_B='RK 3', 6
, RACK_A='RK 4' AND RACK_B='RK 4', 6
, RACK_A='RK 5' AND RACK_B='RK 5', 6
, RACK_A='RK 6' AND RACK_B='RK 6', 6
, RACK_A='RK 7' AND RACK_B='RK 7', 6
and so on say more 20 statements all with LENGTH = 6
, RACK_A='RK 1' AND RACK_B='RK 2' OR RACK_A = 'RK 2' AND RACK_B = 'RK 1',6.5
, RACK_A='RK 1' AND RACK_B='RK 3' OR RACK_A = 'RK 3' AND RACK_B = 'RK 1',7
, RACK_A='RK 1' AND RACK_B='RK 4' OR RACK_A = 'RK 4' AND RACK_B = 'RK 1',7.5
, RACK_A='RK 1' AND RACK_B='RK 5' OR RACK_A = 'RK 5' AND RACK_B = 'RK 1',8
, RACK_A='RK 1' AND RACK_B='RK 6' OR RACK_A = 'RK 6' AND RACK_B = 'RK 1',8.5
, RACK_A='RK 1' AND RACK_B='RK 7' OR RACK_A = 'RK 7' AND RACK_B = 'RK 1',9
and so on each LENGTH incremented by +.5
, RACK_A= 'MCR DESK' AND RACK_B='PCR=' OR RACK_A= 'PCR' AND RACK_B='MCR_DESK',54
, RACK_A= 'CAR' AND RACK_B='INGEST' OR RACK_A= 'INGEST' AND RACK_B='CAR',47
and so on each length is random figure ,
, true, 0)
WHERE ID>=1 AND ID<= 2000
答案 0 :(得分:0)
下面是SQL Server的代码,我将在短时间内为您提供MS ACCESS的代码。但是如果您知道我在查询中使用的MS-ACCESS的类似功能,那么您可以自己尝试转换
UPDATE MASTER_SCHEDULE
SET LENGTH = ABS( CAST(SUBSTRING(RACK_A, CHARINDEX(' ',RACK_A)+1, LEN(RACK_A) - CHARINDEX (' ',RACK_A)) AS INT)
- CAST(SUBSTRING(RACK_B, CHARINDEX(' ',RACK_B)+1, LEN(RACK_B) - CHARINDEX(' ',RACK_B)) AS INT)) * 0.5 + 6
我在查询中执行的操作是提取列RACK_A
和RACK_B
的整数部分,减去值,取结果的绝对值(如果减法导致负值则需要),乘以0.5,最后加6。
看到它在SQL Fiddle工作:http://sqlfiddle.com/#!3/0abad/1
以下是MS-ACCESS的相应代码:
UPDATE MASTER_SCHEDULE SET MASTER_SCHEDULE.LENGTH = Abs(CInt(Mid(RACK_A,InStr(1,RACK_A,' ')+1,Len(RACK_A)-InStr(1,RACK_A,' ')))-CInt(Mid(RACK_B,InStr(1,RACK_B,' ')+1,Len(RACK_B)-InStr(1,RACK_B,' '))))*0.5+6;
让我知道它是否按预期工作。
答案 1 :(得分:0)
根据此处给出的样本数据,我假设列RACK_A和RACK_B中的所有数据都具有格式“RK {Room Number}”。然后你可以尝试这个查询:
UPDATE MASTER_SCHEDULE
SET CABLE_LENTH =
ABS((RIGHT(RACK_A, LEN(RACK_A)-3) - RIGHT(RACK_B, LEN(RACK_B)-3)))
*
0.5
+
6
WHERE ID>=1 AND ID<= 2000