对返回记录中的字段进行排序查询

发布于 2024-12-27 08:17:28 字数 1199 浏览 1 评论 0原文

我有一个在其 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春有你 2025-01-03 08:17:28

用于

ORDER BY (status).colorlevel

引用您的记录类型的列。

顺便说一句:我使用小写(colorlevel 而不是 colorLevel),因为如果不加双引号,标识符就会转换为小写,并且使用混合大小写标识符通常是在 PostgreSQL 中是个坏主意。


至于您的附加问题,类似的语法要求。我还使用子查询来优化查询:

SELECT id
     , name
     , (x.status).status
     , (x.status).toelichting
     , (x.status).colorLevel
FROM   tabel
     , (SELECT getStatus(now(), now(), now()) as status) x
ORDER  BY (x.status).colorlevel

阅读有关访问复合类型< /a> 在手册中。


在附加输入后回答

要使用表中的列,请将其全部放入子查询中。我试图避免多次调用该函数,因为这可能会很昂贵。

SELECT
    id,
    name,
    (status).status,
    (status).toelichting,
    (status).colorLevel
FROM (
    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 ...
    ) x
ORDER BY
    (status).colorlevel

Use

ORDER BY (status).colorlevel

to reference a column of your record type.

As an aside: I used lower case(colorlevel instead of colorLevel) 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:

SELECT id
     , name
     , (x.status).status
     , (x.status).toelichting
     , (x.status).colorLevel
FROM   tabel
     , (SELECT getStatus(now(), now(), now()) as status) x
ORDER  BY (x.status).colorlevel

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.

SELECT
    id,
    name,
    (status).status,
    (status).toelichting,
    (status).colorLevel
FROM (
    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 ...
    ) x
ORDER BY
    (status).colorlevel
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文