1

以下查询返回所有具有贷方余额(列 ID 和总计)的客户 - 有没有办法只返回总计的总和?

SELECT Customer_ID AS 'ID',
  (
    SELECT SUM(Credit) - SUM(Debit)
    FROM A_Sales_Ledger
    WHERE Document_Date <= '2013-09-30'
      AND Customer_ID = ID
  ) AS 'Total'
FROM A_Sales_Ledger
WHERE Total > 0
GROUP BY Customer_ID
4

1 回答 1

1

尝试这样的事情:

SELECT SUM(a.total)
FROM (
  SELECT (SUM(Credit) - SUM(Debit)) AS "total"
  FROM A_Sales_Ledger
  WHERE Document_Date <= '2013-09-30'
  GROUP BY Customer_ID
  HAVING SUM(Credit) - SUM(Debit) > 0
  ) a;

sqlfiddle demo

于 2013-10-20T16:05:29.343 回答