gpt4 book ai didi

SQLITE:显示每个类别的总计(总和)

转载 作者:搜寻专家 更新时间:2023-10-30 19:46:24 29 4
gpt4 key购买 nike

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

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 时,将子类别的结果加到父类别中)是否可能会更好

谁能帮帮我?感谢您的帮助!谢谢!

最佳答案

试试这个:

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

关于SQLITE:显示每个类别的总计(总和),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10882691/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com