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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:04:26+00:00 2026-06-07T05:04:26+00:00

From the following three tables, I need to: Return one row for each record

  • 0

From the following three tables, I need to:

  1. Return one row for each record in People, don’t duplicate the person if they have more than one associated contact.
  2. Include associated ContactValue, where ContactType is HOMEPHONE, and the start of the date range is prior to now, and the end of the date range is later than now or null. If a person does not have a home phone, still show the person, but show NULL as the value for ContactValue.

People

PersonId (PK, int)
FullName (nvarchar)

PeopleContacts

PersonId (PK, int)
ContactId (PK, int)
StartValidDate (PK, datetime)
EndValidDate (datetime, null)

Contacts

ContactId (PK, int)
ContactType (nvarchar)
ContactValue (nvarchar)

The People table contains a unique list of people. PeopleContacts may contain several contact associations for each person, for different contact types, and a date range that the contact is good for. Contacts contains a list of contact values of certain types. For example, ContactType of “WORKPHONE” and ContactValue of “(555) 555 5555”.

Requirements:

  1. Return one row for each record in People, don’t duplicate the person if they have more than one associated contact.
  2. Include associated ContactValue, where ContactType is HOMEPHONE, and the start of the date range is prior to now, and the end of the date range is later than now or null. If a person does not have a home phone, still show the person, but show NULL as the value for ContactValue.

If People contained three rows, with:

1     Jones, Bob
2     Smith, Bob
3     Smith, Fred

And PeopleContacts contained four rows, with:

1     4    01/01/2012     NULL
2     1    01/01/2012     02/01/2012
2     2    02/02/2012     NULL
3     3    01/01/2012     NULL
3     4    01/01/2012     NULL

And Contacts contained four rows, with:

1     HOMEPHONE     (555) 555 5252
2     HOMEPHONE     (666) 666 6666
3     HOMEPHONE     (777) 777 7777
4     WORKPHONE     (555) 555 5555

Output from the correct query, if run after 02/02/2012, should look like:

FullName     ContactValue
--------     ------------
Jones, Bob   NULL
Smith, Bob   (666) 666 6666
Smith, Fred  (777) 777 7777

Jones, Bob has an association to a contact, but it’s to a WORKPHONE, so ContactValue should be NULL. Smith, Bob has associations to two HOMEPHONE records, but only one, (666) 666 6666, still has a valid date range. Smith, Fred has an association to both a HOMEPHONE and a WORKPHONE.

  • 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-07T05:04:28+00:00Added an answer on June 7, 2026 at 5:04 am

    I would solve this using a Common Table Expression – This should be more efficient than using APPLY which would be evaluated for each row. Using a CTE, you would just get a single result set and then do a left outer join on that.

    I would use RANK to make sure you only get one contact back … by ordering the CTE and joining the CTE by the first rank you will just get a single contact.

    The SQL would look something like this:

    WITH [CTE] AS (
    SELECT
            pc.PersonId
        ,   pc.ContactValue
        ,   RANK() OVER (PARTITION BY PersonId ORDER BY ContactValue DESC) AS [Seed]
    FROM
        PeopleContacts pc
        INNER JOIN  Contacts c ON (pc.ContactId = c.ContactId AND c.ContactType='HOME')
        WHERE pc.StartValidDate <= GETDATE()
        AND (pc.EndValidDate > GETDATE() OR pc.EndValidDate IS NULL)
    )
    
    SELECT      FullName
            ,   ISNULL(CTE.ContactValue, '') 
    FROM People p
    LEFT OUTER JOIN CTE ON (CTE.PersonId = p.PersonID AND CTE.Seed = 1)
    

    (This code is untested, if you set up a sql fiddle then I’d be happy to get it working)

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

Sidebar

Related Questions

I have three mysql table from same database Db1. Three tables have following columns.
Is there a possibility to speedup a SQL-query like the following one? SELECT.... FROM
I currently need to execute a database query that involves data from three separate
In one of my MySQL tables, I have following columns: Skills varchar(80) Industry varchar(40)
I am looking to return the following information for each contact contained within a
I have the following task: there are a simple table from mysql database: <html>
Following on from my other post about primary keys I am wondering if there
I have to build a tree using the following data which comes from a
I have the following piece of code from a book. There is this function
I have following code to remove group from collection. Technically, there should be no

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.