Mysql累计总有时只工作

时间:2016-05-03 02:31:26

标签: mysql sql

我正在使用以下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 |
+------------------+----------------+-------+

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

e.g。这就是我对旧数据的作用:

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

这就是它对我最新数据的作用:

public class RunClock: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };
    ThreadPoolTimer _clockTimer = null;
    private string clock="";
    public string Clock
    {
        set
        {
            clock= value;
            this.OnPropertyChanged();
        }
        get { return clock; } }
    public RunClock()
    {

        _clockTimer = ThreadPoolTimer.CreatePeriodicTimer(_clockTimer_Tick, TimeSpan.FromMilliseconds(1000));            
    private void _clockTimer_Tick(ThreadPoolTimer timer)
    {
        DateTime datetime = DateTime.Now;
        Str1 = datetime.ToString();
    }
    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

如何不再计算运行总计?

任何想法都赞赏!

1 个答案:

答案 0 :(得分:1)

累计金额和汇总有时不会混合。试试这个:

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;