2014-01-16 87 views
0

我給查詢和表列名稱與數據類型。使用SQL查詢計算單位費率和數量查詢

Column name  Data Type 
iBasicRate  Decimal    
iTaxesAndLevies Decimal 
vsUnitRate  nchar 
iAmount   Deciaml 
vsQuantity  Deciaml 

首先查詢查找的單位費率

公式是Unit Rate=Basic Rate + Taxes

CASE tBOQ.iBOQLineItemType WHEN '3' THEN Convert(VARCHAR,CONVERT(DECIMAL,tBOQ.iBasicRate)) + Convert(VARCHAR,CONVERT(DECIMAL,tBOQ.iTaxesAndLevies)) 
      WHEN '5' THEN Convert(VARCHAR,CONVERT(DECIMAL,tBOQ.iBasicRate)) + Convert(VARCHAR,CONVERT(DECIMAL,tBOQ.iTaxesAndLevies))  
      ELSE '---'  
      END as vsUnitRate, 

在此查詢,這是不給加(添加量),它只是附加的下一個量。 例子:

Basic rate  Tax  Unit rate(Basic rate+tax) 
4500.00  225.00 4500225 (Show like this) 

和第二查詢查找金額

公式是Unit Rate*Quantity=Amount

CASE tBOQ.iBOQLineItemType WHEN '3' THEN CONVERT(varchar,tBOQ.iAmount) 
      WHEN '5' THEN CONVERT(varchar,tBOQ.iAmount)   
      ELSE '---'   
      END as iAmount , 
+0

而什麼是問題? –

+0

爲什麼使用'convert'?當itemtype = 3時,然後basicrate + taxe結束爲xxx1,當itemtype = 5時,case .... then結束爲xxx2 –

+0

爲什麼你的unitrate是nchar類型?將其更改爲十進制並刪除轉換。 – Srini

回答

0

也許這樣的事情?:

CASE WHEN tBOQ.iBOQLineItemType in ('3', '5') THEN tBOQ.iBasicRate + 
                tBOQ.iTaxesAndLevies 
      ELSE 0  
      END as vsUnitRate, 

CASE WHEN tBOQ.iBOQLineItemType in ('3', '5') THEN (tBOQ.iBasicRate + 
                tBOQ.iTaxesAndLevies) * 
                tBOQ.vsQuantity  
      ELSE 0   
      END as iAmount ,