Q
Problem solve Get help with specific problems with your technologies, process and projects.

Adding and subtracting in inner joins

I have two tables:

Allowance  [Account ID] key
Claims     [Account ID], [Claim #] key

I am querying the two via a inner join. I am using account ID to complete the join.The allowance table stores the alowance dollar amount [allowance amount] allocated for the account. There is one record per account. The claim table has the [dollar transactions] that has posted to the account. There are multiple transactions [dollar transactions] in the second table in relationship to the first [account].

How do I sum the [dollar transaction] by [Account Id] in the claims table? And display the difference (varaice) between the [dollar transaction] in the second table table and [account amount] in the fisrt table?


Here you go:

SELECT Allowance.AccountID,
       MAX(Allowance.AllowanceAmount) as AllowanceAmount,
       SUM(Claims.Amount) as ClaimsTotal,
       MAX(Allowance.AllowanceAmount) ?
         SUM(Claims.Amount) as Variance
  FROM Allowance, Claims
  WHERE Allowance.AccountID = Claims.AccountID
  GROUP BY Allowance.AccountID

For More Information

  • What do you think about this answer? E-mail the Edtior at tdichiara@techtarget.com with your feedback.
  • The Best SQL Web Links: tips, tutorials, scripts, and more.
  • Have a SQL tip to offer your fellow DBA's and developers? The best tips submitted will receive a cool prize--submit your tip today!
  • Ask your technical SQL questions--or help out your peers by answering them--in our live discussion forums.
  • Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.

Dig Deeper on Oracle and SQL

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide.com

SearchDataCenter

SearchContentManagement

SearchHRSoftware

Close