我有一个查询,它给我语法错误。我没有得到它。请帮忙整理一下。
SELECT
T3.[AcctName],
SUM(T0.[DebLTotal]/85) AS buget,(Select sum(T3.[Debit]/85) From JDT1 T3 ) as 'CurrentBudget'
FROM
OBGT T0
INNER JOIN
(SELECT
CASE WHEN T1.[AcctCode] = '5001' THEN T1.[AcctName] = 'Salaries'
END as [AcctName],
[AcctCode]
FROM
OACT AS T1) AS T3 ON T0.[AcctCode] = T3.[AcctCode]
INNER JOIN
OBGS T2 ON T0.[Instance] = T2.[AbsId]
WHERE
T2.[Name] = 'Main Budget 2015'
GROUP BY
T3.[AcctName], T2.[Name]
我在这一行收到Incorrect syntax near '='
错误
SELECT CASE WHEN T1.[AcctCode] = '5001' THEN T1.[AcctName] = 'Salaries'
答案 0 :(得分:3)
问题是你在CASE
表达式中间的作业:
SELECT
CASE WHEN T1.[AcctCode] = '5001' THEN T1.[AcctName] = 'Salaries'
END as [AcctName],
[AcctCode]
FROM
OACT AS T1
这将永远不会有效,因为CASE
只是T-SQL中的表达式 - 它只能返回一个原子值 - 它无法进行分配或控制代码流。
您需要使用以下内容:
SELECT
[AcctName] = CASE
WHEN [AcctCode] = N'5001' THEN 'Salaries'
ELSE 'Other'
END,
[AcctCode]
FROM
OACT AS T1
如果您的AcctCode
是5001 - 那么您使用Salaries
作为AcctName
(如果它是另一个值 - 您想做什么?)
更新:不确定仅执行其他部分的意思 - 请查看此演示:
DECLARE @t1 TABLE (ID INT NOT NULL, AcctCode INT)
INSERT INTO @T1 VALUES(1, 5000), (2, 5001), (3, 5000), (4, 5002), (5, 5001), (6, 4999)
SELECT
[AcctName] = CASE
WHEN [AcctCode] = 5001 THEN 'Salaries'
ELSE 'Other'
END,
[AcctCode]
FROM @T1
输出将清楚地显示我的CASE
表达式正常工作 - 对于5001
,AcctName
的值设置为Salaries
,而对于所有其他值,它返回Other
。
答案 1 :(得分:0)
这可能是因为你把'围绕一个数字?尝试将其写为T1.[AcctCode] = 5001
答案 2 :(得分:0)
这应解决您的问题,以下行不正确
SELECT CASE WHEN T1.[AcctCode] = '5001' THEN T1.[AcctName] = 'Salaries'
SELECT T3.[AcctName] ,
SUM(T0.[DebLTotal] / 85) AS buget ,
( SELECT SUM(T3.[Debit] / 85)
FROM JDT1 T3
) AS 'CurrentBudget'
FROM OBGT T0
INNER JOIN ( SELECT CASE WHEN T1.[AcctCode] = '5001' THEN 'Salaries'
ELSE T1.[AcctName]
END AS [AcctName] ,
[AcctCode]
FROM OACT AS T1
) AS T3 ON T0.[AcctCode] = T3.[AcctCode]
INNER JOIN OBGS T2 ON T0.[Instance] = T2.[AbsId]
WHERE T2.[Name] = 'Main Budget 2015'
GROUP BY T3.[AcctName] ,
T2.[Name]
答案 3 :(得分:0)
我只得到这个查询的其他部分。
SELECT
T3.[AcctName] ,
SUM(T0.[DebLTotal] / 85) AS buget ,
(SELECT SUM(T3.[Debit] / 85)
FROM JDT1 T3) AS 'CurrentBudget'
FROM
OBGT T0
INNER JOIN
(SELECT
AcctName = CASE
WHEN T1.[AcctCode] = '10' THEN 'Fixed Assets'
WHEN T1.[AcctCode] = '50' THEN 'Salaries'
ELSE 'Other'
END,
[AcctCode]
FROM
OACT AS T1) AS T3 ON T0.[AcctCode] = T3.[AcctCode]
INNER JOIN
OBGS T2 ON T0.[Instance] = T2.[AbsId]
WHERE
T2.[Name] = 'Main Budget 2015'
GROUP BY
T3.[AcctName], T2.[Name]
只有ELSE 'Other'
正在运作。无论我放在我的CASE WHEN
中,它都会给我一行,就像:
AcctName budget
Other 2312836