我有桌子
name quentity
pramo 1000
ladkat 2000
我希望选择输出为总和的新行
name quentity
pramod 1000
ladkat 2000
total 3000
答案 0 :(得分:1)
尝试使用UNION:
(SELECT *
FROM your_table)
UNION
(SELECT sum(quentity) as "total"
FROM your_table)
答案 1 :(得分:1)
SQL Server,你可以试试这个:
SELECT name,quentity
FROM [your-table]
UNION
SELECT 'Total',SUM(quentity)
FROM [your-table]
答案 2 :(得分:1)
使用Union Operator,就像这样
select * from test.item
union
select "Total", sum(qty) from test.item;
答案 3 :(得分:0)
大多数数据库都支持某些版本的rollup
或grouping sets
。例如:
select coalesce(name, 'Total') as name, sum(quantity) as quantity
from t
group by name with rollup;
或:
select coalesce(name, 'Total') as name, sum(quantity) as quantity
from t
group by grouping sets ((name), ()) ;
请注意,它们使用聚合查询,因此您需要group by
子句。