My friend sent me the commands that he wrote in server 2008 and they worked with no problems, mine however from a copy and past did not work with 2012. Is there any reason why? Here is the code:
Use Kudler_Database
SELECT AccountNumber, [Description], ShortDescription,Balance
FROM Chart_of_Accounts
ORDER BY left (AccountNumber, 2)
COMPUTE SUM(Balance) BY left (AccountNumber, 2)
COMPUTE SUM(Balance);
Here is the error :
Msg 156, Level 15, State 1, Line 6 Incorrect syntax near the keyword 'COMPUTE'.
COMPUTE
is no longer available in SQL server 2012, thats why you are getting that error. See this page:
It said that:
This topic describes the Database Engine features that are no longer available in SQL Server 2012:
*Transact-SQL syntax | COMPUTE / COMPUTE BY *
A kind of hack with RollUp since Compute By is deprecated in SQL Server 2012 - (see "SQL SERVER – Use ROLL UP Clause instead of COMPUTE BY")
DECLARE @t TABLE(AccountNumber VARCHAR(10),[Description] VARCHAR(100),ShortDescription VARCHAR(100),Balance INT)
INSERT INTO @t SELECT '1234567890','Some Description for 1st Account','Short Description for 1st Account',2000 Union All
SELECT '2345678901','Some Description for 2nd Account','Short Description for 2nd Account',3000 Union All
SELECT '1234567890','Some Description for 1st Account','Short Description for 1st Account',4000
SELECT
AccountNumber
,Balance
,Total = SUM(Balance)
FROM @t
GROUP BY AccountNumber,Balance
WITH ROLLUP
Result
AccountNumber Balance total
1234567890 2000 2000
1234567890 4000 4000
1234567890 NULL 6000
2345678901 3000 3000
2345678901 NULL 3000
NULL NULL 9000
OR
you can use the below
DECLARE @t TABLE(AccountNumber VARCHAR(10),[Description] VARCHAR(100),ShortDescription VARCHAR(100),Balance INT)
INSERT INTO @t SELECT '1234567890','Some Description for 1st Account','Short Description for 1st Account',2000 Union All
SELECT '2345678901','Some Description for 2nd Account','Short Description for 2nd Account',3000 Union All
SELECT '1234567890','Some Description for 1st Account','Short Description for 1st Account',4000
;With CTE AS
(
SELECT
AccountNumber
,[Description]
,ShortDescription
,Balance
,SubTotal = SUM(Balance) OVER (PARTITION BY AccountNumber ORDER BY LEFT (AccountNumber, 2))
,Rn = ROW_NUMBER() OVER(PARTITION BY AccountNumber ORDER BY LEFT (AccountNumber, 2))
FROM @t)
SELECT
AccountNumber
,[Description]
,ShortDescription
,Balance = CAST(Balance AS VARCHAR(10))
,SubTotal = CASE WHEN Rn != 1 THEN NULL ELSE SubTotal END
FROM CTE
UNION ALL
SELECT
' ', ' ',' ' ,'Total Amount' , SUM(Balance) FROM CTE
The output being
AccountNumber Description ShortDescription Balance SubTotal
1234567890 Some Description for 1st Account Short Description for 1st Account 2000 6000
1234567890 Some Description for 1st Account Short Description for 1st Account 4000 NULL
2345678901 Some Description for 2nd Account Short Description for 2nd Account 3000 3000
Total Amount 9000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With