我的表列是这样的,我希望不使用UNION的情况下最后一行中所有记录的总和
$2.stringValue
预期结果将是
no_of_books
1
3
3
3
5
我已经尝试过这种方法,但是在PostgreSQL之类的Oracle中寻找更好的功能
no_of_books
1
3
3
3
5
15
OR
select aa.no_of_books sum_prod from (SELECT prodcount
FROM "table1") aa
union all
select sum(bb.sum_prod) from (
select aa.no_of_books sum_prod from (SELECT prodcount
FROM "table1") aa ) bb
答案 0 :(得分:0)
您可以将rollup
与子查询一起使用:
select coalesce(no_of_books, (select sum(no_of_books) from table t))
from table t
group by rollup(no_of_books);
但是,这将直接通过sum()
完成:
select sum(no_of_books)
from table t
group by rollup(no_of_books);
答案 1 :(得分:0)
使用rollup
:
select sum(no_of_books)
from books t
group by rollup(no_of_books)
或cube
select sum(no_of_books)
from books t
group by cube(no_of_books);