我有下面的数据集,看起来像这样。
t mean max min std data_id
4/14/2010 0:00 12.6941 12.6941 12.6941 12.6941 1
4/14/2010 0:00 12.3851 12.3851 12.3851 12.3851 2
4/14/2010 0:20 12.389 12.389 12.389 12.389 1
4/14/2010 0:20 12.1836 12.1836 12.1836 12.1836 2
4/14/2010 0:20 11.3887 11.3887 11.3887 11.3887 6
这里唯一的data_id是(1,2,6),但我有另一个data_id集(1,2,4,5,6),我想用它来获取数据。
现在对于时间t不存在的所有data_id我想要将null(mean,max.std,min)值添加到它们,所以在这种情况下我想要下面的结果集: -
'2010-04-14 00:00:00','12.6941,12.6941,12.6941,12.6941,12.3851,12.3851,12.3851,12.3851,,,,,,,,,,,,,'
'2010-04-14 00:20:00','12.389,12.389,12.389,12.389,12.1836,12.1836,12.1836,12.1836,,,,,,,,,11.3887,11.3887,11.3887,11.3887'
我使用了以下查询: -
with dataset as (
select *
from (values ('2010-04-14T00:00'::TIMESTAMP, 12.6941, 12.6941, 12.6941, 12.6941, 1),
('2010-04-14T00:00'::TIMESTAMP, 12.3851, 12.3851, 12.3851, 12.3851, 2),
('2010-04-14T00:20'::TIMESTAMP, 12.389, 12.389, 12.389, 12.389, 1),
('2010-04-14T00:20'::TIMESTAMP, 12.1836, 12.1836, 12.1836, 12.1836, 2),
('2010-04-14T00:20'::TIMESTAMP, 11.3887, 11.3887, 11.3887, 11.3887, 6)
) AS data(t, mean, max, min, std, data_id)
),
dataset_full as (
select t.t, d.data_id,
ds.mean, ds.max, ds.min, ds.std
from (select distinct t from dataset) t cross join
(select distinct data_id from dataset) d left join
dataset ds
on ds.t = t.t and ds.data_id = d.data_id
)
select t,string_agg(concat(mean, ',', max, ',', min, ',', std), ',' order by data_id)
from dataset_full
group by t
order by t;
我得到以下结果: -
'2010-04-14 00:00:00','12.6941,12.6941,12.6941,12.6941,12.3851,12.3851,12.3851,12.3851,,,,'
'2010-04-14 00:20:00','12.389,12.389,12.389,12.389,12.1836,12.1836,12.1836,12.1836,11.3887,11.3887,11.3887,11.3887'
我没有得到data_id(4,5,6)at = 4/14/2010 0:00和data_id(4,5)at t = 4/14/2010 0:20的空值。
答案 0 :(得分:1)
只需在定义data_set_full
时包含所需的ID:
dataset_full as (
select t.t, d.data_id,
ds.mean, ds.max, ds.min, ds.std
from (select distinct t from dataset) t cross join
(values (1), (2), (4), (5), (6)) d(data_id) left join
dataset ds
on ds.t = t.t and ds.data_id = d.data_id
)
cross join
的目的是获取结果集中所需的所有记录。因此,请包含您想要的ID和时间戳。然后left join
会带来适当的数据(如果有的话)。