我在 hasura 中写了这个 postgres 函数
DROP FUNCTION IF EXISTS get_shipment_data(TEXT, DATE);
CREATE OR REPLACE FUNCTION get_shipment_data(carriercode TEXT, shipdate DATE)
RETURNS SETOF RECORD
AS $$ DECLARE r RECORD;
BEGIN FOR r IN (
SELECT view_group_by_shipday_filter.shipday AS shipday,
sum(view_group_by_shipday_filter.delivered) AS delivered,
sum(view_group_by_shipday_filter.transit) AS transit,
sum(view_group_by_shipday_filter.refused) AS refused,
sum(view_group_by_shipday_filter.undeliverable) AS undeliverable,
sum((view_group_by_shipday_filter.undeliverable + view_group_by_shipday_filter.refused)) AS total_exceptions FROM view_group_by_shipday_filter
WHERE (carriercode IS NULL OR view_group_by_shipday_filter.carriercode = carriercode) AND (shipdate IS NULL OR DATE(view_group_by_shipday_filter.shipdate) = shipdate)
GROUP BY view_group_by_shipday_filter.shipday )
LOOP RETURN NEXT r;
END LOOP;
RETURN;
END;
$$ LANGUAGE plpgsql;
当我在没有跟踪的情况下在 hasura 中运行它时它会执行但是当我跟踪它时它会给出错误
Inconsistent object: in function "get_shipment_data":
the function "get_shipment_data" cannot be tracked for the following reasons:
• the function does not return a "COMPOSITE" type
• the function does not return a table
上下文:我从一个基表创建了这个视图
view_group_by_shipday_filter
,该基表按carriercode
、shipdate
和shipday
分组。我必须在组中添加carriercode
、shipdate
因为我会基于它们进行过滤。我原以为它会在视图中给我独特的shipday
,但它正在创建重复的shipday
。
这就是为什么我试图在 function
之上创建一个 view
,以便在必要时应用过滤器并仅按 shipday
分组,这样我就可以获得独特的 shipday
.
我还在学习 postgres 函数,一般来说是 postgres。在这里停留了一段时间,我们将不胜感激。