从许多记录中返回总和

时间:2016-09-27 09:35:00

标签: sql sqlite sum

我在SQLite

中有一张桌子
CREATE TABLE "INVENTORY" {
   "product" VARCHAR NOT NULL, 
   "version" VARCHAR NOT NULL,
   "quantity" INTEGER,
   //etc
}

和一些记录就像

"product" - "01" - 1000
"product" - "01" - 1000
"product" - "02" - 4000
"product" - "02" - 1000
"product" - "03" - 3000

我尝试进行查询,以便获得每种产品和版本的所有金额。在上面的示例中,查询将返回

"product" - "01" - 2000
"product" - "02" - 5000
"product" - "03" - 3000

我尝试了这个返回ambiguous column name: product

的查询
SELECT product , version, SUM(quantity) AS total 
from INVENTORY 
INNER JOIN INVENTORY
       ON product AND version
GROUP BY product, version

1 个答案:

答案 0 :(得分:2)

SELECT product, version, SUM(quantity) AS total 
FROM INVENTORY
GROUP BY product, version