繁体   English   中英

Mysql 累积总数有时只工作

[英]Mysql cumulative total only working sometimes

我正在使用以下 SQL 来计算数据处理函数的按日期运行的总数:

set @running_total := 0;
select date(event_date), (@running_total := @running_total + count(distinct de.iddocument)) AS cumulative_sum , count(distinct de.iddocument)
from document_event as de
left join document as d on d.iddocument = de.iddocument
where d.iddatastream = 142
and de.event_type = 'RESEARCHED'
and de.update_by_id is not null
group by date(event_date);

这个 SQL 已经完美运行了一年,但是在最新一批数据上它不再计算运行总数,它只显示每天的总数。

例如,这就是它对我的旧数据所做的:

+------------------+----------------+-------+
| date(event_date) | cumulative_sum | count |
+------------------+----------------+-------+
| 2015-11-09       |            167 |   167 |
| 2015-11-10       |            329 |   162 |
| 2015-11-11       |            775 |   446 |
| 2015-11-12       |           1151 |   376 |
| 2015-11-13       |           1680 |   529 |
| 2015-11-16       |           2266 |   586 |
| 2015-11-17       |           2837 |   571 |
| 2015-11-18       |           3590 |   753 |
| 2015-11-19       |           4162 |   572 |
+------------------+----------------+-------+

这就是它对我的最新数据所做的:

+------------------+----------------+-------+
| date(event_date) | cumulative_sum | count |
+------------------+----------------+-------+
| 2016-04-20       |              6 |     6 |
| 2016-04-21       |             91 |    91 |
| 2016-04-22       |            151 |   151 |
| 2016-04-26       |            239 |   239 |
| 2016-04-27       |            203 |   203 |
| 2016-04-28       |            312 |   312 |
| 2016-04-29       |            374 |   374 |
| 2016-05-02       |            368 |   368 |
| 2016-05-03       |            226 |   226 |
+------------------+----------------+-------+

怎么可能不再计算运行总数?

任何想法表示赞赏!

累积和和聚合有时不混合。 尝试这个:

select dte,
       (@running_total := @running_total + cnt) AS cumulative_sum, 
       cnt
from (select date(event_date) as dte, count(distinct de.iddocument) as cnt
      from document_event de left join
           document d
           on d.iddocument = de.iddocument
      where d.iddatastream = 142 and
            de.event_type = 'RESEARCHED' and
            de.update_by_id is not null
      group by date(event_date)
      order by date(event_date)
     ) cross join
     (select @running_total := 0) params;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM