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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:02:29+00:00 2026-05-27T22:02:29+00:00

I have come across this piece of ‘voodoo’ SQL which is being used to

  • 0

I have come across this piece of ‘voodoo’ SQL which is being used to perform custom grouping of data from a table. I would like to understand how it does it magic, but I am unable to grok it. Can a SQL expert out there explain in simple English to someone who is not very SQL literate, the various parts of this snippet, which allows it to do its magic?

select ceil(rnk/10.0) as grp,
       col1, col2, col3, col4, col5, col6, col7
from (select e.col1, e.col2, e.col3, e.col4, e.col5, e.col6, e.col7,
             (select count(*) 
              from mytable d
              where e.col1 > d.col1)+1 as rnk
      from mytable e) x
order by grp;

The part of the SQL above that I can’t seem to get my head around is the inner SQL that returns the column ‘x’:

(select count(*) from mytable d
                where e.col1 > d.col1)+1 as rnk
                from mytable e
                ) x

I would expect to be able to run that query by itself:

select count(*) from mytable d
                where e.col1 > d.col1)+1 as rnk
                from mytable e

However, when I do that, I get the error:

ERROR: syntax error at or near “+” LINE 2: where
e.col1 > d.col1)+1 as rnk

So, what’s going on there?!

Also, the current SQL is hard coded with the number 10. I would like to wrap a function around it, in order to be able to call the function with numbers other than 10.

The backend database is PostgreSQL, so the function will be in PL/pgSQL. Here is my first attempt at writing such a function – however, this is not quite correct, as I want to return multiple rows of the specified columns – so the function below needs to be modified somewhat, not entirely sure how:

CREATE OR REPLACE FUNCTION my_custom_grouping(in integer, 
                                              out grp integer,
                                              out col1 double,
                                              out col2 double,
                                              out col3 double,
                                              out col4 double,
                                              out col5 double,
                                              out col6 double,
                                              out col7 double
                                              )

    AS $$ SELECT 
       ceil(rnk/$1) as grp,
                col1, col2, col3, col4, col5, col6, col7
                from (
                select e.col1, e.col2, e.col3, e.col4, e.col5, e.col6, e.col7,
                (select count(*) from mytable d
                where e.col1 > d.col1)+1 as rnk
                from mytable e
                ) x
                order by grp;
    $$
    LANGUAGE SQL;

Apart from the function not returning more than one row, I’m not sure if this is the best way to parametrize the query – am I on the correct path? – if yes, how would I modify the function above to return multiple rows instead of the current single “row” (i.e. “multiple column” output)?

Is that the correct way to run aggregate functions (grouped by ‘grp’) on the data returned from the 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-27T22:02:30+00:00Added an answer on May 27, 2026 at 10:02 pm

    Your (simplified!) function could look like this:

    CREATE OR REPLACE FUNCTION my_custom_grouping(integer)
    RETURNS TABLE (
       grp integer,
       col1 double precision,
       col2 double precision,
       col3 double precision,
       col4 double precision,
       col5 double precision,
       col6 double precision,
       col7 double precision) AS
    $BODY$
        SELECT ceil(rank() OVER (ORDER BY col1) / $1)::int as grp
              ,col1, col2, col3, col4, col5, col6, col7
        FROM   mytable 
        ORDER  BY 1;
    $BODY$ LANGUAGE SQL;
    

    Major points:

    • Note that this is language SQL, so not a PL/pgSQL function. You could use language plpgsql, too, but that’s not necessary here.

    • I replaced the core of your voodoo with the window function rank(), which should do the same exactly, just simpler.

    • I also removed the subquery altogether. It is not necessary.

    • The type double is called double precision in PostgreSQL.

    • To return multiple rows, define a function as RETURNS SETOF record or RETURNS TABLE as I did.

    • ORDER BY can use positional parameters, so you do not have to spell out the calculation of the first column again: ORDER BY 1.
      However, multiple rows in the same grp. Add more columns or expressions to the ORDER BY clause to arrive at a stable sort order.

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

Sidebar

Related Questions

I have to edit a PL/SQL function. I have come across this line which
I'm trying to figure out someone else's code and have come across this piece
I have an simple question (?) about SQL. I have come across this problem
I have come across this problem several times in which I would like to
I have come across this piece of code (I'm trying to include all details
I have come across this great function/command . Colour to RGB, you can do
My co-worker and I have come across this warning message a couple times recently.
I'm reviewing my old algorithms notes and have come across this proof. It was
I am using $SUB for the first time and have come across this problem.
I have never come across this issue but most recently I noticed that a

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.