在这里,我得到的cr.credit值是相同值的倍数。但是我需要确切的值来计算总价值。任何人都可以帮我得到答案。

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

最佳答案

您可以对该列做不同的值,然后求和,这样您就可以得出不同的值求和。喜欢

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


无论您在哪里使用它,都像上面一样使用它。

关于mysql - 组合sql中的多个列求和,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42993968/

10-10 06:38