SQL 查询 - 多个 ISNULL 和计算的问题
SQL Query - Issues with Multiple ISNULL's and Calculations
我写了这个基本上计算项目未来价格的查询,有点像 'What If' 报告。我在 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 命令时遇到的错误:
消息 102,级别 15,状态 1,第 17 行
')' 附近的语法不正确。
任何帮助都会很棒,我很确定我有很多括号(或太少)但我似乎无法使查询正常工作。
大部分括号都是不必要的。我猜你想要第一个 (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
我写了这个基本上计算项目未来价格的查询,有点像 'What If' 报告。我在 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 命令时遇到的错误:
消息 102,级别 15,状态 1,第 17 行 ')' 附近的语法不正确。
任何帮助都会很棒,我很确定我有很多括号(或太少)但我似乎无法使查询正常工作。
大部分括号都是不必要的。我猜你想要第一个 (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