我是PL / pgsql的新手,我想创建一个新的函数。
就我而言,我有这张桌子:
Column 1: VARIABLE = 2 2 2 2 2 2 2 2 2 2 Column 2: VALUE = 20 20 20 20 180 180 180 180 180 180 Column3: STRETCH = 1 1 1 1 1 1 2 2 2 2
我想做一个函数来从字段VALUE创建N个数组,其中STRETCH是不同的,例如:
Array 1: 20,20,20,20,180,180, Array 2: 180,180,180,180,
我该怎么做?
答案 0 :(得分:1)
这是提供测试用例的有用方法:
CREATE TABLE tbl (variable int, value real, stretch int);
INSERT INTO tbl VALUES
(2, 20, 1)
,(2, 20, 1)
,(2, 20, 1)
,(2, 20, 1)
,(2, 180, 1)
,(2, 180, 1)
,(2, 180, 2)
,(2, 180, 2)
,(2, 180, 2)
,(2, 180, 2);
SELECT variable, stretch, array_agg(value) AS val_arr
FROM tbl
GROUP BY 1,2
ORDER BY 1,2;
我想这样做,因为那时我想得到我的所有数组 计算每个的平均值。
SELECT variable, stretch, avg(value) AS val_avg
FROM tbl
GROUP BY 1,2
ORDER BY 1,2;
Aggregate functions in the manual.
Try a search to find plenty of code examples how to create a function.