错误消息
ORDER BY子句在视图,内联函数,派生表,子查询和公用表表达式中无效,除非还指定了TOP,OFFSET或FOR XML。
代码:
cnx.ConnectionString = "User ID=MYID;Password="+chr(34)+"MyPass"+chr(34)+" ;Data Source=MyTEST"
答案 0 :(得分:0)
将第一个ORDER BY移到最后:
SELECT *
FROM
(SELECT
t.[Statement_ID], t.[InvoiceID],
t.S_Type as Type, t.Description, t.Date,
t.Debit, t.Credit, b.Balance
FROM
Statement as t
CROSS apply
(SELECT Balance = SUM(Debit) - SUM(Credit)
FROM Statement as x
WHERE (x.date < t.date or x.date = t.date and x.[Statement_ID] <= t.[Statement_ID] )
AND x.CustID = t.CustID ) b
WHERE
t.CustID = '48'
AND date >= '2015-01-01' AND date <= '2016-01-01'
) x
ORDER BY
Date, InvoiceID, Statement_ID