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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:16:31+00:00 2026-06-10T18:16:31+00:00

I am trying to solve an interesting problem. I have a table that has,

  • 0

I am trying to solve an interesting problem. I have a table that has, among other data, these columns (dates in this sample are shown in European format – dd/mm/yyyy):

n_place_id   dt_visit_date
(integer)    (date)
==========   =============
   1           10/02/2012
   3           11/03/2012
   4           11/05/2012
  13           14/06/2012
   3           04/10/2012
   3           03/11/2012
   5           05/09/2012
  13           18/08/2012

Basically, each place may be visited multiple times – and the dates may be in the past (completed visits) or in the future (planned visits). For the sake of simplicity, today’s visits are part of planned future visits.

Now, I need to run a select on this table, which would pull unique place IDs from this table (without date) sorted in the following order:

  1. Future visits go before past visits
  2. Future visits take precedence in sorting over past visits for the same place
  3. For future visits, the earliest date must take precedence in sorting for the same place
  4. For past visits, the latest date must take precedence in sorting for the same place.

For example, for the sample data shown above, the result I need is:

 5     (earliest future visit)
 3     (next future visit into the future)
13     (latest past visit)
 4     (previous past visit)
 1     (earlier visit in the past)

Now, I can achieve the desired sorting using case when in the order by clause like so:

select
    n_place_id
from
    place_visit
order by
    (case when dt_visit_date >= now()::date then 1 else 2 end),
    (case when dt_visit_date >= now():: date then 1 else -1 end) * extract(epoch from dt_visit_date)

This sort of does what I need, but it does contain repeated IDs, whereas I need unique place IDs. If I try to add distinct to the select statement, postgres complains that I must have the order by in the select clause – but then the unique won’t be sensible any more, as I have dates in there.

Somehow I feel that there should be a way to get the result I need in one select statement, but I can’t get my head around how to do it.

If this can’t be done, then, of course, I’ll have to do the whole thing in the code, but I’d prefer to have this in one SQL statement.

P.S. I am not worried about the performance, because the dataset I will be sorting is not large. After the where clause will be applied, it will rarely contain more than about 10 records.

  • 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-10T18:16:33+00:00Added an answer on June 10, 2026 at 6:16 pm

    Try this

     select n_place_id
     from 
     (
        select *, 
        extract(epoch from (dt_visit_date - now())) as seconds,  
        1 - SIGN(extract(epoch from (dt_visit_date - now())) ) as futurepast
        from #t
     ) v
     group by n_place_id
     order by max(futurepast) desc, min(abs(seconds))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interesting problem that I'm trying to solve and was wondering if
I have an interesting problem here I've been trying to solve for the last
I'm trying to solve the following problem in Redis. I have a list that
I am trying to solve this problem http://www.spoj.pl/problems/PEBBMOV/ . I think I have the
I'm trying to solve the following problem: I have a download button that when
I have been trying to solve this problem for three days now, it's really
Trying to solve a problem with templatetags. I have two templatetags: @register.inclusion_tag('directory/_alphabet.html') def alphabet_list(names):
Trying to solve this problem . I would like to learn how the bootstrapper
I am trying to solve the following problem with Puppet: I have multiple nodes.
I'm trying to solve this particular problem using c++. Problem: Given a set of

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.