我是SQL的新手,我正在编写一个脚本来提取在2015年9月30日之前发生的事务。我收到了一条错误消息 - 关键字' WHERE'。
附近的语法不正确我做错了什么?
SELECT
T1.Account,
T2.AcctName,
SUM(T1.Debit - T1.Credit) AS 'Balance',
Sum(T1.SysDeb - T1.SysCred) AS 'USDBalance'
FROM OJDT T0
INNER JOIN JDT1 T1 ON T0.TransId = T1.TransId
Left Join OACT T2 ON T2.AcctCode = T1.Account
GROUP BY
T1.Account, T2.AcctName WHERE T1.Refdate <= '09-30-2015'
答案 0 :(得分:5)
WHERE
位于GROUP BY
:
Select T1.Account,
T2.AcctName,
Sum(T1.Debit - T1.Credit) As 'Balance',
Sum(T1.SysDeb - T1.SysCred) As 'USDBalance'
From OJDT T0
Inner Join JDT1 T1 ON T0.TransId = T1.TransId
Left Join OACT T2 ON T2.AcctCode = T1.Account
Where T1.Refdate <= '09-30-2015'
Group By T1.Account, T2.AcctName
答案 1 :(得分:1)
您可以使用CROSS APPLY
来简化GROUP BY
的一些复杂性。在下面,此代码执行并返回与上述答案相同的结果。
SELECT
balances.Account,
balances.AcctName,
balances.Balance AS 'Balance',
balances.USDBalance AS 'USDBalance'
FROM OJDT T0
CROSS APPLY (
SELECT
SUM(T1.Debit - T1.Credit) AS Balance,
Sum(T1.SysDeb - T1.SysCred) AS USDBalance,
T1.Account AS Account,
T2.AcctName AS AcctName
FROM JDT1 T1
LEFT OUTER JOIN OACT T2 ON T2.AcctCode = T1.Account
WHERE T1.TransId = T0.TransId AND T1.Refdate <= '09-30-2015'
) balances