【问题标题】:How to get credit debit total for last 3 month via mysql query?如何通过mysql查询获得最近3个月的贷记借记总额?
【发布时间】:2019-08-23 14:41:59
【问题描述】:

我需要过去三个月的贷方和借方总额。我现在正在做的是得到贷方和借方的总和,然后我用借方减去贷方,这是不正确的,请帮助我。

我需要为单个用户提供过去三个月的(贷记 - 借记)。但现在它变为负数,这是不正确的。贷记和借记记录不应为负数(-ve)。信用每 3 个月到期一次

我的表如下

CREATE TABLE `cb_customer_credit_log` (
  `id` int(11) NOT NULL,
  `ord_id` varchar(255) NOT NULL,
  `coupon_assignee` varchar(255) NOT NULL,
  `coupon_assigned_to_id` varchar(255) NOT NULL,
  `coupon_id` varchar(255) NOT NULL,
  `coupon_code` varchar(255) NOT NULL,
  `coupon_amount` varchar(255) NOT NULL,
  `coupon_dis_type` varchar(255) NOT NULL,
  `amount` float(8,2) NOT NULL,
  `cr_dr` enum('C','D') NOT NULL,
  `order_note` text NOT NULL,
  `created_on` datetime NOT NULL,
  `modified_on` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


INSERT INTO `cb_customer_credit_log` (`id`, `ord_id`, `coupon_assignee`, `coupon_assigned_to_id`, `coupon_id`, `coupon_code`, `coupon_amount`, `coupon_dis_type`, `amount`, `cr_dr`, `order_note`, `created_on`, `modified_on`) VALUES
(1, '3327', 'pranav.patel@cc.com', 'marmik.p@cc.com', '2953', 'demo_code1', '10', 'fixed_cart', 10.00, 'C', 'new order credit', '2019-05-15 11:48:18', '0000-00-00 00:00:00'),
(5, '3350', 'pranav.patel@cc.com', '-', '-', '-', '-', '-', 9.00, 'D', 'credit used by user', '2019-08-23 07:34:31', '0000-00-00 00:00:00'),
(9, '3359', 'pranav.patel@cc.com', 'marmik.p@cc.com', '2953', 'demo_code1', '10', 'fixed_cart', 0.50, 'C', 'new order credit', '2019-08-23 00:00:00', '0000-00-00 00:00:00'),
(11, '3373', 'pranav.patel@cc.com', '-', '-', '-', '-', '-', 1.00, 'D', 'credit used by user', '2019-08-23 12:47:35', '0000-00-00 00:00:00'),
(12, '3378', 'marmik.p@cc.com', 'jayesh.b@cc.com', '2953', 'demo_code1', '10', 'fixed_cart', 0.50, 'C', 'new order credit', '2019-08-23 13:00:20', '0000-00-00 00:00:00');





-----My Query -----
SELECT 
    SUM(COALESCE(CASE WHEN cr_dr = 'C' THEN amount END,0)) - 
    SUM(COALESCE(CASE WHEN cr_dr = 'D' THEN amount END,0)) balance 
    FROM 
        cb_customer_credit_log 
    WHERE 
        coupon_assignee = 'pranav.patel@cc.com'
     AND 
        created_on >= now() - interval 3 month 

以上查询不适用于我最近三个月的数据

【问题讨论】:

  • 您的预期输出是什么?以表格文本格式将该位添加到问题中。
  • 我觉得不错。

标签: php mysql


【解决方案1】:

你的答案是正确的。 尝试以下查询: 对于特定(条件)用户:

select coupon_assignee, 
    SUM(COALESCE(CASE WHEN cr_dr = 'C' THEN amount END,0)) AS `Total Credit`,
    SUM(COALESCE(CASE WHEN cr_dr = 'D' THEN amount END,0)) AS `Total Debit`,
    ((SUM(COALESCE(CASE WHEN cr_dr = 'C' THEN amount END,0))) - (SUM(COALESCE(CASE 
    WHEN cr_dr = 'D' THEN amount END,0))))   AS `Total Balance` 
from cb_customer_credit_log
where coupon_assignee = 'pranav.patel@cc.com' 
AND created_on >= now() - interval 3 month;

所有用户:

select coupon_assignee, 
    SUM(COALESCE(CASE WHEN cr_dr = 'C' THEN amount END,0)) AS `Total Credit`,
    SUM(COALESCE(CASE WHEN cr_dr = 'D' THEN amount END,0)) AS `Total Debit`,
    ((SUM(COALESCE(CASE WHEN cr_dr = 'C' THEN amount END,0))) - (SUM(COALESCE(CASE 
    WHEN cr_dr = 'D' THEN amount END,0))))   AS `Total Balance` 
from cb_customer_credit_log
where created_on >= now() - interval 3 month
group by coupon_assignee;

对于组合用户:

select
    SUM(COALESCE(CASE WHEN cr_dr = 'C' THEN amount END,0)) AS `Total Credit`,
    SUM(COALESCE(CASE WHEN cr_dr = 'D' THEN amount END,0)) AS `Total Debit`,
    ((SUM(COALESCE(CASE WHEN cr_dr = 'C' THEN amount END,0))) - (SUM(COALESCE(CASE 
    WHEN cr_dr = 'D' THEN amount END,0))))   AS `Total Balance` 
from cb_customer_credit_log
where created_on >= now() - interval 3 month;

【讨论】:

  • @PranavPatel -9.5 是我所期望的
  • 是的,根据您和数据,这是正确的,但是某人的信用怎么会减去……这是违反逻辑的……信用在 3 个月后到期。
猜你喜欢
  • 2021-04-02
  • 1970-01-01
  • 2012-11-30
  • 1970-01-01
  • 2017-07-29
  • 2022-12-15
  • 1970-01-01
  • 2019-07-12
  • 2011-08-10
相关资源
最近更新 更多