MySQL累积总和按日期分组,但在特定日期后排除

时间:2019-12-10 14:53:09

标签: mysql date group-by cumulative-sum

我有一个涉及日期的查询,该日期确实接近完成,但是我不太了解最后一部分。表格列为:

purchased TINYINT
purchased_date DATE
expired TINYINT
expiration_date DATE

我正在运行的查询是这样:

SELECT 
    e_date,
    num_interactions,
    @runningTotal := @runningTotal + totals.num_interactions AS runningTotal
FROM
(SELECT 
    DATE(purchase_date) AS e_date,
    COUNT(*) AS num_interactions
FROM domain_names AS d
WHERE purchased = 1
AND purchase_date != "0000-00-00"
GROUP BY DATE(d.purchase_date)) totals
ORDER BY e_date;

效果很好,但我需要作最后的调整。我需要从expiration date之后过期(基于e_date)的运行总计中排除/减去名称。

换句话说,如果运行总计不应在域过期后对其进行计数。非常感谢您的建议。这是我在这里的第一篇文章,所以希望我已经清楚地解释了。

编辑: 我整理了一个MCRE:

CREATE TABLE IF NOT EXISTS `domain_names` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `purchased` tinyint(1) DEFAULT NULL,
  `purchase_date` date DEFAULT NULL,
  `expired` tinyint(1) DEFAULT NULL,
  `expiration_date` date DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `index_domain_names_on_id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

INSERT INTO `domain_names` (`id`, `name`, `purchased`, `purchase_date`, `expired`, `expiration_date`) VALUES
  ('1', 'example1.com', '1', '2019-01-12', NULL, '2019-08-12'),
  ('2', 'example2.com', '1', '2019-01-12', NULL, '2020-10-12'),
  ('3', 'example3.com', '1', '2019-01-12', '1', '2019-10-12'),
  ('4', 'example4.com', '1', '2019-10-12', NULL, '2020-10-12'),
  ('5', 'example2.com', '1', '2019-03-19', NULL, '2020-03-19'),
  ('6', 'example3.com', '1', '2019-08-19', NULL, '2020-08-10'),
  ('7', 'example4.com', '1', '2019-11-28', NULL, '2020-10-12');

http://sqlfiddle.com/#!9/2d9f71/1/1

2 个答案:

答案 0 :(得分:1)

这是不依赖变量和子查询的另一种方法

SELECT 
    DATE(d.purchase_date),
    COUNT(DISTINCT d.id) AS num_interactions,
    COUNT(DISTINCT c.id) AS cumulative_sum_not_expired
FROM domain_names AS d
LEFT JOIN domain_names AS c ON (DATE(c.purchase_date) <= DATE(d.purchase_date)
                                AND c.purchased = 1
                                AND c.purchase_date != 0
                                AND c.expiration_date >= CURDATE())
WHERE d.purchased = 1
      AND d.purchase_date != 0
GROUP BY 1
ORDER BY 1;

http://sqlfiddle.com/#!9/2d9f71/36/0

您可以在此处调整排除条件: c.expiration_date >= CURDATE()

答案 1 :(得分:0)

如果您需要排除过期的域,则应在where子句expiration_date > CURDATE()中添加couning子查询或仅计算未过期的域COUNT(case when expiration_date > CURDATE() then 1 end) ...

SET @runningTotal := 0;
SET @runningTotalNE := 0;
SELECT 
    e_date,
    num_interactions,
    @runningTotal := @runningTotal + totals.num_interactions AS runningTotal,
    @runningTotalNE := @runningTotalNE + totals.notexpired AS runningTotalNotExpired
FROM
    (SELECT 
     purchase_date AS e_date,
     COUNT(*) AS num_interactions,
     COUNT(case when expiration_date > CURDATE() then 1 end) AS notexpired
     FROM domain_names AS d
     WHERE purchased = 1
     /*AND expiration_date > CURDATE()*/
     AND purchase_date != "0000-00-00"
     GROUP BY d.purchase_date) totals
     ORDER BY e_date