如何从第二天早上8点到早上8点获取每日计数器读数总和?
create table Tbl (counterID int, `timestamp` datetime, `counter` int);
insert into Tbl values
(1, '2017-09-18 07:07:03', 22);
insert into Tbl values
(1, '2017-09-18 08:08:03', 25);
insert into Tbl values
(1, '2017-09-18 19:08:33', 30);
insert into Tbl values
(1, '2017-09-19 10:14:54', 8);
insert into Tbl values
(1, '2017-09-19 10:15:24', 13);
insert into Tbl values
(1, '2017-10-20 04:21:08', 5);
insert into Tbl values
(1, '2017-10-23 14:21:38', 24);
insert into Tbl values
(1, '2017-10-23 14:22:08', 72);
insert into Tbl values
(1, '2017-10-23 14:22:38', 86);
insert into Tbl values
(1, '2017-10-24 03:23:09', 100);
insert into Tbl values
(1, '2017-10-24 04:23:38', 120);
insert into Tbl values
(1, '2017-10-24 04:24:08', 125);
insert into Tbl values
(1, '2017-10-25 14:56:52', 2);
insert into Tbl values
(1, '2017-10-25 14:57:22', 8);
insert into Tbl values
(1, '2017-10-25 16:39:22', 21);
insert into Tbl values
(1, '2017-10-25 16:41:52', 22);
insert into Tbl values
(1, '2017-10-25 16:42:22', 23);
insert into Tbl values
(1, '2017-10-25 17:18:13', 26);
insert into Tbl values
(1, '2017-10-25 17:21:15', 17);
insert into Tbl values
(1, '2017-10-25 17:21:46', 19);
insert into Tbl values
(1, '2017-10-25 17:22:46', 41);
insert into Tbl values
(1, '2017-10-26 08:41:58', 2);
insert into Tbl values
(1, '2017-10-26 14:02:28', 5);
insert into Tbl values
(1, '2017-10-30 13:39:20', 1);
insert into Tbl values
(1, '2017-10-30 13:40:19', 4);
答案 0 :(得分:0)
以下是您的查询的解决方案,请查看:
select counterid,date(ts2) ts,sum(counter) counter_sum
from (select counterid,counter,convert_tz(ts,'+08:00','+00:00') ts2
from tx3) s
group by counterid,date(ts2);
输出
+-----------+------------+-------------+
| counterid | ts | counter_sum |
+-----------+------------+-------------+
| 1 | 2017-09-17 | 22 |
| 1 | 2017-09-18 | 55 |
| 1 | 2017-09-19 | 21 |
| 1 | 2017-10-19 | 5 |
| 1 | 2017-10-23 | 527 |
| 1 | 2017-10-25 | 179 |
| 1 | 2017-10-26 | 7 |
| 1 | 2017-10-30 | 5 |
+-----------+------------+-------------+
答案 1 :(得分:0)
尝试此查询:
select counterid, start, end, sum(counter) from (
select counterid,
case when cast(timestamp as time) < '08:00:00' then date_add(cast(`timestamp` as date), interval -1 day)
else cast(`timestamp` as date) end `start`,
case when cast(timestamp as time) < '08:00:00' then cast(`timestamp` as date)
else date_add(cast(`timestamp` as date), interval 1 day) end `end`,
counter
from tbl
) `a` group by counterid, start, end