将功能单独返回为不同的列

时间:2019-07-16 13:53:14

标签: postgresql

我有这个Postgres / fucntion,它以参数user_id和其Universe作为参数,并返回其coutry,区域和存储。

CREATE OR REPLACE FUNCTION get_user_details(
   user_id uuid,
   user_universe_id uuid,
   OUT country text,
   OUT region text,
   OUT store text
   )
AS $$
DECLARE
    user_group_id uuid;
    user_group_path ltree;
begin
    -- lets select the group_id first
    -- in case of user belong to multiple groups we chose the longest one
    select into user_group_id ug.group_id from user_groups ug, groups_tree gt where gt.id = ug.group_id and ug.u_id = user_id and ug.universe_id = user_universe_id 
    order by gt.hierarchy_degree desc limit 1 ;

    -- lets slect the group path
    SELECT INTO user_group_path path from groups_tree where id =  user_group_id and universe_id = user_universe_id ;
    -- detect the country from this query
    select into country name from groups_tree where hierarchy_degree = 1 and  path @> user_group_path and universe_id = user_universe_id  ;

    -- detect the country from this query
    select into region name from groups_tree where hierarchy_degree = 2 and  path @> user_group_path and universe_id = user_universe_id  ;

    -- detect the country from this query
    select into store name from groups_tree where hierarchy_degree = 3 and  path @> user_group_path and universe_id = user_universe_id  ;


END; $$
LANGUAGE plpgsql;

此函数返回以下数据格式:

get_user_details|
----------------|
(Edzo-team,Dev,)|

我想要一个技巧,以便可以跨多列打印数据 预期结果

country        |region        |store      |
----------------|--------------|-----------
(Edzo-team)     |(Dev)         |

0 个答案:

没有答案