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 7411613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:24:46+00:00 2026-05-29T06:24:46+00:00

It is very common for a web page to have multiple ordering options for

  • 0

It is very common for a web page to have multiple ordering options for a table. Right now I have a case where there are 12 options (ordenable columns). The easiest (that I know of) way to do it is to build the SQL query concatenating strings. But I’m wondering if it is the best approach. The string concatenation is something like this (python code):

order = {
    1: "c1 desc, c2",
    2: "c2, c3",
    ...
    12: "c10, c9 desc"
    }
...
query = """
select c1, c2
from the_table
order by %(order)s
"""
...
cursor.execute(query, {'order': AsIs(order[order_option])})
...

My alternative solution until now is to place a series of cases in the order by clause:

select c1, c2
from the_table
order by
    case %(order_option)s
        when 1 then array[c1 * -1, c2]
        when 2 then array[c2, c3]
        else [0.0, 0.0]
    end
    ,
    case %(order_option)s
        when 3 then c4
        else ''
    end
    ,
    ...
    ,
    case when %(order_option)s < 1 or %(order_option)s > 12 then c5 end
;

What is the best practice concerning multiple ordering choices? What happens with index utilization in my alternative code?

  • 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-29T06:24:47+00:00Added an answer on May 29, 2026 at 6:24 am

    First of all, @order is not valid PostgreSQL syntax. You probably borrowed the syntax style from MS SQL Server or MySQL. You cannot use variables in a plain SQL query like that.

    In PostgreSQL you would probably create a function. You can use variables there, just drop the @.

    Sorting by ARRAY is generally rather slow – and not necessary in your case. You could simplify to:

    ORDER  BY
           CASE _order
              WHEN 1 THEN c2
              WHEN 2 THEN c3 * -1
              ELSE NULL  -- undefined!
           END
         , c1
    

    However, a CASE expression like this cannot use plain indexes. So, if you are looking for performance, one way (of several) would be a plpgsql function like this:

    CREATE OR REPLACE FUNCTION foo(int)
      RETURNS TABLE(c1 int, c2 int) AS
    $BODY$
    BEGIN
    
    CASE $1
    WHEN 1 THEN
        RETURN QUERY
        SELECT t.c1, t.c2
        FROM   tbl t
        ORDER  BY t.c2, t.c1;
    
    WHEN 2 THEN
        RETURN QUERY
        SELECT t.c1, t.c2
        FROM   tbl t
        ORDER  BY t.c3 DESC, t.c1;
    ELSE
        RAISE WARNING 'Unexpected parameter: "%"', $1;
    END CASE;
    
    END;
    $BODY$
      LANGUAGE plpgsql STABLE;
    

    This way, even plain indexes can be used.

    If you actually only have two alternatives for ORDER BY, you could also just write two
    functions.

    Create multi-column indexes on (c2, c1) and (c3 DESC, c1) for maximum performance. But be aware that maintaining indexes carries a cost, too, especially if your table sees a lot of write operations.


    Additional answer for rephrased question

    As I said, the CASE construct will not use plain indexes. Indexes on expressions would be an option, but what you have in your example is outside the scope.

    So, if you want performance, build the query in your app (your first approach) or write a server side function (possibly with dynamic SQL and EXECUTE) that does something similar inside PostgreSQL. The WHERE clause with a complex CASE statement works, but is slower.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing an embedded web server. Now, I have an access validation page
I have a very common situation. I have a file, and I need to
I have a very common situation here. And for years I haven't found if
This is a very common scenario: displaying images in a ListView which have to
I have a ASP.Net web page with a grid view. I want to filter
With ASP.NET MVC, it is common to have AJAX code(e.g. jQuery) to invoke web
Ok, this is an very common scenario in web dev, with a very common
Trying to avoid re-inventing the wheel here. I have a Google Web Toolkit page
Folks, I have a feeling there's a classic design pattern that covers this case,
I have a web form page Default.aspx, which inherits from a BasePage class that

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.