我已经编写了一个SQL查询,它在结果集中返回一个coulmn:
rate
此处有float
类型。
SELECT rate
FROM partner.exchange
WHERE id in (
(SELECT max(id)
FROM partner.exchange
WHERE currency_id = 1),
(SELECT max(id)
FROM partner.exchange
WHERE currency_id = 2),
(SELECT max(id)
FROM partner.exchange
WHERE currency_id = 3)
);
我需要编写plpgsql函数,将该结果放入float数组并返回一个。我怎么能这样做?
答案 0 :(得分:0)
由于查询的结果只有一列,因此可以将其行放入数组:
SELECT ARRAY(SELECT rate FROM ...[rest of subquery]);
要从SQL中获取此数组中的特定元素,比如第3个元素,您可以写:
SELECT s.x[3] FROM
(SELECT ARRAY(SELECT rate FROM ...[rest of subquery]) AS x)
AS s;
在程序风格上,这可能是:
DECLARE
x float[];
...
BEGIN
...
SELECT ARRAY(SELECT rate FROM ...[rest of subquery]) INTO x;
RETURN x[3];
END