views:

50

answers:

4

I am a student this is homework. I use SQL server to check my work but have to write script by hand.

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.

I have one table with 4 columns. Account(numbers), Description, Short_Description, and Balance(money). The Account numbers range from 100001-610003. COA is the chart of accounts that is an excel link. Thanks for any advice.

This is what I have so far...

CREATE VIEW [account_balance_sums]
 AS 
 SELECT Account, Short_Description,Balance
 FROM COA
 Where Account, (first two digits 10-61 of account #)
 AND sum
 GO

SELECT * FROM [account_balance_sums]
+1  A: 

I don't want to do it for you as you will never learn but you will need the GROUP BY clause.

Chris Diver
Thanks... that is for grouping by the first 2 numbers of the account. How do I pick the first 2 numbers?
rhonda
You will need a function, you can specify functions in the GROUP BY clause, have a think about it, give it a try and come back here if you get stuck. It will be different depending on the way the account number is stored, is it a VARCHAR or a INT.
Chris Diver
Yes, you can specify non-aggregate functions in the GROUP BY clause. `GROUP BY LEFT(account, 2)` works for me on SQL Server 2005...
OMG Ponies
That's what I said.
Chris Diver
+1  A: 

Given that this is homework I just want to give you enough to get going, but it seems to me that you want to do something like

SELECT ...., SUM(Balance) as group_subtotal, SUBSTRING(Account,0,2) AS account_group
FROM ....
GROUP BY SUBSTRING(Account,0,2), ...
JacobM
@JacobM is there a benefit to use SUBSTRING instead of LEFT?
Shaded
No, LEFT makes sense in this context. For a student, if you're only going to learn one command, SUBSTRING is good to understand as it is more general.
JacobM
You need to correct the GROUP BY - it won't return an error, but the sum won't reflect the LEFT/SUBSTRING for grouping - you'll get a row for each group.
OMG Ponies
He's actually grouping by the alias (which isn't allowed in SQL Server)
Tom H.
@Tom yes that is correct, it's worth noting that you can `ORDER BY` an alias.
Chris Diver
A: 

Since this is homework, I don't want to give it away, but you'll need to add up the balances, so you'll need to use the SUM function.

Also, since you want to SUM them by the first two digits of the account number, you'll need to use GROUP BY to create the groups, and SUBSTR to get the first two digits.

Exactly how you put it all together is the real trick of course.

Eric Petroelje
+1  A: 

So, GROUP BY and SUM will get you most of the way. Then use WITH CUBE/ROLLUP to get the grand total. You may want to look into the GROUPING() function if you go this route.

Daniel P
Thanks looking into that cube rollupHere is what I have so far...CREATE VIEW [account_balance_sums]AS SELECT SUM(balance) AS total, SUBSTRING(Account,0,2) AS account_group FROM COA GROUP BY account_group GOSELECT * FROM [account_balance_sums]
rhonda
Does that work? you need to put your SUBSTRING function in the GROUP BY i'd expect.
Chris Diver