Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql累积总计有时仅起作用_Mysql_Sql - Fatal编程技术网

Mysql累积总计有时仅起作用

Mysql累积总计有时仅起作用,mysql,sql,Mysql,Sql,我使用以下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.idd

我使用以下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已经完美地工作了一年,但是对于最新的一批数据,它不再计算运行总数,它只显示每一天的总数

e、 g.这是它对我的旧数据所做的:

+------------------+----------------+-------+
| 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;

太棒了,这就是诀窍,你能详细说明你的方法为什么有效吗?@danspants。唉,我真的无法详细说明。我观察到变量有时不适用于
groupby
查询,因为
select
的求值顺序不正确。在子查询中具体化
groupby
可以解决此问题。