问题描述
------------------------------------------
| UserId | Timestamp | Key | Value |
------------------------------------------
99 | 1598603308 | Spo2 | 99
99 | 1598603318 | Spo2 | 98
99 | 1598603328 | Spo2 | 96
99 | 1598603338 | Spo2 | 97
...
...
99 | 1598603307 | Breath | 99
99 | 1598603311 | Breath | 98
99 | 1598603315 | Breath | 96
99 | 1598603319 | Breath | 97
...
这是我要编写的功能
CREATE OR REPLACE FUNCTION public.test(
"UserIds" uuid[])
RETURNS record
LANGUAGE 'plpgsql'
COST 100
VOLATILE
AS $BODY$
BEGIN
EXECUTE $$
select json_agg(rows) as "LatestRecords" from (
select "RowId"[1] as "UserId","RowId"[2] as "Timestamp","HeartRate","BreathRate","Sdnn","Temperature","Spo2","Plusstage"
from crosstab('select ''{"UserId","Timestamp"}''::text[],"Key","Value" from lrecords where "UserId" = ANY($1)')
AS ct("RowId" text[],"HeartRate" real,"BreathRate" real,"Sdnn" real,"Temperature" real,"Spo2" real,"Plusstage" real)) rows
$$ using "UserIds";
END;
$BODY$;
ALTER FUNCTION public.test(uuid[])
OWNER TO postgres;
我得到的错误是
ERROR: there is no parameter $1
LINE 1: ...ext[],"Value" from lrecords where "UserId" = ANY($1)
uuid数组确实可以在其他函数中工作,但是当其在crosstab
中时不起作用。我尝试将crosstab
中的语句更改为
'select ''{"UserId","Value" from lrecords where "UserId" = ANY('||$1||')
但没有帮助。
我不确定这里缺少什么。任何帮助表示赞赏:)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)