Solved

Query help...

Posted on 2013-01-24
7
362 Views
Last Modified: 2013-01-24
Let's say I have:

Select BankAccount, LastName from Accounts

and it lists all accounts and the account holders last name... one record per.

Now I want to see their total deposits... ever.

Deposits are in a AccountsTransactions table that has two fields

BankAccount and DepositAmount.

Select BankAccount, LastName, DepositAmount from Accounts, AccountsTransactions

will show a record for each deposit made...

Select BankAccount, LastName, Sum(DepositAmount) from Accounts, AccountsTransactions

will return one record with a grand total of all deposits made.

How do I accomplish this?
0
Comment
Question by:classnet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 38816431
Try this:

Select a.BankAccount, a.LastName, SUM(t.DepositAmount) as TotDeposits 
from Accounts a INNER JOIN AccountsTransactions t ON a.BankAccount = t.BankAccount
GROUP BY  a.BankAccount, a.LastName

Open in new window

0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 38816436
If you're getting at all deposits for each person, then ...

SELECT a.BankAccount, a.LastName, Sum(at.DepositAmount)
FROM Accounts a
    JOIN AccountsTransactions at ON a.BankAccount = at.BankAccount
GROUP BY Select a.BankAccount, a.LastName

>will return one record with a grand total of all deposits made.
If you're getting at all deposits for all BankAccounts, then it would just be..

SELECT Sum(DepositAmount)
FROM AccountsTransactions
0
 

Author Comment

by:classnet
ID: 38816446
So, if you "select" 15 fields and sum one field can I assume that you have to "group by" the other 14?

What if you select a, b, (c - d) + (e * f) as g, sum(h)

how do you "group by"?  

Group by a, b, (c - d) + (e * f) or
Group by a, b, g
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 61

Expert Comment

by:mbizup
ID: 38816460
You would group by the actual expressions:

Group by a, b, (c - d) + (e * f)
0
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 250 total points
ID: 38816464
>So, if you "select" 15 fields and sum one field can I assume that you have to "group by" the other 14?
Correct.  Whenever you apply an aggregate such as SUM(), COUNT(), MIN(), MAX(), you have to GROUP BY all other columns.  

There is an exception for if it's a hard-coded column such as '1' as some_code, and calculations such as the cdef one you posted.
0
 
LVL 61

Accepted Solution

by:
mbizup earned 250 total points
ID: 38816505
If you have a lot of additional fields in your accounts table that you want to include, you might also be able to use a subquery like this:


SELECT a.*, q.TotDeposit
FROM Accounts 
INNER JOIN
(SELECT BankAccount, SUM(DepositAmount) as TotDeposit
FROM AccountsTransactions
GROUP BY BankAccount) q ON a.BankAccount = q.BankAccount

Open in new window

0
 

Author Closing Comment

by:classnet
ID: 38816553
Thank you very much!  This will help others I'm sure.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question