I am a student this is homework... I have one table with four columns: Accounts(numbers), balances(money), and two for descriptions. I have to get a grand total of th开发者_如何学编程e balances... This VIEW shows the sums of the balance column but I need to total the sums, also. Any help pointing me in the right direction will be appreciated.
CREATE VIEW [account_balance_sums]
AS
SELECT SUM(balance) AS total,
SUBSTRING(Account,0,2) AS account_group,
FROM COA
GROUP BY account_group
GO
SELECT * FROM [account_balance_sums]
You just want the total of all balances?
SELECT Sum(Balances)
FROM COA
Additionally your VIEW
will not work as you cannot have an alias in a GROUP BY
clause..
Edit after comment...
I'm not sure whether the question implies that the grand total should be a part of the view, also is your account number column numeric? As SUBSTRING will not work.
CREATE VIEW viewAccount_Balance_Sums
AS
SELECT SUM(Balance) as Total, LEFT(Account,2) AS Account_group
FROM COA
GROUP BY LEFT(Account,2)
UNION ALL
SELECT SUM(Balance), 'Grand Total'
FROM COA
Try totaling them the same way the view creates a total for each account, by using SUM?
SELECT SUM(balance) FROM COA
(Just don't GROUP BY
, so that you get a full total instead of just a per-accountgroup total.)
Alternatively, you could sum the account totals returned from the view:
SELECT SUM(total) FROM [account_balance_sums]
Try using SUM in conjunction with the view in a query.
SELECT SUM(balance) AS total, SUBSTRING(Account,0,2) AS account_group
FROM COA
GROUP BY account_group wirh rollup
精彩评论