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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:21:07+00:00 2026-05-27T02:21:07+00:00

We’re currently migrating our database back-end from Firebird to PostgreSQL. We use NHibernate as

  • 0

We’re currently migrating our database back-end from Firebird to PostgreSQL. We use NHibernate as an ORM and the transition has been quite painless albeit a few differences in SQL.

We have one query which we execute using NHibernate.CreateSQLQuery as we build the SQL ourselves. The query returns a string List of the most 5 most recently accessed Ninja names for the passed in UserID.

This was no problem in Firebird using the following SQL;

 SELECT FIRST 5 DISTINCT NI.NINJA_NAME
 FROM NINJA_ACCESS NA 
      INNER JOIN NINJAS NI ON NA.NINJA_ID = NI.NINJA_ID
 WHERE NA.USER_ID = 1
 ORDER BY NI.NINJA_ACCESS_DATE DESC

Returning an ordered (by access date descending) list of strings.

Now in PostgreSQL we are encountering problems with this simple query;

 SELECT DISTINCT ON (NI.NINJA_NAME) NI.NINJA_NAME
 FROM NINJA_ACCESS NA 
      INNER JOIN NINJAS NI ON NA.NINJA_ID = NI.NINJA_ID
 WHERE NA.USER_ID = 1
 ORDER BY NI.NINJA_ACCESS_DATE DESC
 LIMIT 5

It appears that we cannot return a single “distinct” column of data with ordering being performed on a separate column that isn’t included in the select. Any variation in the above SQL doesn’t work unless we include the Access_Date column in the select.

Any ideas how we can resolve this simple query in PostgreSQL?

Side-note: This is an important query for our Ninja Management System, the Ninja’s insist that it work!!!

  • 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-27T02:21:08+00:00Added an answer on May 27, 2026 at 2:21 am

    This query should be the equivalent:

    SELECT n.ninja_name
          ,max(n.ninja_access_date) AS max_access_date
    FROM   ninja_access na 
    JOIN   ninjas n USING (ninja_id)
    WHERE  na.user_id = 1
    GROUP  BY n.ninja_name
    ORDER  BY 2 DESC
    LIMIT  5;
    
    • ORDER BY 2 is just shorthand for ORDER BY max(n.ninja_access_date).
    • We need the max(n.ninja_access_date) to get the most recent entry per ninja_name.
      And sort descending to get the most recent names.
    • JOIN ninjas n USING (ninja_id) is shorthand for
      JOIN ninjas n ON n.ninja_id = na.ninja_id

    You don’t have to include the access date in the SELECT list. To only get the names:

    SELECT n.ninja_name
    FROM   ninja_access na 
    JOIN   ninjas ni USING (ninja_id)
    WHERE  na.user_id = 1
    GROUP  BY n.ninja_name
    ORDER  BY max(n.ninja_access_date) DESC
    LIMIT  5;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
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 to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace

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.