views:

45

answers:

3

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 the 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]
+2  A: 

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
Chris Diver
Create and display a VIEW that displays the account balance subtotals for each account group. Account group is defined as the first two digits of the account number. Display a grand total of the balance column at the end of the display.
rhonda
I've edited the post, but the question isn't clear whether they are two separate queries or the output from the view.
Chris Diver
+1  A: 

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]
Amber
Thank you... just what I needed.
rhonda
+1  A: 

Try using SUM in conjunction with the view in a query.

Will A
N.B. - was assuming that the view shouldn't be modified, and needed to be used in the answer.
Will A