此查询出了什么问题?我一直在努力处理数据类型,但没有发现任何问题。
谢谢。
select * from crosstab(
$$
select * from
(values
(1546300800, 187923, 1.5),
(1546300800, 187924, 200),
(1546300800, 187926, 120),
(1546300800, 187927, 100),
(1546387200, 187923, 1.5),
(1546387200, 187924, 250),
(1546387200, 187926, 120),
(1546387200, 187927, 125),
(1546473600, 187923, 1.5),
(1546473600, 187924, 275),
(1546473600, 187926, 120),
(1546473600, 187927, 137.5),
(1546560000, 187923, 1.75)
) as t (datetime, trace, value)
$$
)
as final_result (
unixdatetime int,
trace1 double precision,
trace2 double precision,
trace3 double precision,
trace4 double precision
)
答案 0 :(得分:1)
尽管您没有提到自己遇到的错误,但这是使它起作用的方法:
初始化模块tablefunc
:
CREATE EXTENSION tablefunc;
然后,您需要注意数据类型。 PostgreSQL可能会很挑剔,并且在许多情况下不会自动进行魔术转换。在这种情况下,它们必须匹配,或者至少交叉表列类型定义必须能够完全包含交叉表函数SQL返回的类型。
您在这里有2个选择:
decimal
:select * from crosstab(
$$
select * from (values
(1546300800, 187923, 1.5),
(1546300800, 187924, 200),
(1546300800, 187926, 120),
(1546300800, 187927, 100),
(1546387200, 187923, 1.5),
(1546387200, 187924, 250),
(1546387200, 187926, 120),
(1546387200, 187927, 125),
(1546473600, 187923, 1.5),
(1546473600, 187924, 275),
(1546473600, 187926, 120),
(1546473600, 187927, 137.5),
(1546560000, 187923, 1.75)
) as t (datetime, trace, value)
$$
) as final_result (
unixdatetime int,
trace1 decimal,
trace2 decimal,
trace3 decimal,
trace4 decimal
);
select * from crosstab(
$$
select * from (values
(1546300800, 187923, 1.5::double precision),
(1546300800, 187924, 200::double precision),
(1546300800, 187926, 120::double precision),
(1546300800, 187927, 100::double precision),
(1546387200, 187923, 1.5::double precision),
(1546387200, 187924, 250::double precision),
(1546387200, 187926, 120::double precision),
(1546387200, 187927, 125::double precision),
(1546473600, 187923, 1.5::double precision),
(1546473600, 187924, 275::double precision),
(1546473600, 187926, 120::double precision),
(1546473600, 187927, 137.5::double precision),
(1546560000, 187923, 1.75::double precision)
) as t (datetime, trace, value)
$$
) as final_result (
unixdatetime int,
trace1 double precision,
trace2 double precision,
trace3 double precision,
trace4 double precision
);
两种情况下的结果如下:
unixdatetime | trace1 | trace2 | trace3 | trace4
--------------+--------+--------+--------+--------
1546300800 | 1.5 | 200 | 120 | 100
1546387200 | 1.5 | 250 | 120 | 125
1546473600 | 1.5 | 275 | 120 | 137.5
1546560000 | 1.75 | | |
(4 rows)
答案 1 :(得分:1)
谢谢你们的帮助! Ancoron的解决方案运行完美。
关于第二个解决方案,我们无需像下面的代码那样将每行中的每个值显式转换为双精度,而是可以将类型转换应用于列名:
select * from crosstab(
$$
select datetime, trace, value::double precision from
(values
(1546300800, 187923, 1.5),
(1546300800, 187924, 200),
(1546300800, 187926, 120),
(1546300800, 187927, 100),
(1546387200, 187923, 1.5),
(1546387200, 187924, 250),
(1546387200, 187926, 120),
(1546387200, 187927, 125),
(1546473600, 187923, 1.5),
(1546473600, 187924, 275),
(1546473600, 187926, 120),
(1546473600, 187927, 137.5),
(1546560000, 187923, 1.75)
) as t (datetime, trace, value)
$$
)
as final_result (
unixdatetime int,
trace1 double precision,
trace2 double precision,
trace3 double precision,
trace4 double precision
)