Solved

Query help...

Posted on 2013-01-24
7
358 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
  • 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 65

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 65

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Help 27 46
SQL Syntax: How to force case sensitive query? 2 32
SQL server vNext 18 31
delete the first occurence of a duplicate row in sql 5 26
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

831 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