我想为特定公司计算以下内容,即total credit
:
我已经计算了credit_amount(200+100+5000+100 = 5400
)的总和,即Used Credit
现在我要计算应该是这样的200 + 100 + 5000 + 70 = 5370
70
值credit_amount
之所以来,是因为它来自最后一个包
这是credit_remaining
100 - recharge_date
30 = 70。
正如你可以看到200 / 0.24 = 833.33
100 / 0.24 = 416.667
5000 / 0.20 = 25000
70 / 0.30 = 233.33
如果该日期大于当前日期,那么包是活动的。所以当前的活动包是我已附上的截图中的第一个。
如何找到使用过的积分?
使用的总呼叫数量相同..它将是
used calls
因此26483.33
将是上面的总和,大约为SELECT (SELECT Sum(credit_amount)
FROM `company_credit`
WHERE `company_id` = 13) AS total_credit,
Sum(credit_amount / rate) AS used_calls,
Sum(credit_amount) AS used_credit
FROM `company_credit`
WHERE `company_id` = 13
AND recharge_date < Now()
ORDER BY `id` DESC
这是我的查询代码,它没有提供我需要的值:
.nodes()
答案 0 :(得分:4)
SELECT Sum(credit_amount) AS total_credit,
Sum(CASE WHEN recharge_date < Now() THEN (credit_amount / rate) ELSE ((credit_amount-credit_reamining) / rate) END ) AS total_sms,
Sum(CASE WHEN recharge_date < Now() THEN credit_amount ELSE (credit_amount-credit_reamining) END ) AS used_credit
FROM `company_credit`
WHERE `company_id` = 13
ORDER BY `id` DESC
此查询适用于 company_id = 13
。如果您为所有company_id计算相同的逻辑,只需删除 WHERE
条件并添加 GROUP BY company_id
。您将获得所有公司的计算记录。
尝试以上查询。
我在这里只是使用 CASE WHEN
来指定哪些记录还有余额。
使用该查询,您最终可以计算 total_sms,used_credit,total_credit
。
答案 1 :(得分:0)
SELECT SUM(credt_amount) ,
SUM(
CASE
WHEN cr_date >
(SELECT cur_date FROM tbl_eod_bod_stat
)
THEN credt_amount - credt_rem
ELSE credt_amount
END ) AS Used_Credit ,
SUM(
CASE
WHEN cr_date >
(SELECT cur_date FROM tbl_eod_bod_stat
)
THEN (credt_amount - credt_rem)/rate
ELSE ( credt_amount / rate)
END) AS rateDiv
FROM tbl_company_2;
请查看这是否有帮助。