对返回记录中的字段进行排序查询
我有一个在其 select 子句中调用函数的查询。该函数返回一个记录类型。在调用查询中,我想按返回记录中的字段之一进行排序,如果可能的话,我还想将记录的字段作为调用查询的字段返回。为了清楚起见,这里是代码的简化版本:
CREATE OR REPLACE FUNCTION getStatus(lastContact timestamptz, lastAlTime timestamptz, lastGps timestamptz, out status varchar, out toelichting varchar, out colorLevel integer)
RETURNS record AS
$BODY$
BEGIN
status := 'controle_status_ok';
toelichting := '';
colorLevel := 3;
END
$BODY$
LANGUAGE 'plpgsql' VOLATILE
COST 100;
ALTER FUNCTION DMI_Controle_getStatus(timestamptz, timestamptz, timestamptz, out varchar, out varchar, out integer) OWNER TO xyz;
使用此函数,我想要一个像这样的查询:
SELECT
id,
name,
getStatus(tabel3.lastcontact, tabel4.lastchanged, tabel5.lastfound) as status
FROM
tabel1
left join tabel2 on ...
left join tabel3 on ...
left join tabel4 on ...
left join tabel5 on ...
ORDER BY
status
Postgres 出现以下错误:
错误:无法识别类型记录的排序运算符
提示:使用显式排序运算符或修改查询。
问题:我应该如何根据 getStatus 返回的 colorLevel 值进行排序?
附加问题:我可以在调用 getStatus 函数的查询字段中返回 getStatus 函数的三个字段吗?
I've got a query that calls a function in its select clause. The function returns a record type. In the calling query, I want to order by one of the fields in the returned record and if possible I'd also like to return the fields of the record as fields of the calling query. To make this clear, here's a simplified version of the code:
CREATE OR REPLACE FUNCTION getStatus(lastContact timestamptz, lastAlTime timestamptz, lastGps timestamptz, out status varchar, out toelichting varchar, out colorLevel integer)
RETURNS record AS
$BODY$
BEGIN
status := 'controle_status_ok';
toelichting := '';
colorLevel := 3;
END
$BODY$
LANGUAGE 'plpgsql' VOLATILE
COST 100;
ALTER FUNCTION DMI_Controle_getStatus(timestamptz, timestamptz, timestamptz, out varchar, out varchar, out integer) OWNER TO xyz;
Using this function, I want to have a query like this one:
SELECT
id,
name,
getStatus(tabel3.lastcontact, tabel4.lastchanged, tabel5.lastfound) as status
FROM
tabel1
left join tabel2 on ...
left join tabel3 on ...
left join tabel4 on ...
left join tabel5 on ...
ORDER BY
status
Postgres comes with the following error:
ERROR: could not identify an ordering operator for type record
HINT: Use an explicit ordering operator or modify the query.
The question: how should I order by the value of colorLevel that's been returned by getStatus?
Additional question: can I return the three fields of the getStatus function at fields of the query that calls the getStatus function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用于
引用您的记录类型的列。
顺便说一句:我使用小写(
colorlevel
而不是colorLevel
),因为如果不加双引号,标识符就会转换为小写,并且使用混合大小写标识符通常是在 PostgreSQL 中是个坏主意。至于您的附加问题,类似的语法要求。我还使用子查询来优化查询:
阅读有关访问复合类型< /a> 在手册中。
在附加输入后回答
要使用表中的列,请将其全部放入子查询中。我试图避免多次调用该函数,因为这可能会很昂贵。
Use
to reference a column of your record type.
As an aside: I used lower case(
colorlevel
instead ofcolorLevel
) because identifiers are cast to lower case if not double-quoted anyway, and using mixed case identifiers is generally a bad idea in PostgreSQL.As to your additional question, similar syntax requirement. I also use a subquery to optimize the query:
Read about accessing composite types in the manual.
Answer after additional input
To use columns from your tables, put it all in the a subquery. I am trying to avoid to call the function multiple times, because that may be expensive.