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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:07:28+00:00 2026-05-30T12:07:28+00:00

I am working on a postgresql query that i am not sure how to

  • 0

I am working on a postgresql query that i am not sure how to produce the output.

Lets say i have a sql query whose output i want is

         name    date     visit_number  visit
          x    2011-01-01     123         ??   (value i want=1)
          y    2011-01-01     123         ??   (value i want=empty)
          a    2011-02-02     345         ??   (value i want=1) 
          b    2011-02-02     345         ??    (empty)
          c    2011-02-02     345         ??     (empty)

currently my sql query contains all the values except the last column visit. I want the visit column to work this way…if visit_number contains same value for multiple rows, i want the column visit to show the value 1 for the first row and just null or empty for the remaining rows where the visit_number is the same. How do i do that???

i could write the sample query in any way.it could simply be :

    select name,date,visit_number from sometable order by date;

I am using postgres 8.1 version.

Thanks

  • 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-30T12:07:30+00:00Added an answer on May 30, 2026 at 12:07 pm

    The first thing you should do is upgrade to a modern day version of PostgreSQL. Version 8.1 has reached end of life in November 2010.

    In a more recent version you can conveniently solve this with window functions:

    SELECT name, date, visit_number
         , CASE WHEN row_number() OVER (PARTITION BY visit_number
                                        ORDER BY date, name) = 1
              THEN 1
              ELSE NULL
           END AS visit
    FROM   tbl
    ORDER  BY date, name;
    

    I ordered by name additionally to break ties.


    For versions before PostgreSQL 8.4, this query should work (untested):

    SELECT name, date, visit_number
         , CASE WHEN EXISTS (
             SELECT *
             FROM   tbl t1
             WHERE  t1.visit_number =  tbl.visit_number -- more to make it unique?
             AND    t1.date <= tbl.date -- or more columns to make order unambiguous
             AND    t1.name <  tbl.name
             )
           THEN NULL ELSE 1 END AS visit
    FROM   tbl
    ORDER  BY date, name;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am thinking of working on a Rails application that uses PostgreSQL. I have
I have SQL query, which is working nice on Oracle and MSSQL. Now I'm
I have a criteria query not really working. Here the model: @Entity public class
I have been working with PostgreSQL, playing around with Wikipedia's millions of hyperlinks and
I'm working on PostgreSQL 8.4 in read committed mode. I know that for each
I'm more of a MS-SQL / PostgreSQL guy, but I'm working on a MySQL
I have a table I'm working on that has around 3 million tuples. It
I am working on a search query in PostgreSQL, and one of the things
I have a sql function that does a simple sql select statement: CREATE OR
I'm working on a PostgreSQL 8.1 SQL script which needs to delete a large

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.