устройство | дата-время | метраж
-------------------------------------------------------------------------------
1 | 01/01/2022 05:00:01 | 1,01
2 | 01/01/2022 05:00:01 | 2,10
3 | 01/01/2022 05:00:01 | 3,04
4 | 01/01/2022 05:00:01 | 1,41
5 | 01/01/2022 05:00:01 | 2,60
1 | 02/01/2022 01:01:01 | 3,01
2 | 03/01/2022 03:12:01 | 6,14
3 | 04/01/2022 05:13:01 | 1,01
4 | 05/01/2022 03:24:01 | 5,51
5 | 06/01/2022 02:35:01 | 3,74
дата | устр_1 | устр_2 | устр_3 | устр_4 | устр_5
--------------------------------------------------------------------------------
01/01/2022 05:00:01 | 1,01 | 2,10 | 3,04 | 1,41 | 2,60
02/01/2022 01:01:01 | 3,01 | null | null | null | null
03/01/2022 03:12:01 | null | 6,14 | null | null | null
04/01/2022 05:13:01 | null | null | 1,01 | null | null
...
CREATE TABLE data(
device INT NOT NULL,
date DATETIME NOT NULL,
metric NUMERIC(10,2) NOT NULL
);
INSERT INTO data VALUES (1, '2022-01-01 00:00:00', 2.34),
(2, '2022-01-01 00:00:00', 1.23),
(3, '2022-01-01 00:00:00', 3.82),
(4, '2022-01-01 00:00:00', 0.45),
(1, '2022-01-01 01:00:00', 2.00),
(2, '2022-01-01 01:00:00', 1.82),
(3, '2022-01-01 01:00:00', 3.09);
SELECT date, SUM(d1), SUM(d2), SUM(d3), SUM(d4) FROM (
SELECT date,
CASE WHEN device=1 THEN metric ELSE NULL END AS d1,
CASE WHEN device=2 THEN metric ELSE NULL END AS d2,
CASE WHEN device=3 THEN metric ELSE NULL END AS d3,
CASE WHEN device=4 THEN metric ELSE NULL END AS d4
FROM data
) nested
GROUP BY date;
select
_date,
sum(case when _ustr = 1 then _metr else 0 end) _ustr_1,
sum(case when _ustr = 2 then _metr else 0 end) _ustr_3,
sum(case when _ustr = 3 then _metr else 0 end) _ustr_3,
sum(case when _ustr = 4 then _metr else 0 end) _ustr_4,
sum(case when _ustr = 5 then _metr else 0 end) _ustr_5
from tbl
group by _date;