对不起,如果之前有人问过并回答过,但我找不到能解决我特殊问题的答案。
尝试运行ssrs查询时收到以下消息,我不知道如何更正它。通常情况下,错误指的是一个额外的空间或者可以从复制和粘贴中获取的任何空间,但是如果有空的话我就找不到它,而且我不确定问题可能是什么。以下是查询后面的错误消息。任何帮助/意见将不胜感激
执行查询时发生错误。 ' ='附近的语法不正确。
其他信息:
' ='附近的语法不正确。 (Microsoft SQL Server,错误:102)
------------------------------"
SELECT
company.cmp_name
,commodity.cmd_code
,commodity.cmd_name
,IIF(vTTSTMW_ChargeDetails.[Delivery Year]=@DeliveryYear, SUM(vTTSTMW_ChargeDetails.Charge),0) AS [Revenue Current Year]
,IIF(vTTSTMW_ChargeDetails.[Delivery Year]=@DeliveryYear-1, SUM(vTTSTMW_ChargeDetails.Charge),0) AS [Revenue Prior Year]
FROM
company
LEFT OUTER JOIN vTTSTMW_ChargeDetails
ON company.cmp_id = vTTSTMW_ChargeDetails.[Bill To ID]
LEFT OUTER JOIN commodity
ON vTTSTMW_ChargeDetails.[Order CMD Code] = commodity.cmd_code
GROUP BY
company.cmp_name
,commodity.cmd_code
,commodity.cmd_name
答案 0 :(得分:0)
如果您使用的是早于2012版本的SQL,请尝试更改此内容:
,IIF(vTTSTMW_ChargeDetails.[Delivery Year]=@DeliveryYear, SUM(vTTSTMW_ChargeDetails.Charge),0) AS [Revenue Current Year]
,IIF(vTTSTMW_ChargeDetails.[Delivery Year]=@DeliveryYear-1, SUM(vTTSTMW_ChargeDetails.Charge),0) AS [Revenue Prior Year]
到此:
,SUM(CASE WHEN vTTSTMW_ChargeDetails.[Delivery Year]=@DeliveryYear THEN vTTSTMW_ChargeDetails.Charge ELSE 0 END) AS [Revenue Current Year]
,SUM(CASE WHEN vTTSTMW_ChargeDetails.[Delivery Year]=@DeliveryYear-1 THEN vTTSTMW_ChargeDetails.Charge ELSE 0 END) AS [Revenue Prior Year]
修改
尝试改变这个:
@DeliveryYear-1
对此:
CONVERT(nvarchar(7), CAST(@DeliveryYear AS int)-1)
答案 1 :(得分:0)
您可能需要将聚合放在IIF语句之外,如下所示:
SELECT
company.cmp_name
, commodity.cmd_code
, commodity.cmd_name
, SUM(
IIF(
vTTSTMW_ChargeDetails.[Delivery Year] = @DeliveryYear,
vTTSTMW_ChargeDetails.Charge,
0
)
) AS [Revenue Current Year]
, SUM(
IIF(
vTTSTMW_ChargeDetails.[Delivery Year] = @DeliveryYear - 1,
vTTSTMW_ChargeDetails.Charge,
0
)
) AS [Revenue Prior Year]
FROM
company
LEFT OUTER JOIN vTTSTMW_ChargeDetails ON company.cmp_id = vTTSTMW_ChargeDetails.[Bill To ID]
LEFT OUTER JOIN commodity ON vTTSTMW_ChargeDetails.[Order CMD Code] = commodity.cmd_code
GROUP BY
company.cmp_name
, commodity.cmd_code
, commodity.cmd_name