2017-03-24 193 views
0

這裏我將cr.credit值作爲相同值的倍數。但我需要確切的價值來計算總價值。任何人都可以幫助我得到答案。在sql中總結多個列總計

SELECT 
    gm.id, 
    gm.groupid, 
    gm.tktno, 
    gm.memberid, 
    (COALESCE((g.emdue - ae.dividend), 0)) AS a, 
    SUM(ra.debit) AS deb, 
    SUM(DISTINCT ra.credit) AS cr, 
    COALESCE((SUM(ra.debit) - SUM(ra.credit)), 0) AS b, 
    COALESCE(SUM(cr.credit_value), 0) AS c, 
    COALESCE((COALESCE((g.emdue - ae.dividend), 0) - COALESCE((SUM(ra.debit) - SUM(ra.credit)), 0) 
    - COALESCE(SUM(cr.credit_value), 0)), 0) AS total 
FROM groupmember gm 
LEFT JOIN runningaccount ra 
    ON ra.group_id = gm.groupid 
    AND ra.ticket_no = gm.tktno 
    AND gm.memberid = ra.member_id 
    AND gm.id = ra.group_member_id 
LEFT JOIN chitreceipt cr 
    ON gm.groupid = cr.groupid 
    AND gm.memberid = cr.memberid 
    AND gm.tktno = cr.tktno 
LEFT JOIN groupnew g 
    ON g.id = gm.groupid 
LEFT JOIN auctionentry ae 
    ON ae.groupid = gm.groupid 
GROUP BY gm.memberid, 
     gm.groupid, 
     gm.tktno 
+0

這是mySql .. –

回答

0

你可以做不同的列,然後總和,所以你可以得到不同的值總和。像

sum(distinct COALESCE(cr.credit_value,0)) 

無論你使用它,就像上面一樣使用它。

+0

謝謝,我得到了我的輸出。 –

+0

'nvl()'???問題被標記爲'MySQL'並且沒有'nvl()'。你確實是指'IsNull()'還是'COALESCE()'? – Rahul

+0

對不起,我編輯了它,我的意思是說合並 – Rams