Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从函数PostgreSQL而不是记录返回多个列和行_Sql_Postgresql_Plpgsql_Postgresql 9.6 - Fatal编程技术网

从函数PostgreSQL而不是记录返回多个列和行

从函数PostgreSQL而不是记录返回多个列和行,sql,postgresql,plpgsql,postgresql-9.6,Sql,Postgresql,Plpgsql,Postgresql 9.6,我在网上读到了关于PostgreSQL函数和返回结果的文章 在此链接中: 我编写了这个函数: 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_numbe

我在网上读到了关于PostgreSQL函数和返回结果的文章 在此链接中:

  • 我编写了这个函数:

    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($1,accountId);
    $BODY$
    LANGUAGE sql;
    
    它可以工作并在单列类型的记录中返回结果。 请注意,可能会返回多行

    现在的答复是:

    record
    (1172,1172,1011,0)
    (1172,1412,10,40)
    .....
    
    我希望我的结果不是作为记录,而是作为多个列

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

    是否有一种方法可以从PostgreSQL函数返回多个列

    通过此查询,我可以按预期看到它:

    SELECT * FROM brand_hierarchy (id)
    

    返回表(或集合)的函数应在FROM子句中使用:

    select * 
    from brand_hierarchy(1234)
    

    我找到了这个函数
    crosstab
    我想这就是你要找的

    此语法可能存在性能问题。对任何输出列调用brand_hierarchy函数一次