我有下面的数据集,看起来像这样。
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:10 12.389 12.389 12.389 12.389 1
4/14/2010 0:10 12.1836 12.1836 12.1836 12.1836 2
4/14/2010 0:10 11.3887 11.3887 11.3887 11.3887 3
我想将数据转换为
t,str_agg
'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:10:00','12.3890','12.3890','12.3890','12.3890','12.1836','12.1836','12.1836','12.1836','11.3887','11.3887','11.3887','11.3887'
因此,如果您查看结果,则对于t = 4/14/2010 0:00,data_id 3没有数据,因此结果将没有以逗号分隔的值。
我希望这个结果在postgres中。我尝试过以下查询: -
select t,string_agg(mean||','||max||','||min||','||std,',') within group(order by t)
from table_name
group by t
order by t;
但这给了我以下结果: -
t,str_agg
'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:10:00','12.3890','12.3890','12.3890','12.3890','12.1836','12.1836','12.1836','12.1836','11.3887','11.3887','11.3887','11.3887'
答案 0 :(得分:2)
WITH dataset AS (
SELECT *
FROM
(
VALUES
('2010-04-14T00:00'::TIMESTAMP, 1, 1, 1, 1, 1),
('2010-04-14T00:00'::TIMESTAMP, 2, 2, 2, 2, 2),
('2010-04-14T00:20'::TIMESTAMP, 3, 3, 3, 3, 1),
('2010-04-14T00:20'::TIMESTAMP, 4, 4, 4, 4, 2),
('2010-04-14T00:20'::TIMESTAMP, 5, 5, 5, 5, 3)
) AS data(t, mean, max, min, std, data_id)
),
timestamps AS (
SELECT t FROM dataset GROUP BY t
),
data_id AS (
SELECT data_id AS id FROM dataset GROUP BY data_id
),
dataset_full AS (
SELECT
coalesce(dataset.t, ts.t) AS t,
mean,
max,
min,
std,
data_id
FROM
-- generate_series(
-- (SELECT min(t) FROM dataset),
-- (SELECT max(t) FROM dataset),
-- '10 minutes')
-- AS ts(t)
timestamps AS ts
-- CROSS JOIN generate_series(
-- (SELECT min(data_id) FROM dataset),
-- (SELECT max(data_id) FROM dataset))
-- AS data_id(id)
CROSS JOIN data_id
LEFT JOIN dataset ON ts.t = dataset.t AND data_id.id = dataset.data_id
)
SELECT
t,
string_agg(concat(mean, ',', max, ',', min, ',', std), ',')
FROM dataset_full
GROUP BY t
ORDER BY t;
数据集 CTE(公用表表达式)就在那里代替您的表。
dataset_full 通过为10m间隔和data_id值的每个组合生成一行来添加所有缺失的行。 dataset 然后LEFT JOIN到它,这意味着现在有那些以前不存在的行的NULL值。然后将NULL值转换为string_agg中的空字符串,从而产生您想要的结果。
修改强>
我在评论中根据请求更改了它,以便它只返回原始数据集中存在时间戳的行。
编辑2
我根据OP的另一个请求更改了它,只使用数据集中的data_ids。