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. I am trying to get rid of all of the null values just have one row of data for each timestamp. 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:
tag_id IN (934, 647, 622, 641, 460, 481)
EDIT:
Since I brought up the subject, the where
clause should look like:
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.