[英]Merging rows in MySQL with null values on same timestamp
I have a query that is returning all of the data that I want but with a lot of extra null values because of how the case statements execute.我有一个查询,它返回我想要的所有数据,但由于 case 语句的执行方式,还有很多额外的 null 值。 I am trying to get rid of all of the null values just have one row of data for each timestamp.我试图摆脱所有 null 值,每个时间戳只有一行数据。 Here is the query:这是查询:
SELECT
FROM_UNIXTIME(t_stamp/1000, '%b %D, %Y %h:%i %p') as t_stamp,
(case when t.id = 460 then s.floatvalue end) as ait_3022_btu_dry,
(case when t.id = 460 then s.dataintegrity end) as ait_3022_btu_dry_qual,
(case when t.id = 481 then s.floatvalue end) as ait_3022_ch4,
(case when t.id = 481 then s.dataintegrity end) as ait_3022_ch4_qual,
(case when t.id = 622 then s.floatvalue end) as fqe_3004_cd_lr,
(case when t.id = 622 then s.dataintegrity end) as fqe_3004_cd_lr_qual,
(case when t.id = 641 then s.floatvalue end) as at_3005,
(case when t.id = 641 then s.dataintegrity end) as at_3005_qual,
(case when t.id = 647 then s.floatvalue end) as ft_3004_scfm,
(case when t.id = 647 then s.dataintegrity end) as ft_3004_scfm_qual,
(case when t.id = 934 then s.floatvalue end) as ft_3005_scfm,
(case when t.id = 934 then s.dataintegrity end) as ft_3005_scfm_qual
FROM sqlt_data_1_2019_10 s
inner join sqlth_te t
on s.tagid = t.id
WHERE
(s.floatvalue AND s.dataintegrity) IS NOT NULL
AND
(t_stamp >= 1570597200000 and t_stamp <= 1570629600000)
AND
(tagid = 934
or tagid = 647
or tagid = 622
or tagid = 641
or tagid = 460
or tagid = 481)
ORDER BY t_stamp ASC
and here is what it is returning:这是它返回的内容:
I need to return just one row per timestamp每个时间戳我只需要返回一行
You just need to turn on aggregation:您只需要打开聚合:
SELECT
FROM_UNIXTIME(t_stamp/1000, '%b %D, %Y %h:%i %p') as t_stamp,
MAX(case when t.id = 460 then s.floatvalue end) as ait_3022_btu_dry,
MAX(case when t.id = 460 then s.dataintegrity end) as ait_3022_btu_dry_qual,
MAX(case when t.id = 481 then s.floatvalue end) as ait_3022_ch4,
MAX(case when t.id = 481 then s.dataintegrity end) as ait_3022_ch4_qual,
MAX(case when t.id = 622 then s.floatvalue end) as fqe_3004_cd_lr,
MAX(case when t.id = 622 then s.dataintegrity end) as fqe_3004_cd_lr_qual,
MAX(case when t.id = 641 then s.floatvalue end) as at_3005,
MAX(case when t.id = 641 then s.dataintegrity end) as at_3005_qual,
MAX(case when t.id = 647 then s.floatvalue end) as ft_3004_scfm,
MAX(case when t.id = 647 then s.dataintegrity end) as ft_3004_scfm_qual,
MAX(case when t.id = 934 then s.floatvalue end) as ft_3005_scfm,
MAX(case when t.id = 934 then s.dataintegrity end) as ft_3005_scfm_qual
FROM sqlt_data_1_2019_10 s
inner join sqlth_te t
on s.tagid = t.id
WHERE
(s.floatvalue AND s.dataintegrity) IS NOT NULL
AND
(t_stamp >= 1570597200000 and t_stamp <= 1570629600000)
AND
(tagid = 934
or tagid = 647
or tagid = 622
or tagid = 641
or tagid = 460
or tagid = 481)
GROUP BY FROM_UNIXTIME(t_stamp/1000, '%b %D, %Y %h:%i %p')
ORDER BY t_stamp ASC
Also as commented by Gordon Linoff, please note that the conditions on tagid in the WHERE clause would be better written:同样正如 Gordon Linoff 评论的那样,请注意 WHERE 子句中 tagid 的条件最好写成:
tag_id IN (934, 647, 622, 641, 460, 481)
EDIT:编辑:
Since I brought up the subject, the where
clause should look like:由于我提出了这个主题,因此where
子句应如下所示:
WHERE s.floatvalue IS NOT NULL AND
s.dataintegrity IS NOT NULL AND
t_stamp >= 1570597200000 AND
t_stamp <= 1570629600000 AND
tagid IN (934, 647, 622, 641, 460, 481)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.