Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I do not know how to sum one of the column (TOTAL) in temp table

SQL
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[usp_Customer_Due_Until_Now]
 @Code nvarchar(30)
AS
BEGIN

SET NOCOUNT ON;
select code as Code, Name as Name, sum(Nett) SINETT into #tableSI  From SIHead Where Code=@code Group By Code,Name
select code as Code, Name as Name, sum(-Nett) SINETT into #tableSR  From SRHead Where Code=@code Group By Code,Name
select code as Code, Name as Name, sum(-ChqAmount) SINETT into #tableRC  From RCHead Where Code=@code Group By Code,Name
select code as Code, Name as Name, sum(Nett) SINETT  into #tableDN  From DNHead  Where Code=@code Group By Code,Name
select code as Code, Name as Name, sum(-Nett) SINETT  into #tableCN  From CNHead Where Code=@code Group By Code,Name

select code,name,SUM(SINETT) AS TOTAL from #tableSI GROUP BY CODE,NAME
UNION
select code,name,SUM(SINETT) AS TOTAL from #tableSR GROUP BY CODE,NAME
UNION
select code,name,SUM(SINETT) AS TOTAL from #tableRC GROUP BY CODE,NAME
UNION
select code,name,SUM(SINETT) AS TOTAL from #tableDN GROUP BY CODE,NAME
UNION
select code,name,SUM(SINETT) AS TOTAL from #tableCN GROUP BY CODE,NAME
drop table #tableSI
drop table #tableSr
drop table #tablerc
drop table #tableDN
drop table #tableCN
END


Current Result is
SQL
CODE    NAME                     TOTAL    

DT001 	DAKNA TRADING SDN BHD	-96545.61
DT001 	DAKNA TRADING SDN BHD	-1590
DT001 	DAKNA TRADING SDN BHD	-689
DT001 	DAKNA TRADING SDN BHD	1060
DT001 	DAKNA TRADING SDN BHD	94263.21


pls advice me how to sum total in single line

Pls help me.

Maideen
Posted

Try this ...

SQL
select t1.code,t1.name,(SUM(t1.SINETT)+SUM(t2.SINETT)+SUM(t3.SINETT)+SUM(t4.SINETT)SUM(t5.SINETT)) AS TOTAL from #tableSI t1 
inner join #tableSR t2 on t1.code=t2.code
inner join #tableRC t3 on t1.code=t3.code
inner join #tableDN t4 on t1.code=t4.code
inner join #tableCN  t5 on t1.code=t5.code
 
Share this answer
 
Comments
Maideen Abdul Kader 17-Dec-14 5:52am    
thanks

both KM Perumal and Tomas takac code is working fine

Thank you very much both

madieen
KM Perumal 17-Dec-14 6:54am    
Thanks
Maideen Abdul Kader 17-Dec-14 6:55am    
Hi KM Perumal.
I am using your code. Thanks working fine. But If any table does not have data, the result not appear
Example if #tableRc does not have data for the particular client, the result shows nothing.
Pls help me in this issue
Maideen
You can wrap your results in a CTE and then use it to sum the totals like this:
SQL
with results as
(
  select code,name,SUM(SINETT) AS TOTAL from #tableSI GROUP BY CODE,NAME
  UNION
  select code,name,SUM(SINETT) AS TOTAL from #tableSR GROUP BY CODE,NAME
  UNION
  select code,name,SUM(SINETT) AS TOTAL from #tableRC GROUP BY CODE,NAME
  UNION
  select code,name,SUM(SINETT) AS TOTAL from #tableDN GROUP BY CODE,NAME
  UNION
  select code,name,SUM(SINETT) AS TOTAL from #tableCN GROUP BY CODE,NAME
)
select * from results
union all
select '' as code, 'grand total' as name, sum(total) as total from results

This will give you this result:
code  name                  total
----- --------------------- ---------------------------------------
DT001 DAKNA TRADING SDN BHD -96545.61
DT001 DAKNA TRADING SDN BHD -1590.00
DT001 DAKNA TRADING SDN BHD -689.00
DT001 DAKNA TRADING SDN BHD 1060.00
DT001 DAKNA TRADING SDN BHD 94263.21
      grand total           -3501.40
 
Share this answer
 
Comments
Maideen Abdul Kader 17-Dec-14 5:51am    
thanks

both KM Perumal and Tomas takac code is working fine

Thank you very much both

madieen
Tomas Takac 17-Dec-14 7:24am    
Why the down vote? Could the down voter please explaint the reason?

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