我正在尝试在Postgres中创建一个查询,以获得表格形式的结果,如下所示。我们的想法是创建一个表,其中包含在dateM处名称为variableN的变量的值。
表示输出不是按照传感器名称排列日期值对的完整列表,而是输出logtime variable1 variable2 variable3
------------------------------------------------------
date1 value11 value12 value13
date2 value21 value22 value23
通过这种方式,我使用函数交叉表,但对我来说,这个函数是第一次使用,我不知道它。我尝试了几种不同的方式,但没有人工作。我会非常感激任何帮助。
select * from crosstab(
'SELECT sensorhistdatalog.valuedate as logtime, sensorhistdatalog.value as logvalue, sensor.name FROM public.sensor
INNER JOIN public.sensorhistdatalog
ON sensor.id = sensorhistdatalog.sensor_id
where sensorhistdatalog.valuedate between ''2017-03-06'' and ''2017-03-07''
and sensor.name in (''LHTES_ON','C_VELOCIDAD_V3_MAN','C_VELOCIDAD_V4_MAN','VELOCIDAD_V3','VELOCIDAD_V4','LHTES MODE','TEMP_COMF_W','TEMP_COMF_S',
'TO_EXT','TO_INT','TI_EXT','TI_INT','TEMP_PLATE_1','TEMP_PLATE_2','TEMP_PLATE_3','TEMP_PLATE_4','DUMPER_1','DUMPER_2','REF_V3','REF_V4'')
order by name, valuedate asc')
as (
logtime text,
logvalue text,
name text);
交叉表是sensorhistdatalog和sensor,其中sensorhistdatalog有几列,但我们主要使用sensor_id(表传感器的id中的外键),日期和值。传感器表基本上是具有id,名称和描述的传感器列表。
错误是:
ERROR: no existing function crosstab(unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown)
答案 0 :(得分:0)
$something$
进行整个查询 AS子句不描述您的查询结果,它必须描述crosstab
的结果
SELECT * FROM crosstab(
$$SELECT shdl.valuedate as logtime,
shdl.value as logvalue,
s.name
FROM public.sensor as s
INNER JOIN public.sensorhistdatalog as shdl
ON s.id = shdl.sensor_id
WHERE shdl.valuedate between '2017-03-06' and '2017-03-07'
AND s.name in ('LHTES_ON', 'C_VELOCIDAD_V3_MAN', 'C_VELOCIDAD_V4_MAN', 'VELOCIDAD_V3', 'VELOCIDAD_V4', 'LHTES MODE', 'TEMP_COMF_W', 'TEMP_COMF_S', 'TO_EXT', 'TO_INT', 'TI_EXT', 'TI_INT', 'TEMP_PLATE_1', 'TEMP_PLATE_2', 'TEMP_PLATE_3', 'TEMP_PLATE_4', 'DUMPER_1', 'DUMPER_2', 'REF_V3', 'REF_V4'')
ORDER BY name, valuedate asc
$$)
AS (logtime text, variable1 text, variable2 text, variable3 text);