SQL查询 - 多个ISNULL和计算的问题

时间:2015-03-05 20:49:56

标签: sql sap

我编写了这个查询,它基本上计算了物品的未来价格,一种“假设”报告。虽然INSULL在select命令中进行了最后两次计算,但我有一些问题,我不太清楚我做错了什么,下面是完整的查询:

SELECT
    T1.[ItemCode],
    T2.[ItemName],
    T2.Cardcode, 
    T4.Price as 'Buying Price',
    T4.Currency as 'Buy Currency',
    isnull(T6.Rate,1) as 'Todays Exchange Rate (Buy)',
    ( ISNULL(T4.price,1)/ ISNULL (T6.Rate,1)) as 'Todays Buying Price',
    T0.Cardcode as 'Customer code',
    T0.[CardName] as 'Filler',
    T3.Price as 'Selling Price',
    T3.Currency as 'Sell Currency',
    isnull(T5.Rate,1) as 'Todays Exchange Rate (Sell)',
    (ISNULL(T3.price,1)/ ISNULL (T5.Rate,1)) as 'Todays Selling Price',
    (ISNULL(T3.price,1)/ ISNULL (T5.Rate,1))-(ISNULL(T4.price,1)/ ISNULL (T6.Rate,1))/( ISNULL(T3.price,1))*100) as 'Gross %',
    (((( ISNULL(T4.price,1)/ ISNULL (T6.Rate,1))+ T2.CstGrpCode)* T2.U_Shipping_Percent)/(( ISNULL(T3.price,1))/( ISNULL (T5.Rate,1))))) *100 as 'Net % exc. outbound cost'
FROM
    ORDR T0
    Inner join RDR1 T1 on T0.docentry = T1.docentry
    Inner join OITM T2 on T1.itemcode = T2.itemcode
    left outer join ITM1 T3 on T1.itemcode = T3.itemcode and T3.pricelist = 2
    left outer join ITM1 T4 on T1.itemcode = T4.itemcode and T4.pricelist = 1
    left outer join ORTT T5 on T3.Currency = T5.Currency and CONVERT(varchar(10), T5.RateDate, 102) = CONVERT(varchar(10), getdate(), 102)
    left outer join ORTT T6 on T4.Currency = T6.Currency and CONVERT(varchar(10), T6.RateDate, 102) = CONVERT(varchar(10), getdate(), 102)
Where
    T3.Price <> 0 and
    T4.Price <> 0       
Order by
    T0.DocEntry

这是我在上一次计算select命令时遇到的错误:

Msg 102,Level 15,State 1,Line 17 ')'附近的语法不正确。

任何帮助都会很棒,我很确定我有很多支架(或者太少),但我似乎无法让查询起作用。

1 个答案:

答案 0 :(得分:0)

大部分的parens是不必要的。我猜你想要(A-B)/ C作为第一个。

(ISNULL(T3.price,1) / ISNULL(T5.Rate,1) - ISNULL(T4.price,1) / ISNULL(T6.Rate,1)) /
    ISNULL(T3.price,1) / 100 as 'Gross %',

第二个相当于此,我认为(T4 / T6 + T2)/(T3 * T5):

(ISNULL(T4.price,1) / ISNULL(T6.Rate,1) + T2.CstGrpCode) * T2.U_Shipping_Percent) /
    ISNULL(T5.Rate,1) / ISNULL(T3.price,1) * 100