MySQL汇总表 - 评估和调整

时间:2016-08-16 08:11:02

标签: mysql select sum

我有一个非常简单的汇总表,汇总表中的2个字段,每隔15分钟收集一次记录。所以;

SELECT timevalue, SUM(value1)  AS sumvalue1, SUM(value2) AS sumvalue2 
FROM table 
GROUP BY timevalue

返回我期望的结果;

timevalue        sumvalue1  sumvalue2
-------------------------------------
16/08/2016 08:30    3000      200
16/08/2016 08:45    3200      150
16/08/2016 09:00    3100      400
16/08/2016 09:15    3300      450
16/08/2016 09:30    3400      600

我的问题是,有没有办法检查每个总和值是否永远不会低于之前的值?如果它是从前一个时间值返回的总和? (因此总和值总是与前一个时间值相同或更大)。

结果表应如下所示;

timevalue        sumvalue1  sumvalue2
-------------------------------------
16/08/2016 08:30    3000      200
16/08/2016 08:45    3200      200
16/08/2016 09:00    3200      400
16/08/2016 09:15    3300      450
16/08/2016 09:30    3400      600

我猜我需要某种if语句?关于如何实现这一点的任何想法?

非常感谢

2 个答案:

答案 0 :(得分:0)

不是最漂亮的例子,但仍然应该有效。

INSERT INTO SecondTable VALUES (
  NOW(),
  GREATEST(
    YOUR_SUM_1, (SELECT sumvalue1 FROM FirstTable ORDER BY timevalue DESC LIMIT 1)
  ),
  GREATEST(
    YOUR_SUM_2, (SELECT sumvalue2 FROM FirstTable ORDER BY timevalue DESC LIMIT 1)
  )
 );

答案 1 :(得分:0)

您可以使用用户定义的变量执行此操作,然后执行某些算法

select timevalue,sumvalue_1 as sumvalue1, sumvalue_2 as sumvalue2 from
(
 select 
 timevalue,
 if(@prev_val1 = sumvalue1 or @prev_val1 > sumvalue1,@prev_val1,sumvalue1) as sumvalue_1,
 if(@prev_val2 = sumvalue2 or @prev_val2 > sumvalue2,@prev_val2,sumvalue2) as sumvalue_2,
 @prev_val1 := sumvalue1,
 @prev_val2 := sumvalue2
 from mytable,(select @prev_val1:=0,@prev_val2:=0)x
 order by timevalue
)x
order by timevalue 

这是一个演示

create table mytable (
 timevalue datetime,
 sumvalue1 int,
 sumvalue2 int
);

insert into mytable values 
('2016-08-16 08:30:00',3000,200),
('2016-08-16 08:45:00',3200,150),
('2016-08-16 09:00:00',3100,400),
('2016-08-16 09:15:00',3300,450),
('2016-08-16 09:30:00',3400,600);


mysql> select * from mytable;
+---------------------+-----------+-----------+
| timevalue           | sumvalue1 | sumvalue2 |
+---------------------+-----------+-----------+
| 2016-08-16 08:30:00 |      3000 |       200 |
| 2016-08-16 08:45:00 |      3200 |       150 |
| 2016-08-16 09:00:00 |      3100 |       400 |
| 2016-08-16 09:15:00 |      3300 |       450 |
| 2016-08-16 09:30:00 |      3400 |       600 |
+---------------------+-----------+-----------+

现在有了查询

mysql> select timevalue,sumvalue_1 as sumvalue1, sumvalue_2 as sumvalue2 from
    -> (
    ->  select 
    ->  timevalue,
    ->  if(@prev_val1 = sumvalue1 or @prev_val1 > sumvalue1,@prev_val1,sumvalue1) as sumvalue_1,
    ->  if(@prev_val2 = sumvalue2 or @prev_val2 > sumvalue2,@prev_val2,sumvalue2) as sumvalue_2,
    ->  @prev_val1 := sumvalue1,
    ->  @prev_val2 := sumvalue2
    ->  from mytable,(select @prev_val1:=0,@prev_val2:=0)x
    ->  order by timevalue
    -> )x
    -> order by timevalue;
+---------------------+-----------+-----------+
| timevalue           | sumvalue1 | sumvalue2 |
+---------------------+-----------+-----------+
| 2016-08-16 08:30:00 |      3000 |       200 |
| 2016-08-16 08:45:00 |      3200 |       200 |
| 2016-08-16 09:00:00 |      3200 |       400 |
| 2016-08-16 09:15:00 |      3300 |       450 |
| 2016-08-16 09:30:00 |      3400 |       600 |
+---------------------+-----------+-----------+