Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
BigRetina
Posting Yak Master
144 Posts |
Posted - 2003-04-06 : 09:28:18
|
| Hi..I have the following tableAccountNo Balance--------- -------1 0110 50120 2512011 25How can I write a select statement that will give me the Balance of accountno 1 to be 100..that is accoutns (110,120,12011) are sub accounts from acount no 1..so their balances will sum up in the main account no.The account no column is a char(10)...maybe a grouping on the first cahracter..but how to do this?Thanks In Advance |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-04-06 : 11:14:58
|
| select sum(Balance), left(AccountNo,1)from tblgroup by left(AccountNo,1)An exceedingly bad table design and almost certainly wrong (as in not being able to hold consistent data).Anyone want to guess the next question.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
BigRetina
Posting Yak Master
144 Posts |
Posted - 2003-04-07 : 01:52:34
|
lol..first..this is just two columns of the table..so the design is not BAD as u thought it is!..i just needed that for specific report!.hierarchy of accounts (logically) is implied in the CODING. that is it!Second..it is not mine!..although I think it is a good design!..lolso I shouldnt be taking credit for it..IF ANY! |
 |
|
|
BigRetina
Posting Yak Master
144 Posts |
Posted - 2003-04-07 : 04:30:59
|
| For the first level it worked fine!!..that is LEFT(MainAccountNo,1)..but..for the second level this the query I am using :SELECT LEFT(MainAccountNo,3) AccountNo, SUM(Balance) BalanceFROM Accounts A INNER JOIN AccountsTotals B ON A.AccountId = B.AccountIdWHERE A.FiscalYear = 2003Group By LEFT(MainAccountNo,3)It is showing the following results :AccountNo Balance--------- --------1 .000110 25 115 .000117 25120 50...etc...What I WANT is the following : AccountNo Balance--------- --------1 100 110 25 115 .000117 25120 50note : account 1 actually has no balance but it is the SUM of any sub account below it in the tree...ofcourse account 110 is the same and so on!..I would appreciate it if u can help!Thanks AgainEdited by - bigretina on 04/07/2003 04:31:53 |
 |
|
|
|
|
|