3

我有两个表(输入和类别):

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)
);

我需要从每个类别的输入表中获取总和(dValue 列)。即使总和结果为零。

如果我能得到每个父类别的总和会更好(何时categories.iId = categories.iParent,将子类别的结果与父类别相加)

谁能帮我?我很感激任何帮助!谢谢!

4

1 回答 1

3

尝试这个:

select c.iParent, sum(i.dValue)
from categories c
left outer join inputs i on i.iCategory=c.iId
group by c.iParent

编辑:使用帐户:

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
于 2012-06-04T14:13:18.097 回答