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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:29:33+00:00 2026-05-16T11:29:33+00:00

I am looking to return the following information for each contact contained within a

  • 0

I am looking to return the following information for each contact contained within a set of tables:

Contact_id    |    first_Event_date    |   first_Event_id    | event_type
id123456      |    01/12/2007          |   eveid123456       | table1
id456455      |    05/06/1999          |   eveid456585       | table4

Where the data reflects the first event that each contact has ever been involved with (which could be contained in any of up to 8 tables), and the event_type tells you from which table the event is from.

I have the following query script as a starting point, and it works fine when trying to pull just the contact_id and event_date but when I try to also include the event_id it seems to arbitrarily pull an ID from somewhere which is not correct:

SELECT
table1.contact_id               AS contact_id
MIN(table1.date_received)       AS event_date
table1.event_id                 AS event_id
FROM table1
GROUP BY table1.contact_id
UNION
SELECT
table2.contact_id               
MIN(table2.date_received)       
table2.event_id                 
FROM table2
GROUP BY table2.contact_id

And this is repeated for tables 3-6. I know that I need to also include the table1.event_id etc in the GROUP BY clause, but when I do it returns all mentions of each event for each contact (for each table), so one contact has multiple rows returned for the table1 subquery when there should be at most 1 row returned.

Additionally, in case in helps not all contacts will appear in all of the tables (but will appear at least once across all of the table) and I’m using sql server 2005.

Thanks in advance 🙂

  • 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-16T11:29:33+00:00Added an answer on May 16, 2026 at 11:29 am

    Try merging the tables using UNION ALL to return the first result for each source table, then running an outer query to get the earliest across them all.

    In this example, the first step is selected into a temp table, and the second step against that temp table. It would be possible to carry this operation out as a single nested query, but more confusing to read:

    step 1 – get the earliest row for each contact from each table

    SELECT  contact_id ,
            event_id ,
            date_received
    INTO #firstEventsAllTables      
    FROM    (   SELECT  contact_id ,
                        event_id ,
                        date_received,
                        ROW_NUMBER() OVER (PARTITION BY contact_id
                                           ORDER BY date_received
                                          ) AS rn
                FROM table1      
            ) AS t1
    WHERE rn = 1
    
    UNION ALL       
    
    
    SELECT  contact_id ,
            event_id ,
            date_received
    FROM    (   SELECT  contact_id ,
                        event_id ,
                        date_received,
                        ROW_NUMBER() OVER (PARTITION BY contact_id
                                           ORDER BY date_received
                                          ) AS rn
                FROM table2      
            ) AS t2
    WHERE rn = 1
    
    UNION ALL
    
    etc...
    

    step 2 – find the earliest row per contact across all tables

    SELECT  contact_id ,
            event_id ,
            date_received
    FROM    (   SELECT  contact_id ,
                        event_id ,
                        date_received,
                        ROW_NUMBER() OVER (PARTITION BY contact_id
                                           ORDER BY date_received
                                          ) AS rn
                FROM #firstEventsAllTables       
            ) AS f
    WHERE rn = 1
    

    (untested)

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

Sidebar

Related Questions

I'm looking at the following code snippet: my @ret = <someMethod> return (undef) if(
I'm looking for (arguably) the correct way to return data from a XmlHttpRequest .
I'm looking to force my application to shut down, and return an Exit code.
Looking for C# class which wraps calls to do the following: read and write
HI, Using SQL server 2005 I have the following query: SELECT contact_id ,YEAR(date_created) AS
I'm looking for possible solutions for the following scenario: I have a service that
I am looking for a XSL snippet that simply returns the XML unaltered. It
What I mean is, I'm looking for really short code that returns the lower
Looking for feedback on : http://code.google.com/p/google-perftools/wiki/GooglePerformanceTools
Looking for an example that: Launches an EXE Waits for the EXE to finish.

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.