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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:23:32+00:00 2026-06-03T10:23:32+00:00

In postgres, you can query for the first value of in a group with

  • 0

In postgres, you can query for the first value of in a group with DISTINCT ON. How can this be achieved in Oracle?

From the postgres manual:

SELECT DISTINCT ON ( expression [, …] ) keeps only the first row of
each set of rows where the given expressions evaluate to equal. The
DISTINCT ON expressions are interpreted using the same rules as for
ORDER BY (see above). Note that the “first row” of each set is
unpredictable unless ORDER BY is used to ensure that the desired row
appears first.

For example, for a given table:

 col1 | col2 
------+------
 A    | AB
 A    | AD
 A    | BC
 B    | AN
 B    | BA
 C    | AC
 C    | CC

Ascending sort:

> select distinct on(col1) col1, col2 from tmp order by col1, col2 asc;
 col1 | col2 
------+------
 A    | AB
 B    | AN
 C    | AC

Descending sort:

> select distinct on(col1) col1, col2 from tmp order by col1, col2 desc;
 col1 | col2 
------+------
 A    | BC
 B    | BA
 C    | CC
  • 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-06-03T10:23:33+00:00Added an answer on June 3, 2026 at 10:23 am

    The same effect can be replicated in Oracle either by using the first_value() function or by using one of the rank() or row_number() functions.

    Both variants also work in Postgres.

    first_value()

    select distinct col1, 
    first_value(col2) over (partition by col1 order by col2 asc)
    from tmp
    

    first_value gives the first value for the partition, but repeats it for each row, so it is necessary to use it in combination with distinct to get a single row for each partition.

    row_number() / rank()

    select col1, col2 from (
      select col1, col2, 
      row_number() over (partition by col1 order by col2 asc) as rownumber 
      from tmp
    ) foo
    where rownumber = 1
    

    Replacing row_number() with rank() in this example yields the same result.

    A feature of this variant is that it can be used to fetch the first N rows for a given partition (e.g. “last 3 updated”) simply by changing rownumber = 1 to rownumber <= N.

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

Sidebar

Related Questions

How can I do such query in Postgres? IF (select count(*) from orders) >
Can someone help me convert this sqlite query to a postgres query? SELECT count(*),
The query is basically: SELECT DISTINCT my_table.foo from my_table WHERE... Pretending that I'm 100%
In postgres I am fairly sure you can do something like this SELECT authors.stage_name,
This is my query: SELECT autor.entwickler,anwendung.name FROM autor left join anwendung on anwendung.name =
The below Postgres SQL query is returning all records available in the table. Can
How can I execute a SQL query from a Rails application to a MySQL
In Postgres you can switch on query logging, resulting in a file containing all
How can you use parameters in Postgres Like command? I am reading this document
I have a Postgres 9.0 query returning results in a way similar to this:

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.