我有一个表格付款,
paymentid stuid stu_name fullamount pay_comp
1 1234 Jhon 2600 Feb
2 1212 Silva 1300 Jan
3 1234 Jhon 1300 March
4 1212 Silva 3900 April
5 3333 Perera 15600 Dec
在这里学生可以支付全年,几个月或一个月的费用,支付费用是1300, 例如,如果“席尔瓦”第一次支付1300,他将支付1月份的费用。 如果他再做1300次付款,那么它将被标记为下个月,即“2月”。如果有人完成15600付款,它将被保存为“Dec”。 问题是,在计算Jan的总金额时,必须从Dec付款中考虑1300.
我想要的是计算每个月的总金额。问题是“Pay_Com”栏是如上所述的插入记录。它可以是整年或单个月。有什么方法可以解决这个问题。
我尝试了不同的方法,我可以使用全额除以1300来计算每个月的全额。任何人都可以建议我使用灵魂或sql函数。 甚至尝试了IF和Sum的案例..
SELECT
CASE
WHEN round(sum(full_amount)/1300) = 1 THEN 1
ELSE 0
END AS 'Jan',
CASE
WHEN round(sum(full_amount)/1300) = 2 THEN 2
ELSE 0
END AS 'Feb',
CASE
WHEN round(sum(full_amount)/1300) = 3 THEN 3
ELSE 0
END AS 'March',
任何人都可以建议我一个解决方案。 在此先感谢。
我正在寻找类似的东西。
Month Full_PaymentsRecived
Jan 20000
Feb 3900
.... ....
Dec 2600
答案 0 :(得分:1)
你知道,每个月每个月都要到1300,你可以计算每个学生已经支付了多少钱。从这一点开始,你就可以在第一个月开始分摊前锋,然后逐步减少付费金额。
SELECT Y.MM,
SUM(Y.DUEAMT) PAYMENTSEXPECTED,
SUM(Y.PAID) PAYMENTSRECEIVED,
(SUM(Y.PAID) / SUM(Y.DUEAMT)) * 100 RATIO_PAID_DUE,
SUM(CASE WHEN Y.PAID > 0 THEN 1 ELSE 0 END) NUMBER_OF_PAYMENTS_RECEIVED,
SUM(CASE WHEN Y.PAID = 0 THEN 1 ELSE 0 END) NUMBER_OF_PAYMENTS_MISSED,
SUM(CASE WHEN Y.PAID < Y.DUEAMT AND Y.PAID > 0 THEN 1 ELSE 0 END) NUMBER_OF_UNDERPAYMENTS
FROM
(
SELECT X.STUID,X.MM,
X.DUEAMT,
#IF(X.STUID <> @P, @TOTALPAID:=(SELECT SUM(FULLAMOUNT) FROM T WHERE T.STUID = X.STUID),0) TOTALPAID,
IF(X.STUID <> @P, @FULLAMOUNT:=(SELECT SUM(FULLAMOUNT) FROM T WHERE T.STUID = X.STUID)
#- X.DUEAMT
,
@FULLAMOUNT:=@FULLAMOUNT - X.DUEAMT) EXHAUST,
CASE WHEN @FULLAMOUNT >= X.DUEAMT THEN X.DUEAMT
WHEN @FULLAMOUNT < 0 THEN 0
ELSE @FULLAMOUNT
END AS PAID,
@P:=X.STUID
FROM
(
SELECT DISTINCT T.STUID , 1300 AS DUEAMT, S.MM
FROM T,
(
SELECT 1 AS MM UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION
SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION
SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12
) S
) X , (SELECT @TOTALPAID:=0,@FULLAMOUNT:=0,@P:=0) FA
ORDER BY X.STUID,X.MM
) Y
GROUP BY Y.MM
子查询S为每个学生生成每月的到期金额 然后,子查询X计算出每个学生每个月支付的金额 子查询Y然后聚合以获得结果(我添加了一些在您的问题中不需要的加法分析)。
所以给出
MariaDB [sandbox]> select * from t;
+-----------+-------+----------+------------+----------+
| paymentid | stuid | stu_name | fullamount | pay_comp |
+-----------+-------+----------+------------+----------+
| 1 | 1234 | Jhon | 2600 | Feb |
| 2 | 1212 | Silva | 1000 | Jan |
| 3 | 1234 | Jhon | 1300 | Mar |
| 4 | 1212 | Silva | 3900 | Apr |
| 5 | 3333 | Perera | 15600 | Dec |
+-----------+-------+----------+------------+----------+
5行(0.02秒)
结果
MM | PAYMENTSEXPECTED | PAYMENTSRECEIVED | RATIO_PAID_DUE | NUMBER_OF_PAYMENTS_RECEIVED | NUMBER_OF_PAYMENTS_MISSED | NUMBER_OF_UNDERPAYMENTS |
+----+------------------+------------------+-------------------+-----------------------------+---------------------------+-------------------------+
| 1 | 3900 | 3900 | 100 | 3 | 0 | 0 |
| 2 | 3900 | 3900 | 100 | 3 | 0 | 0 |
| 3 | 3900 | 3900 | 100 | 3 | 0 | 0 |
| 4 | 3900 | 2300 | 58.97435897435898 | 2 | 1 | 1 |
| 5 | 3900 | 1300 | 33.33333333333333 | 1 | 2 | 0 |
| 6 | 3900 | 1300 | 33.33333333333333 | 1 | 2 | 0 |
| 7 | 3900 | 1300 | 33.33333333333333 | 1 | 2 | 0 |
| 8 | 3900 | 1300 | 33.33333333333333 | 1 | 2 | 0 |
| 9 | 3900 | 1300 | 33.33333333333333 | 1 | 2 | 0 |
| 10 | 3900 | 1300 | 33.33333333333333 | 1 | 2 | 0 |
| 11 | 3900 | 1300 | 33.33333333333333 | 1 | 2 | 0 |
| 12 | 3900 | 1300 | 33.33333333333333 | 1 | 2 | 0 |
+----+------------------+------------------+-------------------+-----------------------------+---------------------------+-------------------------+
12 rows in set (0.04 sec)