没有数据特定日期,月份时累积查询不起作用

时间:2014-08-04 12:52:04

标签: mysql sql

==============================
ID1 |ID2|ID3 |  Date    |count|
==============================|
1   |a  |b   |01-01-2014|   10|
1   |a  |b   |18-06-2014|   0 |
1   |b  |d   |01-01-2014|   10|
1   |b  |d   |01-02-2014|   5 |
1   |d  |ed  |01-02-2014|   15|
1   |d  |ed  |18-06-2014|   0 |
2   |e  |abc5|02-01-2014|   5 |
2   |e  |abc5|03-01-2014|   5 |
2   |e  |abc6|25-06-2014|   5 |
2   |e  |abc7|21-01-2014|   25| 
3   |b  |d   |01-01-2014|   10|
3   |b  |d   |01-02-2014|   5 |
===============================

查询使用如下:

select id1,tmesg 
from (
       select    id1,id2,id3,count,date, 
                 (a.count + COALESCE((select sum(b.count) 
                                      from   table_test b 
                                      where b.date < a.date 
                                      and  b.id1= a.id1 
                                      and b.id2 = a.id2 
                                      and b.id3 = a.id3 
                                      group by id1,id2,id3),0)) as tmesg
      from table_test a where  date ='2014-08-01'  order by id1,id2,id3) z

group by id1,tmesg

累积输出应如下:

1   40
2   40
3   15

如果是&lt; 2014-08-01。它应该累积并将结果提供给可用的最大日期。

3 个答案:

答案 0 :(得分:1)

我无法理解为什么查询不仅仅是:

select id1, sum(count)
from table_test
where date < '2014-08-01' 
group by id1

这将从您的样本数据中生成所需的输出

答案 1 :(得分:1)

修改

根据OP的说明,无论操作员使用什么,查询都需要能够返回输出,我已经重写了查询,如下所示:

备注

max_dates根据以下规则返回每个id1的最大日期:

1如果条件返回了1行或更多行(例如&lt;'2014-08-01'),则返回这些行中的最大日期。

2如果条件没有返回任何行,则返回基于该id1的所有行的最大日期

select
    cd.id1,
    cd.count_dt,
    (select sum(b.count_no) 
    from counts_date b 
    where b.id1 = max_dates.id1 
    and b.count_dt <= cd.count_dt
    ) as t_mesg
from counts_date cd
inner join
    (select
        all_id1.id1 id1,
        COALESCE(max(a.count_dt), (select max(count_dt) from counts_date b where b.id1=all_id1.id1)) max_dt
    from counts_date a
    right join
    (select distinct id1 from counts_date) all_id1
    on a.id1 = all_id1.id1 and a.count_dt > '2014-09-02'
    group by all_id1.id1
    ) max_dates
on cd.id1= max_dates.id1 and cd.count_dt <= max_dates.max_dt
group by cd.id1, cd.count_dt
order by cd.id1, cd.count_dt;

查询返回每个日期的累计总和。如果只需要总和,则可以修改查询,如下所示:

select
    cd.id1,
    sum(cd.count_no) t_mesg
from counts_date cd
inner join
    (select
        all_id1.id1 id1,
        COALESCE(max(a.count_dt), (select max(count_dt) from counts_date b where b.id1=all_id1.id1)) max_dt
    from counts_date a
    right join
    (select distinct id1 from counts_date) all_id1
    on a.id1 = all_id1.id1 and a.count_dt < '2014-02-02'
    group by all_id1.id1
    ) max_dates
on cd.id1= max_dates.id1 and cd.count_dt <= max_dates.max_dt
group by cd.id1
order by cd.id1;

已更新 demo here 说明了这两个查询的工作情况。

<强>参考

Fetch cumulative sum from MySQL table on SO

答案 2 :(得分:0)

听起来您正在寻找创建日期范围的灵活性。 BETWEEN可以很好地处理这个问题,而不会有任何效率损失。

根据需要使用最短和最长日期传递开始日期和结束日期。您可以按如下方式处理不同的用例......

之前或之前

SELECT id1, sum(count)
FROM table_test
WHERE date BETWEEN '0000-00-00' AND '2014-08-01' 
GROUP BY id1

SELECT id1, sum(count)
FROM table_test
WHERE date BETWEEN '2014-08-01' AND '2014-08-01' 
GROUP BY id1

<或strong>

SELECT id1, sum(count)
FROM table_test
WHERE date BETWEEN '2014-08-01' AND '9999-12-31' 
GROUP BY id1

<强>后

SELECT id1, sum(count)
FROM table_test
WHERE date BETWEEN date_add('2014-08-01', INTERVAL 1 DAY) AND '9999-12-31' 
GROUP BY id1

这比尝试设计不同运算符得到相同结果的查询更简洁。