Return 来自 PostgreSQL 函数而不是记录的多列和多行

Return multiple columns and rows from a function PostgreSQL instead of record

我正在网上阅读有关 PostgreSQL 上的函数和 return 的结果 在这个链接中:

  1. SQL function return-type: TABLE vs SETOF records
  2. How do I reference named parameters in Postgres sql functions?
  3. http://www.postgresqltutorial.com/plpgsql-function-returns-a-table/

我写了这个函数:

create or replace function brand_hierarchy(account_value int)
  RETURNS table (topID INTEGER, accountId INTEGER, liveRowCount bigint,archiveRowCount bigint)
  AS
$BODY$
  SELECT * FROM my_client_numbers
where accountId  = coalesce(,accountId);
$BODY$
LANGUAGE sql;

有效并且 return 结果在单列记录类型中。 请注意,可能不止一行 return.

现在的响应是:

record
(1172,1172,1011,0)
(1172,1412,10,40)
.....

我希望我的结果不是作为记录而是作为多列

|---------|---------|------------|----------------|
| topID   |accountId|liveRowCount|archiveRowCount |
|---------|---------|------------|----------------|
| 1172    |1172     | 1011       |  0             |
| 1172    |1412     | 10         |  40            |

有没有办法从 PostgreSQL 函数return多列

我能够通过此查询按预期看到它:

SELECT * FROM brand_hierarchy (id)

返回 table(或 setof)的函数应在 FROM 子句中使用:

select * 
from brand_hierarchy(1234)

我找到了这个函数 crosstab 我想这就是你要找的 https://www.postgresql.org/docs/9.3/tablefunc.html