I am looking for an SQL statement for grouping items into a sum within a single query. I work for a financial institution so I need general ledger queries. I am making a balance sheet. The first item, for example is unsecured loans. These are made up of the sum of several records in the table.
Unsecured loans might be written like this:
Select Description, sum(bal) as Amount
From GL_Table
Where AccountNum IN (1,2,5,10,15);
The result would be:
Description Amount
------------ ---------
Unsecured 1258100.50
Of course, I have about 20 entries on the balance sheet that each need to be calculated like this. Is there a way I can do this without running 20 queries or without creating 20 views and querying the views with 1 query.
I was thinking a statement like this should work:
Select Description, sum(select bal from GL_Table where AccountNum IN (1,2,5,10,15) as UnsecuredLoans,sum(select bal from GL_table where AccountNum In (3,6,8,9) AS AutoLoans)...and so on so that the output would be
Description Amount
------------ ----------
Unsecured 1258100.50
Auto 632500.71
Other 3242908.50
I haven't been able to make the above work. The problem is that each Description (GL entry) is made up of the sum of 10 or more accounts.
Can anyone help? Also, keep in mind I am running these queries off an 8-year-old Digital Alpha 4200. It's very slow. Soon we will have a new alpha, but until then....
Joe
Unsecured loans might be written like this:
Select Description, sum(bal) as Amount
From GL_Table
Where AccountNum IN (1,2,5,10,15);
The result would be:
Description Amount
------------ ---------
Unsecured 1258100.50
Of course, I have about 20 entries on the balance sheet that each need to be calculated like this. Is there a way I can do this without running 20 queries or without creating 20 views and querying the views with 1 query.
I was thinking a statement like this should work:
Select Description, sum(select bal from GL_Table where AccountNum IN (1,2,5,10,15) as UnsecuredLoans,sum(select bal from GL_table where AccountNum In (3,6,8,9) AS AutoLoans)...and so on so that the output would be
Description Amount
------------ ----------
Unsecured 1258100.50
Auto 632500.71
Other 3242908.50
I haven't been able to make the above work. The problem is that each Description (GL entry) is made up of the sum of 10 or more accounts.
Can anyone help? Also, keep in mind I am running these queries off an 8-year-old Digital Alpha 4200. It's very slow. Soon we will have a new alpha, but until then....
Joe