Author |
Topic |
adbasanta
Posting Yak Master
120 Posts |
Posted - 2012-08-12 : 09:30:42
|
This is a continuation of my previous Financial Reporting post [url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=177014[/url]. I have this query below that displays the hierarchical view of a balance sheet base on BalanceSheet_Template Table.use MFR_Merchandisego; with BalanceSheet (AccountID, AccountName, ParentID,AccountOrder)As ( Select AccountID, Display=convert(varchar(1000),AccountName), ParentID, AccountOrder From BalanceSheet_Template), AccountPart1AS(Select Level=1, AccountID, Display=convert(varchar(1000),AccountName), ParentID, AccountOrderFrom BalanceSheet AWhere A.ParentID is NULLUnion AllSelect Level=B.Level + 2, A.AccountID, Display=convert(varchar(1000),replicate(' ', B.Level) + AccountName), A.ParentID, A.AccountOrder From BalanceSheet A Inner Join AccountPart1 B On B.AccountID = A.ParentID)Select * From AccountPart1Order By AccountOrder This is the result:Level AccountID Display ParentID AccountOrder1 1 ASSETS NULL 13 2 Current Assets 1 25 3 Checking/Savings 2 37 4 Bank 3 45 5 Accounts Receivable 2 55 6 Other Current Assets 2 63 7 Fixed Asset 1 73 8 Other Asset 1 81 9 LIABILITIES & EQUITY NULL 93 10 Liabilities 9 105 11 Current Liabilities 10 117 12 Accounts Payable 11 127 13 Credit Card 11 137 14 Other Current Liability11 145 15 Long Term Liability 10 153 16 Equity 9 165 17 Net Income 16 17 I need to join this to tbl_accnt_list table base on ParentID to get the sub accounts. So the result must be something below as marked in color red:Level AccountID Display ParentID AccountOrder1 1 ASSETS NULL 13 2 Current Assets 1 25 3 Checking/Savings 2 37 4 Bank 3 4 Union Bank 4 World Bank 4 5 5 Accounts Receivable 2 5 Cellcard 5 Collectible Taxes 55 6 Other Current Assets 2 63 7 Fixed Asset 1 73 8 Other Asset 1 81 9 LIABILITIES & EQUITY NULL 93 10 Liabilities 9 105 11 Current Liabilities 10 117 12 Accounts Payable 11 12 Dressed Chicken 12 Cellcard 127 13 Credit Card 11 137 14 Other Current Liability11 145 15 Long Term Liability 10 153 16 Equity 9 165 17 Net Income 16 17 Any hep is greatly appreciated!Thank you & God bless!-------------------------------------------------------------------------------------------------------Learning MS SQL Server 2008 |
|
lionofdezert
Aged Yak Warrior
885 Posts |
Posted - 2012-08-12 : 09:42:13
|
And whats the question ???--------------------------http://connectsql.blogspot.com/ |
 |
|
adbasanta
Posting Yak Master
120 Posts |
Posted - 2012-08-12 : 09:49:13
|
Thank you lionofdezert!The question is, how can I join this to tbl_accnt_list table base on ParentID to get the sub accounts. So the result must be something below as marked in color red:Level AccountID Display ParentID AccountOrder1 1 ASSETS NULL 13 2 Current Assets 1 25 3 Checking/Savings 2 37 4 Bank 3 4 Union Bank 4 World Bank 4 5 5 Accounts Receivable 2 5 Cellcard 5 Collectible Taxes 55 6 Other Current Assets 2 63 7 Fixed Asset 1 73 8 Other Asset 1 81 9 LIABILITIES & EQUITY NULL 93 10 Liabilities 9 105 11 Current Liabilities 10 117 12 Accounts Payable 11 12 Dressed Chicken 12 Cellcard 127 13 Credit Card 11 137 14 Other Current Liability11 145 15 Long Term Liability 10 153 16 Equity 9 165 17 Net Income 16 17 Any hep is greatly appreciated!Thank you & God bless!-------------------------------------------------------------------------------------------------------Learning MS SQL Server 2008 |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-12 : 11:38:10
|
where's the DDL for tbl_accnt_list table? without that we cant understand what columns it contain and how its related to main table------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
adbasanta
Posting Yak Master
120 Posts |
Posted - 2012-08-12 : 11:54:07
|
Thank you visakh16!Here is what the tbl_accnt_list columns which should be link on BalanceSheet_Template table.AccountID AccountName ParentID 21 Union Bank 4 22 World Bank 4 23 Cellcard 5 24 Collectible Taxes 5 25 Dressed Chicken 12 26 Cellcard 12 ........................ Where ParentID is the same as in the BalanceSheet_Template table.Thank you!-------------------------------------------------------------------------------------------------------Learning MS SQL Server 2008 |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-12 : 12:03:43
|
[code]use MFR_Merchandisego; with BalanceSheet (AccountID, AccountName, ParentID,AccountOrder)As ( Select AccountID, Display=convert(varchar(1000),AccountName), ParentID, AccountOrder From BalanceSheet_Template UNION ALL Select (-1) * AccountID, Display=convert(varchar(1000),AccountName), ParentID, NULL From tbl_accnt_list ), AccountPart1AS(Select Level=1, AccountID, Display=convert(varchar(1000),AccountName), ParentID, AccountOrderFrom BalanceSheet AWhere A.ParentID is NULLUnion AllSelect Level=B.Level + 2, ABS(A.AccountID), Display=convert(varchar(1000),replicate(' ', B.Level) + AccountName), A.ParentID, A.AccountOrder From BalanceSheet A Inner Join AccountPart1 B On B.AccountID = A.ParentID)Select * From AccountPart1Order By COALESCE(AccountOrder,ParentID)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
adbasanta
Posting Yak Master
120 Posts |
Posted - 2012-08-12 : 12:45:05
|
Thank you visakh16! Its almost working. But some sub accounts are positioned not under its Parent. Ive marked it in red.Level AccountID Display ParentID AccountOrder1 1 ASSETS NULL 13 2 Current Assets 1 25 3 Checking/Savings 2 37 4 Bank 3 49 275 Cash on Drawer_2 4 NULL9 276 Cash in Bankr_2 4 NULL5 5 Accounts Receivable 2 57 283 Owners Equity 5 NULL7 284 Sales 5 NULL7 285 Shipping & Delivery Income 5 NULL7 286 Freight Cost 5 NULL5 6 Other Current Assets 2 63 7 Fixed Asset 1 73 8 Other Asset 1 81 9 LIABILITIES & EQUITY NULL 93 10 Liabilities 9 105 11 Current Liabilities 10 117 12 Accounts Payable 11 129 289 Bank Charges 12 NULL9 290 Computer Expenses 12 NULL9 291 Depreciation Expense 12 NULL9 292 Insurance Enxpense 12 NULL9 293 Interest Expense 12 NULL9 294 Meals & Intertainment 12 NULL9 300 Repair & Maintainance 13 NULL9 301 Taxes - Property 13 NULL9 302 Communication 13 NULL9 303 Transportation 13 NULL9 304 Tax Payable 13 NULL7 13 Credit Card 11 137 14 Other Current Liability 11 145 15 Long Term Liability 10 153 16 Equity 9 165 17 Net Income 16 17 The accounts marked in red, must be under accounts marked in blue since its ParentID is the AccountID on the account in color blue.Thank you!-------------------------------------------------------------------------------------------------------Learning MS SQL Server 2008 |
 |
|
adbasanta
Posting Yak Master
120 Posts |
Posted - 2012-08-12 : 13:52:10
|
Thank you visakh16. I think the problem why some sub accounts was mis-positioned is because there are some sub accounts on my tbl_accnt_list that do not have ParentID which is not suppose to be. When I put on the ParentID, it works perfectly.One more thing to make this a perfect Balance Sheet, can we add a sub-total on each account that has sub accounts? Example results below, Total marked in red:Level AccountID Display ParentID AccountOrder1 1 ASSETS NULL 13 2 Current Assets 1 25 3 Checking/Savings 2 37 4 Bank 3 49 275 Cash on Drawer_2 4 NULL9 276 Cash in Bankr_2 4 NULL Total Bank Total Checking/Savings5 5 Accounts Receivable 2 57 283 Owners Equity 5 NULL7 284 Sales 5 NULL7 285 Shipping & Delivery Income 5 NULL7 286 Freight Cost 5 NULL Total Accounts Receivable5 6 Other Current Assets 2 6 Total Current Assets3 7 Fixed Asset 1 73 8 Other Asset 1 8 Total ASSETS1 9 LIABILITIES & EQUITY NULL 93 10 Liabilities 9 105 11 Current Liabilities 10 117 12 Accounts Payable 11 129 289 Bank Charges 12 NULL9 290 Computer Expenses 12 NULL9 291 Depreciation Expense 12 NULL9 292 Insurance Enxpense 12 NULL9 293 Interest Expense 12 NULL9 294 Meals & Intertainment 12 NULL Total Accounts Payable7 13 Credit Card 11 139 300 Repair & Maintainance 13 NULL9 301 Taxes - Property 13 NULL9 302 Communication 13 NULL9 303 Transportation 13 NULL9 304 Tax Payable 13 NULL Total Credit Card7 14 Other Current Liability 11 14 Total Current Liabilities5 15 Long Term Liability 10 15 Total Liabilities3 16 Equity 9 165 17 Net Income 16 17 Total Equity Total LIABILITIES & EQUITY Thank you!-------------------------------------------------------------------------------------------------------Learning MS SQL Server 2008 |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-12 : 15:59:12
|
this is something which can be very easily generated at your front end app. which front end application are you using?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
adbasanta
Posting Yak Master
120 Posts |
Posted - 2012-08-12 : 19:35:15
|
Thank you visakh16!I'm using vb6 32 bit. At First I develop this Balance sheet in my front end app but it gets too complicated to handle the positioning of accounts and it gets time the app to generate the balance sheet. That is why Ive decided to generate the report in SQL. Will it be possible to use cube or rollup or CURSOR? Ive heard this powerful stuff of SQL Server but Ive never use this before. If this can be done ALSO in vb6 smoothly it would be greatly appreciated if there is some solution.Some suggest to use Crystal Report but its another big investment for the company while SQL Server or vb6 can do the task!Thank you & God bless!-------------------------------------------------------------------------------------------------------Learning MS SQL Server 2008 |
 |
|
adbasanta
Posting Yak Master
120 Posts |
Posted - 2012-08-13 : 21:32:22
|
Any body has an Idea on how to solve this?Thank you!-------------------------------------------------------------------------------------------------------Learning MS SQL Server 2008 |
 |
|
adbasanta
Posting Yak Master
120 Posts |
Posted - 2012-08-15 : 05:57:54
|
Can I use rollup or cube or grouping_id()? Where in my query to put it?Thank you!-------------------------------------------------------------------------------------------------------Learning MS SQL Server 2008 |
 |
|
|