是否這樣?
SELECT
sub_category
,sum(amount)
FROM
the_table
GROUP BY
sub_category
WHERE
category = 'Children';
或
SELECT
category
,sub_category
,sum(amount)
FROM
the_table
GROUP BY
category
,sub_category
測試/演示了使用SQLite:
$ sqlite3 test.sqlite
SQLite version 3.7.15.2 2013-01-09 11:53:05
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table t (category varchar, subcategory varchar, amount int);
sqlite> insert into t values ('children', 'cloth', 200), ('children', 'cloth', 500), ('transport', 'fuel', 300), ('children', 'school', 500);
sqlite> select * from t ;
children|cloth|200
children|cloth|500
transport|fuel|300
children|school|500
sqlite>
sqlite> select category, subcategory, sum(amount) from t group by category, subcategory;
children|cloth|700
children|school|500
transport|fuel|300
sqlite>
來源
2013-07-13 03:16:58
dsh
我希望每個的總和特定類別下的子類別,如兒童服裝的總額和兒童學校的總額。按照桌子你可以站在下面。感謝您的回覆 –
第二個查詢是您想要的。 「 – Bohemian