![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
[...] I want for each Account, the sum of all amounts. I used a simple query: * * * * *SELECT * * * * *Accounts.name , * * * * *sum(Logs.amount) AS balance * * * * *FROM Accounts, Logs * * * * *WHERE Accounts.id=Logs.account * * * * *GROUP BY Accounts.id But that does not do exactly do what I want because it does not include the Accounts that are never referenced in the Log table. ( I would like them to come out with either a 0 or NULL sum) |
#3
| |||
| |||
|
|
Replying to myself... On Mar 1, 4:08*pm, Fabrice <fabrice.gaut... (AT) gmail (DOT) com> wrote: [...] I want for each Account, the sum of all amounts. I used a simple query: * * * * *SELECT * * * * *Accounts.name , * * * * *sum(Logs.amount) AS balance * * * * *FROM Accounts, Logs * * * * *WHERE Accounts.id=Logs.account * * * * *GROUP BY Accounts.id But that does not do exactly do what I want because it does not include the Accounts that are never referenced in the Log table. ( I would like them to come out with either a 0 or NULL sum) This query: SELECT Accounts.id, Accounts.name, sum(Logs.amount) *FROM Accounts LEFT OUTER JOIN Logs ON Accounts.id=Logs.account GROUP BY id; is better in that it gives me a NULL sum. Now if I could get a 0 instead of NULL that would be perfect. I figured out how to use UNION to add a row with a 0 amount for each account, but I could not find a way to do the aggregate function on the results of the UNION... |
#4
| |||
| |||
|
|
On Mar 1, 8:29 pm, Fabrice <fabrice.gaut... (AT) gmail (DOT) com> wrote: Replying to myself... On Mar 1, 4:08 pm, Fabrice <fabrice.gaut... (AT) gmail (DOT) com> wrote: [...] I want for each Account, the sum of all amounts. I used a simple query: SELECT Accounts.name , sum(Logs.amount) AS balance FROM Accounts, Logs WHERE Accounts.id=Logs.account GROUP BY Accounts.id But that does not do exactly do what I want because it does not include the Accounts that are never referenced in the Log table. ( I would like them to come out with either a 0 or NULL sum) This query: SELECT Accounts.id, Accounts.name, sum(Logs.amount) FROM Accounts LEFT OUTER JOIN Logs ON Accounts.id=Logs.account GROUP BY id; is better in that it gives me a NULL sum. Now if I could get a 0 instead of NULL that would be perfect. I figured out how to use UNION to add a row with a 0 amount for each account, but I could not find a way to do the aggregate function on the results of the UNION... In Oracle you would use the NVL() function. SELECT Accounts.id, Accounts.name, sum(NVL(Logs.amount,0)) FROM Accounts LEFT OUTER JOIN Logs ON Accounts.id=Logs.account GROUP BY id; But since you didn't bother to mention what DBMS you use, that may be the wrong answer. |
#5
| |||
| |||
|
|
On 02.03.2009 03:02, Ed Prochak wrote: On Mar 1, 8:29 pm, Fabrice <fabrice.gaut... (AT) gmail (DOT) com> wrote: In Oracle you would use the NVL() function. SELECT Accounts.id, Accounts.name, sum(NVL(Logs.amount,0)) FROM Accounts LEFT OUTER JOIN Logs ON Accounts.id=Logs.account GROUP BY id; But since you didn't bother to mention what DBMS you use, that may be the wrong answer. For other RDBMS use CASE. robert |
#6
| |||
| |||
|
|
On 02.03.2009 03:02, Ed Prochak wrote: On Mar 1, 8:29 pm, Fabrice <fabrice.gaut... (AT) gmail (DOT) com> wrote: Replying to myself... On Mar 1, 4:08 pm, Fabrice <fabrice.gaut... (AT) gmail (DOT) com> wrote: [...] I want for each Account, the sum of all amounts. I used a simple query: * * * * *SELECT * * * * *Accounts.name , * * * * *sum(Logs.amount) AS balance * * * * *FROM Accounts, Logs * * * * *WHERE Accounts.id=Logs.account * * * * *GROUP BY Accounts.id But that does not do exactly do what I want because it does not include the Accounts that are never referenced in the Log table. ( I would like them to come out with either a 0 or NULL sum) This query: SELECT Accounts.id, Accounts.name, sum(Logs.amount) *FROM Accounts LEFT OUTER JOIN Logs ON Accounts.id=Logs.account GROUP BY id; is better in that it gives me a NULL sum. Now if I could get a 0 instead of NULL that would be perfect. I figured out how to use UNION to add a row with a 0 amount for each account, but I could not find a way to do the aggregate function on the results of the UNION... In Oracle you would use the NVL() function. SELECT Accounts.id, Accounts.name, sum(NVL(Logs.amount,0)) *FROM Accounts LEFT OUTER JOIN Logs ON Accounts.id=Logs.account GROUP BY id; But since you didn't bother to mention what DBMS you use, that may be the wrong answer. For other RDBMS use CASE. * * * * robert |
#7
| |||
| |||
|
|
On Mar 2, 3:13 am, Robert Klemme <shortcut... (AT) googlemail (DOT) com> wrote: On 02.03.2009 03:02, Ed Prochak wrote: In Oracle you would use the NVL() function. SELECT Accounts.id, Accounts.name, sum(NVL(Logs.amount,0)) FROM Accounts LEFT OUTER JOIN Logs ON Accounts.id=Logs.account GROUP BY id; But since you didn't bother to mention what DBMS you use, that may be the wrong answer. For other RDBMS use CASE. Oracle supports CASE. |
|
I was just trying, indirectly, to emphasize that the OP never mentioned what DBMS he uses. |
|
I guess the ESP reference went right over Fabrice's head. A lot of the questioners fail to mention which DBMS product they use. As much as SQL is standard, not all products support the standard at the same level. CASE might not be supported by his DBMS either. |
![]() |
| Thread Tools | |
| Display Modes | |
| |