像这样?
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>