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

The Archive Base Latest Questions

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

I am looking to pull out the minimum value across three tables and then

  • 0

I am looking to pull out the minimum value across three tables and then compare this to the lowest value in a fourth table, where the tables are structure like this:

Table 1
(event_id (PK uniqueidentifier),date_created,contact_id)

Table 2
(event_id (PK uniqueidentifier),date_created,contact_id)

Table 3
(event_id (PK uniqueidentifier),date_created,contact_id)

Table 4
(event_id (PK uniqueidentifier),date_created,contact_id)

Where contact_id joins all of the tables and not all contacts will have an event_id specified in all of the tables (hence the possible need for a coalesce function??)

The pseudo syntax for what I want would be something like:

SELECT(COALESCE(MIN(table1date_created),MIN(table2date_created),MIN(table3date_created) AS earliest_date

CASE WHEN (MIN(earliest_date) < MIN(table4_date_created) THEN '0' ELSE '1' END AS first_event

So that I can then group all those with table4_date_created as the first event by year to end up with something like

YEAR  |  Count
2010  |  1500
2009  |  2500
2008  |  1596

Thanks!

EDIT TO INCLUDE SAMPLE DATA

CASE 1 should return a ‘1’ in quassnoi’s syntax (returns a 0)

Dates in table 1
1991-10-24 00:00:00.000 --first event ever
1995-08-03 00:00:00.000
1990-01-03 00:00:00.000
2000-12-31 00:00:00.000
2000-12-31 00:00:00.000
2000-02-08 00:00:00.000
2002-07-03 00:00:00.000
1999-01-06 00:00:00.000

Dates in table 2
2007-02-02 00:40:30.823

Dates in table 3
2006-09-23 01:13:15.530

Dates in table 4
2006-05-31 00:00:00.000    

CASE 2 (should return a 0 ) currently gives a 0

dates in table 1
2007-11-29 00:00:00.000
2007-08-17 00:00:00.000
2005-12-06 00:00:00.000
2007-05-04 00:00:00.000
2006-08-07 00:00:00.000
2007-06-13 00:00:00.000

Dates in  table 2
2006-06-08 02:08:07.253

dates in table 3
2006-06-08 02:08:07.253

dates in table 4
2001-05-31 00:00:00.000
2000-05-31 00:00:00.000 --first event ever  

So the problem is not just a case of the wrong numeric response being returned, they are all being given the same number (0) when some should be given a 0 and others a 1
Would it be a problem that within each table there may be mutliple occurences of the same date?

  • 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-16T12:37:23+00:00Added an answer on May 16, 2026 at 12:37 pm
    SELECT  CASE
            WHEN
            (
            SELECT  MIN(date_created)
            FROM    (
                    SELECT  date_created
                    FROM    table1
                    UNION ALL
                    SELECT  date_created
                    FROM    table2
                    UNION ALL
                    SELECT  date_created
                    FROM    table3
                    ) q
            ) >=
            (
            SELECT  MIN(date_created)
            FROM    table4
            )
            THEN    1
            ELSE    0
            END
    

    Update

    SET LANGUAGE ENGLISH
    DECLARE @table1 TABLE (date_created DATETIME)
    DECLARE @table2 TABLE (date_created DATETIME)
    DECLARE @table3 TABLE (date_created DATETIME)
    DECLARE @table4 TABLE (date_created DATETIME)
    
    INSERT
    INTO    @table1
    VALUES  ('1991-10-24 00:00:00.000')
    INSERT
    INTO    @table1
    VALUES  ('1995-08-03 00:00:00.000')
    INSERT
    INTO    @table1
    VALUES  ('1990-01-03 00:00:00.000')
    INSERT
    INTO    @table1
    VALUES  ('2000-12-31 00:00:00.000')
    INSERT
    INTO    @table1
    VALUES  ('2000-12-31 00:00:00.000')
    INSERT
    INTO    @table1
    VALUES  ('2000-02-08 00:00:00.000')
    INSERT
    INTO    @table1
    VALUES  ('2002-07-03 00:00:00.000')
    INSERT
    INTO    @table1
    VALUES  ('1999-01-06 00:00:00.000')
    
    INSERT
    INTO    @table2
    VALUES  ('2007-02-02 00:40:30.823')
    
    INSERT
    INTO    @table3
    VALUES  ('2006-09-23 01:13:15.530')
    
    INSERT
    INTO    @table4
    VALUES  ('2006-05-31 00:00:00.000') 
    
    SELECT  CASE
            WHEN
            (
            SELECT  MIN(date_created)
            FROM    (
                    SELECT  date_created
                    FROM    @table1
                    UNION ALL
                    SELECT  date_created
                    FROM    @table2
                    UNION ALL
                    SELECT  date_created
                    FROM    @table3
                    ) q
            ) >=
            (
            SELECT  MIN(date_created)
            FROM    @table4
            )
            THEN    1
            ELSE    0
            END
    
    DELETE @table1
    DELETE @table2
    DELETE @table3
    DELETE @table4
    
    INSERT
    INTO    @table1
    VALUES  ('2007-11-29 00:00:00.000')
    INSERT
    INTO    @table1
    VALUES  ('2007-08-17 00:00:00.000')
    INSERT
    INTO    @table1
    VALUES  ('2005-12-06 00:00:00.000')
    INSERT
    INTO    @table1
    VALUES  ('2007-05-04 00:00:00.000')
    INSERT
    INTO    @table1
    VALUES  ('2006-08-07 00:00:00.000')
    INSERT
    INTO    @table1
    VALUES  ('2007-06-13 00:00:00.000')
    INSERT
    INTO    @table3
    VALUES  ('2006-06-08 02:08:07.253')
    INSERT
    INTO    @table3
    VALUES  ('2006-06-08 02:08:07.253')
    INSERT
    INTO    @table4
    VALUES  ('2001-05-31 00:00:00.000')
    INSERT
    INTO    @table4
    VALUES  ('2000-05-31 00:00:00.000')
    
    SELECT  CASE
            WHEN
            (
            SELECT  MIN(date_created)
            FROM    (
                    SELECT  date_created
                    FROM    @table1
                    UNION ALL
                    SELECT  date_created
                    FROM    @table2
                    UNION ALL
                    SELECT  date_created
                    FROM    @table3
                    ) q
            ) >=
            (
            SELECT  MIN(date_created)
            FROM    @table4
            )
            THEN    1
            ELSE    0
            END
    

    The two queries returns 0 and 1, exactly as they should.

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

Sidebar

Related Questions

I am looking for an efficient way to pull the data I want out
I am looking for a one liner to pull out the first comment block
I want to pull out duplicate records in a MySQL Database. This can be
I am trying to create a report and am looking to pull out some
I am looking for a way to pull the first 100 characters from a
Here's the flow I'm looking for for authentication: Attempt to pull in the user's
Looking at what's running and nothing jumps out. Thanks!
I have a generic class that I am using Reflection to pull out the
This is just an out of curiosity question. Let's say you have a database
Say I have the following 2 tables, CREATE TABLE t1( name VARCHAR(25) NOT NULL,

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.