| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2004-02-11 : 07:32:11
|
| Rolly writes "Dear SQLTeam,Hope you can help me, I have 3 tables....Customer Table==============CustNo CustName------ --------1 Rolly2 Steven3 Philip4 JessieLedger Table============CustNo Document EntryNo------ -------- -------1 A 11 A 22 B 32 B 42 C 53 D 63 D 74 E 8Amount Table============EntryNo Amount------- -------- 1 202 303 404 105 206 507 108 20I want my results to appear as :-CustNo CustName Sum_Amount------ -------- ----------1 Rolly 502 Steven 703 Philip 604 Jessie 20How can I do this? I can retrieve the CustNo and Sum_Amount, but when I add in the CustName column, SQL Server returns me the error "Column 'CustName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.". Can I get around this without adding in the CustName into GROUP BY? Thanks!Rolly" |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2004-02-11 : 07:34:11
|
| You can't get around it, and you don't want to anyway:SELECT C.CustNo, C.CustName, Sum(A.Amount) AS Sum_AmountFROM Customer C INNER JOIN Ledger L ON C.CustNo=L.CustNoINNER JOIN Amount A ON L.EntryNo=A.EntryNoGROUP BY C.CustNo, C.CustNameORDER BY C.CustNo |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-02-11 : 08:06:00
|
| actually, to be honest I prefer doing what you originally intended. i don't like grouping by non-PK columns in tables if you can avoid it.so, here's how I usually write these:SELECT C.CustNo, C.CustName, TotalFROMCustomer CINNER JOIN(SELECT L.CustNo, Sum(A.Amount) as TotalFROM Ledger L INNER JOIN Amount A ON L.EntryNo=A.EntryNoGROUP BY L.CustNo) BON C.CustNo = B.CustNoORDER BY C.CustNolots of times, if people want to return the entire Customer table's columns, i'll see 20 group by's and that's usually a red flag... you aren't aggregating the customer table, but rather the others, so if you use a dervied query and group it by CustNo, you are all set. It logically makes your query a little more clear, and can often perform better as well.the less columns you group by, the less SQL Server has to do to check for duplicates to perform the groupings.- Jeff |
 |
|
|
HendersonToo
Starting Member
20 Posts |
Posted - 2004-02-11 : 18:20:24
|
"Dr Cross Join:" =)I would lean towards the following expression:SELECT C.CustNo, C.CustName, ( SELECT SUM(A.Amount) as Total FROM Ledger L INNER JOIN Amount A ON L.EntryNo = A.EntryNo WHERE L.CustNo = C.CustNo) AS TOTALFROM Customer CORDER BY C.CustNo Now to me the query plans look like they end up at the same performance, but what's your thoughts on the difference between these two? Granted your solution scales to use more than one aggregrate... Are there any other benefits? |
 |
|
|
byrmol
Shed Building SQL Farmer
1591 Posts |
Posted - 2004-02-11 : 18:58:08
|
| I like Rob's better, because AFAIK his would be the only expression that confirms to all the rules required for an indexed view...DavidM"SQL-3 is an abomination.." |
 |
|
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2004-02-11 : 23:20:28
|
| What's the relationship between tables?Ledger tablecustono foreign key references customer(custno)entryno foreign key references amount (entryno)Am I right? |
 |
|
|
smousumi
Starting Member
19 Posts |
Posted - 2004-03-10 : 07:44:47
|
| Hii Rolly,Even i would also suggest u the same as ...select C.CustNo,C.CustName,(Select sum(Amount) from Amount Ainner join ledger Lon A.EntryNo=L.EntryNowhere C.CustNo=L.CustNogroup by CustNo) from Customer Cgroup by C.CustNo,C.CustNameorder by C.CustNou can use group by for CustNo as well CustName..mousumi |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-03-10 : 07:51:35
|
quote: Even i would also suggest u the same as ...select C.CustNo,C.CustName,(Select sum(Amount) from Amount Ainner join ledger Lon A.EntryNo=L.EntryNowhere C.CustNo=L.CustNogroup by CustNo) from Customer Cgroup by C.CustNo,C.CustNameorder by C.CustNou can use group by for CustNo as well CustName..
??there's no need for the outer GROUP BY.- Jeff |
 |
|
|
drymchaser
Aged Yak Warrior
552 Posts |
Posted - 2004-03-10 : 09:13:50
|
quote: Originally posted by jsmith8858
quote: Even i would also suggest u the same as ...select C.CustNo,C.CustName,(Select sum(Amount) from Amount Ainner join ledger Lon A.EntryNo=L.EntryNowhere C.CustNo=L.CustNogroup by CustNo) from Customer Cgroup by C.CustNo,C.CustNameorder by C.CustNou can use group by for CustNo as well CustName..
??there's no need for the outer GROUP BY.- Jeff
...or the inner. |
 |
|
|
|