select Sum from 2 tables

hi there
i have go get sum from 2 tables
in one table i have the total debt for each agreement this is the table :
SELECT [MID]
      ,[AID]
      ,[Pay_Date]
      ,[Pay_Sum]
      ,[Rem]
      ,[Is_Pay]
  FROM [Agreements_Moves]

Open in new window

i need the sum on Pay_Sum col for each AID

the second table that i need to run Sum on PaySum col  on the payment Table this is the table :
SELECT [PayRunId]
      ,[receipt_id]
      ,[MID]
      ,[AID]
      ,[PayDate]
      ,[PaySum]
    [dbo].[Pays]

Open in new window


i need to see what is the total from the first table then see what is the total from the second table then X-Y = balance
for each AID in the first table
thanks ....
Tech_MenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

John_VidmarCommented:
SELECT	[AID]	= ISNULL(a.[AID],b.[AID])
,	[diff]	= ISNULL(a.x,0) - ISNULL(b.y,0)
FROM	(	SELECT	[AID]
		,	x = SUM([Pay_Sum])
		FROM	[Agreements_Moves]
		GROUP
		BY	[AID]
	) a
FULL
JOIN
	(	SELECT	[AID]
		,	y = SUM([PaySum])
		FROM	[dbo].[Pays]
		GROUP
		BY	[AID]
	) b	ON	a.[AID] = b.[AID]

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tech_MenAuthor Commented:
hi john
thanks for your answer its working but i need to see more 2 col
the total from the first table and the total from the second table as well
0
Tech_MenAuthor Commented:
like that  :
x       y     total
100  20    80
400  100  300
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tech_MenAuthor Commented:
your total working very good but i must have does 2 columns
0
Tech_MenAuthor Commented:
maybe this will help to does 2 tables are main table as well to the Agreements_Moves has an Agreements table
and the Pays have a main table called Receipts
0
Tech_MenAuthor Commented:
thanks
0
John_VidmarCommented:
SELECT	[AID]	= ISNULL(a.[AID],b.[AID])
,	[x]	= ISNULL(a.x,0)
,	[y]	= ISNULL(b.y,0)
,	[total]	= ISNULL(a.x,0) - ISNULL(b.y,0)
FROM	(	SELECT	[AID]
		,	x = SUM([Pay_Sum])
		FROM	[Agreements_Moves]
		GROUP
		BY	[AID]
	) a
FULL
JOIN
	(	SELECT	[AID]
		,	y = SUM([PaySum])
		FROM	[dbo].[Pays]
		GROUP
		BY	[AID]
	) b	ON	a.[AID] = b.[AID]

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.