Reputation: 305
I have two tables (inputs and categories):
CREATE TABLE categories (
iId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
sName TEXT NOT NULL,
iParent INTEGER,
FOREIGN KEY (iParent) REFERENCES categories(iId)
);
CREATE TABLE inputs (
iId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
iCategory INTEGER,
dValue DOUBLE NOT NULL,
FOREIGN KEY (iCategory) REFERENCES categories(iId)
);
I need get the sums(dValue column) from inputs table for each category. Even if the sum result is zero.
If is it possible would be better if I could get the sum for each Parent Category ( when categories.iId = categories.iParent
, summing the results of the child categories to the parent category )
Can anyone help me? I appreciate any help! Thanks!
Upvotes: 3
Views: 1531
Reputation: 727027
Try this:
select c.iParent, sum(i.dValue)
from categories c
left outer join inputs i on i.iCategory=c.iId
group by c.iParent
EDIT : With accounts:
select c.iParent, a.iCurrency, sum(i.dValue)
from categories c
left outer join inputs i on i.iCategory=c.iId
left outer join accounts a on i.iAccount=a.iId
group by c.iParent,a.iCurrency
Upvotes: 3