Hi all,
Could someone double check my SQL statement for proper operation and general commonsense approach?
Here's what's going on: I have a parent and child table with a one-to-many relationship, joined on a column named AccountNumberKey. The child table has numeric columns in it I need to sum up.
The data is such that all child records with a given AccountNumberKey value will always have the same values in their two numeric columns. I want to join these tables and for each AccountNumberKey put the sum of those two columns into a temp table. I only need to sum the columns from a single child record given each AccountNumberKey.
Some sample data below will (I hope) make this clearer:
Parent Table Columns
ParentID InstitutionID AccountNumberKey
1 LocalHost 1873283
2 Acme Brokers 3627389
3 Dewey, Cheatem 1392876
4 NCC1701 8837273
5 Peyton Place 9981273
Child Table Columns
ChildID AccountNumberKey Value1 Value2 ProposalNumber
1 1873283 1000 100 58
2 1873283 1000 100 59
3 1873283 1000 100 60
4 1873283 1000 100 61
Here's my SQL Statement:
SELECT DISTINCT Parent.InstitutionID, AccountNumberKey, SUM(Child.Value1 + Child.Value2) as total
INTO #TempTable
FROM Parent
INNER JOIN
Child ON Parent.AccountNumberKey = Child.AccountNumberKey
GROUP BY Parent.InstitutionID, Parent.AccountNumberKey, Child.ProposalNumber
The goal is to link the tables and put data into a temp table so it looks like so:
TempTable columns
InstitutionID AccountNumberKey Total
LocalHost 1873283 1100
Does my SQL query pass muster? I'm no genius when it comes to groupings and wondered if this is A) correct and B) an ok way to go or if there are better joins to try.
Thanks!