sql >> Base de Datos >  >> RDS >> Sqlserver

La cláusula ORDER BY no es válida en vistas, funciones en línea, tablas derivadas

Mover primero ORDENAR POR hasta el 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