如何选择总和(列)的所有列行

时间:2018-03-25 06:07:32

标签: sql sum

select p_id,paid_amount,date, from payments

这是我的查询,它工作正常,但当我添加总和列时,它只选择第一行。

select p_id,paid_amount,date,sum(paid_amount) as 'sum' from payments

2 个答案:

答案 0 :(得分:1)

Most DBMSes support Windowed Aggregates, then it's a simple

select p_id,paid_amount,date,
  sum(paid_amount) over () as "sum" 
from payments

Otherwise you can use a Scalar Subquery:

select p_id,paid_amount,date,
  (select sum(paid_amount) from payments) as "sum"
from payments

答案 1 :(得分:0)

进行两次查询。

SELECT p_id,paid_amount,date, FROM payments;
SELECT SUM(paid_amount) FROM payments;