Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

A cláusula ORDER BY é inválida em visualizações, funções embutidas, tabelas derivadas


Mova primeiro ORDER BY para o final:
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