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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:49:23+00:00 2026-05-16T04:49:23+00:00

I am trying to make SELECT statement for following situation and need help to

  • 0

I am trying to make SELECT statement for following situation and need help to make this SELECT statement. It’s SQL Server 2005.

When the select statement is run, it should return rows which have SentDate as NULL assuming that there are no duplicate PersonID in table. It will return result set with Status as ‘Initial Record’ (as the same person id was not sent already). Once data is exported, SentDate will have today’s date.

Now assume that two new row have been added and PersonID is duplicate. So when statement is run, it should return only newly added two records but status as ‘Follow up Record’ because same Person was exported in previous export job.

Here is the example of what is expected:

The initial table state would be:

RecordId  PersonId  SentDate  CreatedDate
1  1  NULL  8/8/2010
2  2  NULL  8/8/2010
3  3  NULL  8/8/2010

When first time run, it should return following resultset:

RecordId  PersonId  RecordStatus
1  1  ‘Initial Record’
2  2  ‘Initial Record’
3  3  ‘Initial Record’

Now the state of the table will be:

RecordId  PersonId  SentDate  CreatedDate
1  1  8/9/2010  8/8/2010
2  2  8/9/2010  8/8/2010
3  3  8/9/2010  8/8/2010

Suppose later a new has been added in table and so table values will be:

RecordId  PersonId  SentDate  CreatedDate
1  1  8/9/2010  8/8/2010
2  2  8/9/2010  8/8/2010
3  3  8/9/2010  8/8/2010
4  2  NULL  8/8/2010
5  2  NULL  8/8/2010

Now this query should return

RecordId  PersonId  RecordStatus
4  2  ‘Follow up Record’
5  2  ‘Follow up Record’

  • 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-16T04:49:24+00:00Added an answer on May 16, 2026 at 4:49 am

    Assuming your table is named Contacts:

    SELECT RecordId, C1.PersonId, 'Initial Record' AS RecordStatus
    FROM Contacts C1
    INNER JOIN (SELECT PersonId, COUNT(PersonId) as PersonCount 
                FROM Contacts 
                GROUP BY PersonId) C2 ON
     C1.PersonId = C2.PersonId
    WHERE SentDate IS NULL AND PersonCount = 1
    
    UNION
    
    SELECT RecordId, C1.PersonId, 'Follow Up Record' AS RecordStatus
    FROM Contacts C1
    INNER JOIN (SELECT PersonId, COUNT(PersonId) as PersonCount 
                FROM Contacts 
                GROUP BY PersonId) C2 ON
     C1.PersonId = C2.PersonId
    WHERE SentDate IS NULL AND PersonCount > 1
    
    ORDER BY RecordId
    

    The query works by assuming that if a PersonId only exists once in the table, it’s an initial record; otherwise, it is a Follow Up Record.

    EDIT

    By popular demand, now with 100% less UNION!

    SELECT RecordId, C1.PersonId, 
     CASE WHEN PersonCount > 1 THEN 'Follow Up Record' 
     WHEN PersonCount = 1 THEN 'Initial Record' END
     AS RecordStatus
    FROM Contacts C1
    INNER JOIN (SELECT PersonId, COUNT(PersonId) as PersonCount 
                FROM Contacts
                GROUP BY PersonId) C2 ON
        C1.PersonId = C2.PersonId
    WHERE SentDate IS NULL
    

    EDIT 2

    It’s probably overkill, but here’s my first shot at a CTE version:

    With PersonCountCTE (PersonId, PersonCount) AS 
    (
        SELECT PersonId, COUNT(PersonId) AS PersonCount
        FROM Contacts 
        GROUP BY PersonId
    )
    SELECT RecordId, C1.PersonId, 
        CASE WHEN PersonCount > 1 THEN 'Follow Up Record' 
        WHEN PersonCount = 1 THEN 'Initial Record' END
        AS RecordStatus
    FROM Contacts C1 
    INNER JOIN PersonCountCTE C2 ON C1.PersonId = C2.PersonId
    WHERE SentDate IS NULL 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make the following SQL server statement to work, but I get
I have the following sql statement - it's trying to find all the zipcodes
I am trying to use XQuery in SQL Server 2005 to update xml saved
I'm trying to make a select statement that joins results from three in-line table
I'm trying to get the following SQL statement to work: UPDATE myschema.tableA update_tableA SET
I'm trying to run the following MySQL query: mysql> SELECT num.value, agro.mean, agro.dev ->
I am trying to make a form using two drop down select menus: one
I am trying make long screen to vertical direction. So, I need a screen
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
Trying to make a simple number clicker control for BlackBerry 6/7, like this: At

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.