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

  • Home
  • SEARCH
  • 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 6760909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:07:16+00:00 2026-05-26T14:07:16+00:00

I am using PostgreSQL version 9.1 and looking at the Postgres docs , I

  • 0

I am using PostgreSQL version 9.1 and looking at the Postgres docs, I know it is possible to do the following:

SELECT salary, sum(salary) OVER (ORDER BY salary) FROM empsalary;

and this works for my queries.

Now I need to be able to specify an alias instead of a column name in the OVER (ORDER BY ...) ?

I have tried this:

EDIT: I previously forgot to add the rank() to this query:

SELECT salary, <complex expression> as result, rank() OVER (ORDER BY result) FROM empsalary;

and I am getting an error saying that column "result" does not exist.

Is it possible to specify an alias instead of a column name here? Am I missing some special syntax?

EDIT:

I am using Hibernate, with some native SQL for the window function. The full SQL that is generated and executed is below:

select 
       rank() OVER (ORDER BY deltahdlcOverruns DESC) as rank, 

       this_.deviceNo as y1_, 

       (SELECT _dev_.hdlcOverruns FROM abstractperformancestatistic _abs_ 
        INNER JOIN enddevicestatistic _dev_ ON _dev_.id = _abs_.id 
        INNER JOIN linkstatistic _link_ ON _link_.id = _dev_.linkStatistic_id 
        INNER JOIN iptstatistic _ipt_ ON _ipt_.id = _link_.iptStat_id 
        WHERE this_.deviceNo=_dev_.deviceNo AND _abs_.dateTime <= '3910-06-07 00:00:00.0'
        ORDER BY _abs_.dateTime DESC LIMIT 1
       ) 
       - 
       (SELECT _dev_.hdlcOverruns FROM abstractperformancestatistic _abs_ 
        INNER JOIN enddevicestatistic _dev_ ON _dev_.id = _abs_.id 
        INNER JOIN linkstatistic _link_ ON _link_.id = _dev_.linkStatistic_id 
        INNER JOIN iptstatistic _ipt_ ON _ipt_.id = _link_.iptStat_id 
        WHERE this_.deviceNo=_dev_.deviceNo AND _abs_.dateTime >= '3870-06-01 00:00:00.0' 
        ORDER BY _abs_.dateTime LIMIT 1
       ) 
       AS deltahdlcOverruns from EndDeviceStatistic this_ 

       inner join AbstractPerformanceStatistic this_1_ on this_.id=this_1_.id 
       inner join AbstractEntity this_2_ on this_.id=this_2_.id 
       left outer join RawEndDeviceStatistic this_3_ on this_.id=this_3_.id 
       left outer join LinkStatistic l2_ on this_.linkStatistic_id=l2_.id 
       left outer join AbstractPerformanceStatistic l2_1_ on l2_.id=l2_1_.id 
       left outer join AbstractEntity l2_2_ on l2_.id=l2_2_.id 
       left outer join RawLinkStatistic l2_3_ on l2_.id=l2_3_.id 
       left outer join IPTStatistic i1_ on l2_.iptStat_id=i1_.id 
       left outer join AbstractPerformanceStatistic i1_1_ on i1_.id=i1_1_.id 
       left outer join AbstractEntity i1_2_ on i1_.id=i1_2_.id 
       left outer join RawIPTStatistic i1_3_ on i1_.id=i1_3_.id 

       where this_1_.dateTime between ? and ? 

       group by this_.deviceNo limit ?
  • 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-26T14:07:16+00:00Added an answer on May 26, 2026 at 2:07 pm

    Place the alias behind the OVER clause:

    SELECT salary
         , sum(salary) OVER (ORDER BY salary) AS my_alias
    FROM   empsalary;

    Edit after question update

    You cannot reference a column alias (an “output column”) at the same level of a SELECT. You need a subselect or a CTE for this. Like:

    SELECT id, result, rank() OVER (ORDER BY result) AS rnk
    FROM  (
        SELECT id, <complex expression> AS result
        FROM   tbl
        WHERE  <some condition>
        GROUP  BY id
        ) sub;
    

    For your query:

    SELECT rank() OVER (ORDER BY deltahdlcOverruns) AS rnk
         , y1_
         , deltahdlcOverruns
    FROM  (
        SELECT this_.deviceNo as y1_
            ,  (SELECT _dev_.hdlcOverruns FROM abstractperformancestatistic _abs_ 
                JOIN   enddevicestatistic _dev_ USING (id)
                JOIN   linkstatistic _link_ ON _link_.id = _dev_.linkStatistic_id 
                JOIN   iptstatistic _ipt_ ON _ipt_.id = _link_.iptStat_id 
                WHERE  this_.deviceNo=_dev_.deviceNo
                AND    _abs_.dateTime <= '3910-06-07 00:00:00.0'
                ORDER  BY _abs_.dateTime DESC
                LIMIT  1
               ) 
               - 
               (SELECT _dev_.hdlcOverruns FROM abstractperformancestatistic _abs_ 
                JOIN   enddevicestatistic _dev_ USING (id)
                JOIN   linkstatistic _link_ ON _link_.id = _dev_.linkStatistic_id 
                JOIN   iptstatistic _ipt_ ON _ipt_.id = _link_.iptStat_id 
                WHERE  this_.deviceNo=_dev_.deviceNo
                AND    _abs_.dateTime >= '3870-06-01 00:00:00.0' 
                ORDER  BY _abs_.dateTime
                LIMIT  1
               ) AS deltahdlcOverruns
        FROM   EndDeviceStatistic this_ 
        JOIN   AbstractPerformanceStatistic this_1_ USING (id)
        JOIN   AbstractEntity this_2_ USING (id)
        LEFT   JOIN RawEndDeviceStatistic this_3_ USING (id)
        LEFT   JOIN LinkStatistic l2_ ON this_.linkStatistic_id = l2_.id 
        LEFT   JOIN AbstractPerformanceStatistic l2_1_ ON l2_.id = l2_1_.id 
        LEFT   JOIN AbstractEntity l2_2_ ON l2_.id = l2_2_.id 
        LEFT   JOIN RawLinkStatistic l2_3_ ON l2_.id = l2_3_.id 
        LEFT   JOIN IPTStatistic i1_ ON l2_.iptStat_id = i1_.id 
        LEFT   JOIN AbstractPerformanceStatistic i1_1_ ON i1_.id = i1_1_.id 
        LEFT   JOIN AbstractEntity i1_2_ ON i1_.id = i1_2_.id 
        LEFT   JOIN RawIPTStatistic i1_3_ ON i1_.id = i1_3_.id 
        WHERE  this_1_.dateTime between ? and ? 
        GROUP  BY this_.deviceNo
        LIMIT  ?
    ) x;
    

    I made a few additional syntax simplifications.

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

Sidebar

Related Questions

is it possible to make something similar to the following with Postgresql without using
Using PostgreSQL triggers, is it possible to record the changes that have happened to
Just had a discussion at work about the merits of using PostgreSQL over MySQL
I am switching from using SQLite3 to PostgreSQL, and hoped that I could populate
I'm using a JMS queue to read from and insert data into a postgres
I'm loading a fixture generated from sqlite into postgresql database using manage.py loaddata. The
I've been using PostgreSQL a little bit lately, and one of the things that
Right now we are using PostgreSQL 8.3 (on Linux) as a database backend to
I am using PostgreSQL in a project I am working on, and one of
Since I'm using Postgresql and can't use LINQ to SQL, I wrote my own

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.