2011-06-09 101 views
0

列valuses的總和()我有一個表:返回列valuses在一個MySQL查詢

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

你必須使用一個子查詢:

select 
    id, 
    cust#, 
    (select 
     sum(payment) 
    from 
     mytable 
    group by 
     cust# 
    ) as totalpayments, 
    date 
from 
    mytable