无法累计总和“ COUNT(*)”

乔尔

此答案的第二部分使用变量来创建另一列的累加和。我正在做相同的事情,除了我正在使用一条GROUP BY语句,并求和COUNT(*)而不是一列。这是我的代码,用于创建最小表并插入值:

CREATE TABLE `test_group_cumulative` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
    `group_id` int(11) unsigned NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `test_group_cumulative` (`id`, `group_id`)
VALUES
    (1, 1),
    (2, 2),
    (3, 3);

这是失败的代码:

SELECT
    `group_id`,
    COUNT(*) AS `count`,
    @count_cumulative := @count_cumulative + COUNT(*) AS `count_cumulative`
FROM `test_group_cumulative` AS `tgc`
JOIN (SELECT @count_cumulative := 0) AS `_count_cumulative`
GROUP BY `group_id`
ORDER BY `id`;

结果如下:

group_id    count   count_cumulative
1   1   1
2   1   1
3   1   1

如您所见,count_cumulative未正确汇总。但是,这是奇怪的部分。如果我用COUNT(*)incount_cumulative的值替换in 1,则查询可以正常工作。

    @count_cumulative := @count_cumulative + 1 AS `count_cumulative`

这是正确的结果:

group_id    count   count_cumulative
1   1   1
2   1   2
3   1   3

显然,在我的应用中,每个组中将有一个以上的项目,因此COUNT(*)不会总是存在1我知道可以通过联接或子查询来执行此操作,如果需要的话,我会执行此操作,但是我认为这应该可行。那么,为什么不在COUNT(*)累计金额之内工作呢?

哈里穆尔·阿里(Haleemur Ali)

我在进行时间序列分析时经常遇到这个问题。解决此问题的首选方法是将其包装到第二个选择中,然后在最后一层引入计数器。如果需要,您可以使用临时表使此技术适应更复杂的数据流。

我使用您提供的模式做了这个小sqlfiddle: http://sqlfiddle.com/#!2/cc97e/21

这是获取累积计数的查询:

SELECT
tgc.group_id, @count_cumulative := @count_cumulative + cnt as cum_cnt
FROM (
  SELECT
    group_id, COUNT(*) AS cnt
  FROM `test_group_cumulative` 
  group by group_id
  order by id) AS `tgc`, 
(SELECT @count_cumulative := 0) AS `temp_var`; 

这是我得到的结果:

GROUP_ID    CUM_CNT
1           1
2           2
3           3

您的尝试无效的原因:

当您使用临时变量进行分组时,mysql将独立执行各个组,并在为每个组分配临时变量当前值时(在这种情况下为0)。

如果,您运行此查询:

SELECT @count_cumulative;

之后立马

SELECT
    `group_id`,
    COUNT(*) AS `count`,
    @count_cumulative := @count_cumulative + COUNT(*) AS `count_cumulative`
FROM `test_group_cumulative` AS `tgc`
JOIN (SELECT @count_cumulative := 0) AS `_count_cumulative`
GROUP BY `group_id`
ORDER BY `id`;

您将获得值1。对于每个组,@ count_cumulative都将重置为0。

因此,在我提出的解决方案中,我通过先生成“组计数”然后进行累加来规避此问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章