Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to get the final sum totals of of debit and credit of this trialbalance. When I execute this in sql the totals always show in a different gird but i want them as one stored procedure.

Thanks in advance



SQL
CREATE PROCEDURE [dbo].[TrialBalance]

AS

SELECT COA.Account_Code AS [Account Code],COA.Account_Name 
AS [Account Name],CAST(SUM(debit-credit) AS varchar) 
AS [$ Debit],'' AS [$ Credit] FROM COA JOIN CashBook 
ON COA.Account_Name = CashBook.Account_Name 
WHERE COA.Account_Name_Cate IN ('Assets') 
GROUP BY COA.Account_Code, COA.Account_Name

UNION ALL

SELECT COA.Account_Code AS [Account Code],COA.Account_Name 
AS [Account Name],CAST(SUM(debit-credit) AS varchar) 
AS [$ Debit],'' AS [$ Credit] FROM COA JOIN AccountReceivable_LDG 
ON COA.Account_Name = AccountReceivable_LDG.Account_Name 
WHERE COA.Account_Name_Cate IN ('Assets') 
GROUP BY COA.Account_Code, COA.Account_Name

UNION ALL 

SELECT COA.Account_Code 
AS [Account Code],COA.Account_Name 
AS [Account Name],'',CAST(SUM(debit-credit) AS varchar)
FROM COA JOIN AccountPayable_LDG ON COA.Account_Name = AccountPayable_LDG.Account_Name 
WHERE COA.Account_Name_Cate IN ('Liabilities') 
GROUP BY COA.Account_Code, COA.Account_Name



SELECT 'Totals :' AS [-],':' AS [-],CAST(SUM(Debit-Credit) AS varchar) 
AS [-] FROM ( SELECT  Debit, Credit  
FROM   COA JOIN CashBook ON COA.Account_Name = CashBook.Account_Name 
WHERE   COA.Account_Name_Cate IN ('Assets') 

UNION ALL

SELECT  Debit, Credit  
FROM   COA JOIN AccountReceivable_LDG ON COA.Account_Name = AccountReceivable_LDG.Account_Name 
WHERE   COA.Account_Name_Cate IN ('Assets'))s


SELECT 'Totals :' AS [-],':' AS [-],CAST(SUM(Debit-Credit) AS varchar) AS [-] 
FROM ( SELECT  Debit, Credit FROM   COA JOIN AccountPayable_LDG 
ON COA.Account_Name = AccountPayable_LDG.Account_Name 
WHERE   COA.Account_Name_Cate IN ('Liabilities'))s


[edit]Code block added[/edit]
Posted
Updated 12-Jun-13 9:40am
v4
Comments
joshrduncan2012 12-Jun-13 15:14pm    
What is your question?
Maciej Los 12-Jun-13 17:04pm    
Why do you use CAST function to convert numeric value as text?
What are you trying to achive?
What are example data?
Please, use "Improve question" widget to update question.

1 solution

In order for all of the output to be in a single grid, you will need to union all of the selects , including the 'total' ones.

Currently you don't union the detail and the totals
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900