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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:09:28+00:00 2026-06-10T19:09:28+00:00

I have the following union query that queries for the most recent date of

  • 0

I have the following union query that queries for the most recent date of a column if it exists:

SELECT TOP 1 m.sentdate AS 'calltreelastsignedoff' 
FROM Incidents i 
INNER JOIN Plans p ON i.planuid = p.uid 
INNER JOIN IncidentMessages im ON i.uid = im.incidentuid 
INNER JOIN Messages m ON im.messageuid = m.uid 
WHERE p.uid = '031E3346-2921-426E-9494-1111111111' 
UNION
SELECT TOP 1 m.sentdate AS 'calltreelastsignedoff' 
FROM Incidents i
INNER JOIN PlanExercises pe ON i.planexerciseuid = pe.uid
INNER JOIN IncidentMessages im ON i.uid = im.incidentuid 
INNER JOIN Messages m ON im.messageuid = m.uid 
WHERE pe.planuid = '031E3346-2921-426E-9494-1111111111' 

This will return 2 values if each query returns a top 1 result.

What I really want is to select the top 1 of the combined query.

How can I perform a select on the unioned query?

  • 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-10T19:09:29+00:00Added an answer on June 10, 2026 at 7:09 pm

    try this:

    You could do this with a derived table

    select top 1 from
    (   
      SELECT TOP 1 m.sentdate AS 'calltreelastsignedoff' 
      FROM Incidents i 
      INNER JOIN Plans p ON i.planuid = p.uid 
      INNER JOIN IncidentMessages im ON i.uid = im.incidentuid 
      INNER JOIN Messages m ON im.messageuid = m.uid 
      WHERE p.uid = '031E3346-2921-426E-9494-1111111111' 
    
      UNION
    
      SELECT TOP 1 m.sentdate AS 'calltreelastsignedoff' 
      FROM Incidents i
      INNER JOIN PlanExercises pe ON i.planexerciseuid = pe.uid
      INNER JOIN IncidentMessages im ON i.uid = im.incidentuid 
      INNER JOIN Messages m ON im.messageuid = m.uid 
      WHERE pe.planuid = '031E3346-2921-426E-9494-1111111111' 
    )a
    order by <col>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following SQL statementL: select DATE(bla), count(*) from tableA group by DATE(bla) UNION
I have the following query select count(trade_sid), shortcode from trade where trade.trade_date <= sysdate
I have to write a query that performs a union between two tables with
I have the following query in mysql 5.1.41: select distinct table_schema from information_schema.tables where
I have the following MySQL query, which produces the result I want: SELECT `l`.`status`,
I have the following query statement: SELECT idelm_na AS ID, nd.lat_nd AS lat, nd.lon_nd
I have the following query that's returning the error: Incorrect syntax near the keyword
I have the following SQL query that runs fairly well, however it seems to
I have the following query that returns a set of rows based on some
I have the following table: I am trying to create an SQL query that

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.