【发布时间】: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
以上查询不适用于我最近三个月的数据
【问题讨论】:
-
您的预期输出是什么?以表格文本格式将该位添加到问题中。
-
我觉得不错。