我有一张桌子:
id, cust#, payment, date
1, cus1, 508, 2010-01-01
2, cus2, 40, 2010-01-01
3, cus1, 13, 2010-01-02
我有我的查询。
select id, cust#, payment, date group by cust#
正如预期的那样,它会返回结果。我需要它来返回每个列值以及sum(payment) as totalpayments
我无法弄明白,因为这只会返回付款总额而不是每笔付款金额加上总金额
select id, cust#, sum(payment), date group by cust#
这就是我想要的,即使这不是一个有效的查询
select id, cust#, payment, sum(payment) AS totalpayments, date group by cust#
答案 0 :(得分:0)
您必须使用子查询:
select
id,
cust#,
(select
sum(payment)
from
mytable
group by
cust#
) as totalpayments,
date
from
mytable