FreakDJ
01/15/2022, 12:32 AMselect fetch_data()
it says "structure of query does not match function result type"
create or replace function fetch_data()
returns setof "SolanaFloorTracker"
language plpgsql
as $$
begin
return query
SELECT rank_filter.*
FROM (
SELECT "SolanaFloorTracker".*,
rank() OVER (
PARTITION BY "CollectionName"
ORDER BY created_at DESC
)
FROM "SolanaFloorTracker"
) rank_filter WHERE RANK <=2;
end;
$$
I also tried returns setof record
but the output is set-valued function called in context that cannot accept a set. I tested the inner SQL query and it works, so I think I just need to learn about what I need to return to get this to work properly?FreakDJ
01/15/2022, 10:04 PMtourdownunder
01/15/2022, 10:20 PMsql
CREATE TABLE IF NOT EXISTS "SolanaFloorTracker"
(
id INT,
"CollectionName" TEXT,
CREATED_AT TIMESTAMP
);
INSERT INTO "SolanaFloorTracker"(id, "CollectionName", Created_at)
VALUES
(1,'A', '2010-01-19')
, (3,'B', '2010-09-18')
, (4,'A', '2021-05-15')
, (4,'A', '2021-07-25');
-- drop FUNCTION f_foo();
CREATE OR REPLACE FUNCTION f_foo() -- (open_id numeric) -- parameter not used
RETURNS TABLE (id INT,
"CollectionName" TEXT,
CREATED_AT TIMESTAMP, rank bigint) AS
$func$
BEGIN
RETURN QUERY SELECT rank_filter.id, rank_filter."CollectionName", rank_filter.Created_at, rank_filter.rank
FROM (
SELECT sft.*,
rank() OVER (
PARTITION BY sft."CollectionName"
ORDER BY sft.created_at DESC
) as rank
FROM "SolanaFloorTracker" sft
) rank_filter WHERE rank_filter.rank <=2;
END
$func$ LANGUAGE plpgsql;
select * from f_foo()
tourdownunder
01/15/2022, 10:22 PMtourdownunder
01/15/2022, 10:23 PMSolanaFloorTracker
tourdownunder
01/15/2022, 10:24 PMtourdownunder
01/15/2022, 10:24 PMFreakDJ
01/16/2022, 12:56 AMcreate or replace function fetch_data()
returns table ( id INT, "CollectionName" TEXT, created_at timestamp, "FloorPrice" json, "Volume" json, rank bigint)
as $func$
begin
return query
SELECT rank_filter.id, rank_filter.created_at, rank_filter."CollectionName", rank_filter."FloorPrice", rank_filter."Volume", rank_filter.rank
FROM (
SELECT sft.*,
rank() OVER (
PARTITION BY sft."CollectionName"
ORDER BY sft.created_at DESC
) as rank
FROM "SolanaFloorTracker" sft
) rank_filter WHERE rank_filter.RANK <=2;
end;
$func$ language plpgsql
However, when I run select * from fetch_data()
it returns "structure of query does not match function result type"garyaustin
01/16/2022, 1:02 AMFreakDJ
01/16/2022, 1:08 AMFreakDJ
01/16/2022, 1:09 AMFreakDJ
01/16/2022, 1:13 AMsql
create or replace function fetch_data()
returns table ( id INT, created_at timestamp, "CollectionName" TEXT, "FloorPrice" json, "Volume" json, rank bigint)
as $func$
begin
return query
SELECT rank_filter.id, rank_filter.created_at, rank_filter."CollectionName", rank_filter."FloorPrice", rank_filter."Volume", rank_filter.rank
FROM (
SELECT sft.*,
rank() OVER (
PARTITION BY sft."CollectionName"
ORDER BY sft.created_at DESC
) as rank
FROM "SolanaFloorTracker" sft
) rank_filter WHERE rank_filter.RANK <=2;
end;
$func$ language plpgsql
Which should match up with the main table, including types: https://gyazo.com/3c0a759c884735747330406977b58041FreakDJ
01/16/2022, 1:14 AMgaryaustin
01/16/2022, 1:16 AMFreakDJ
01/16/2022, 1:16 AMFreakDJ
01/16/2022, 1:17 AMgaryaustin
01/16/2022, 1:17 AMgaryaustin
01/16/2022, 1:20 AMFreakDJ
01/16/2022, 1:24 AMFreakDJ
01/16/2022, 1:24 AMFreakDJ
01/16/2022, 1:25 AMgaryaustin
01/16/2022, 1:25 AM