Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7075513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:11:11+00:00 2026-05-28T06:11:11+00:00

I’ve got a query that calls a function in its select clause. The function

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:11:12+00:00Added an answer on May 28, 2026 at 6:11 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.