2014-02-17 65 views
0

可以將下面提到的代碼製作得更緊湊,我試圖將同一機架的電纜長度定爲長度固定爲6mts的同一機架insatallation,然後從一個機架到另一個機架由.5mtrs每個遞增,其餘是 具有下述列如何計算相對於兩列條目的長度

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

從這裏給出的樣本數據中,我假定列RACK_A和RACK_B中的所有數據格式爲「RK {房間號}」。然後,你可以試試這個查詢:

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 

參考:MS Access: Functions

+0

它給了標準表達式中的數據類型不匹配代碼3030 SQLSTATE 22005 –

+0

確保我的假設是正確的:「*列RACK_A和RACK_B中的所有數據格式爲」RK {房間號}「*」。否則,查詢將不起作用,我們需要找到另一種方法從RACK_A和RACK_B中獲取房間號。 – har07

+0

「CABLE_LENTH」列的數據類型是什麼? – har07

0

下面數據庫ř一些隨機長度爲SQL代碼服務器,我會爲你提供他在短時間內也爲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_ARACK_B,減去整數部分該值取絕對結果(如果減法結果爲負值則需要)乘以0.5並最後加上6。

在看到它的SQL小提琴工作: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; 

讓我知道它的工作如預期。

+0

它給我的條件表達式代碼數據類型不匹配3030 SQLSTATE 22005 –

+0

是什麼長度字段的數據類型? –

+0

長度字段的數據類型是SHORTTEXT,我嘗試更改爲NUMBER,但它給予的同樣錯誤 –