希望有人能帮我理解如何解决我的问题。我在我的数据库表PAYMENTS中有这样的:
| PAYMENTS.PAYMENT | PAYMENTS.TOTAL |
| CASH |1000 |
| DEBT |500 |
| DEBTPAID |200 |
问题是:CASH
计算为来自销售额加DEBTPAID
的现金。所以我需要在报告中单独记录这些数据(CASH-DEBTPAID AS CASH
。
如上所述,我如何在iReport
中修改PAYMENTS.PAYMENT
。非常感谢您的支持!提前谢谢!
答案 0 :(得分:1)
此SQL语句将在单个记录中返回总CASH和总DEBT。
SELECT SUM(CASE WHEN PAYMENTS.PAYMENT IN ('CASH', 'DEBTPAID') THEN PAYMENTS.TOTAL ELSE 0 END)
AS CASH,
SUM(CASE WHEN PAYMENTS.PAYMENT = 'DEBT' THEN PAYMENTS.TOTAL ELSE 0 END) AS DEBT
FROM PAYMENTS
这会根据您提供给我们的数据为您提供正确的总数。
如果您想要单独记录,则必须以不同的方式完成(我可以告诉您)。
答案 1 :(得分:1)
这个SQL会为CASH和DEBT提供单独的记录:
SELECT CASE WHEN PAYMENTS.PAYMENT IN ('CASH', 'DEBTPAID') THEN 'CASH' ELSE PAYMENTS.PAYMENT END AS PAYMENT,
SUM(PAYMENTS.TOTAL) AS TOTAL
FROM PAYMENTS
GROUP BY CASE WHEN PAYMENTS.PAYMENT IN ('CASH', 'DEBTPAID') THEN 'CASH' ELSE PAYMENTS.PAYMENT END
答案 2 :(得分:1)
这应该这样做。
SELECT SUM(CASE WHEN PAYMENTS.PAYMENT= 'CASH' THEN PAYMENTS.TOTAL ELSE 0 END)
-
SUM(CASE WHEN PAYMENTS.PAYMENT= 'DEBTPAID' THEN PAYMENTS.TOTAL ELSE 0 END) AS CASH,
SUM(CASE WHEN PAYMENTS.PAYMENT = 'DEBT' THEN PAYMENTS.TOTAL ELSE 0 END) AS DEBT
FROM PAYMENTS
答案 3 :(得分:1)
对不起,我一直在四处走动。
如果您正在寻找:
| PAYMENTS.PAYMENT | PAYMENTS.TOTAL |
|现金| 800 |
| DEBT | 500 |
这将为您提供:
SELECT CASE WHEN PAYMENTS.PAYMENT IN ('CASH', 'DEBTPAID') THEN 'CASH' ELSE PAYMENTS.PAYMENT END PAYMENT,
SUM(CASE WHEN PAYMENTS.PAYMENT= 'DEBTPAID' THEN -PAYMENTS.TOTAL ELSE PAYMENTS.TOTAL END) TOTAL
FROM PAYMENTS
GROUP BY CASE WHEN PAYMENTS.PAYMENT IN ('CASH', 'DEBTPAID') THEN 'CASH' ELSE PAYMENTS.PAYMENT END