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

  • SEARCH
  • Home
  • 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 6139565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:00:23+00:00 2026-05-23T18:00:23+00:00

Wonder if someone could give me a quick hand. I have 2 select queries

  • 0

Wonder if someone could give me a quick hand. I have 2 select queries (as shown below) and I want to compare the results of both and only return the result that has the most recent date.

So say I have the following 2 results from the queries:-

---------  ----------  -----------------------  ---------------  ------  --
COMPANY A  EMPLOYEE A  2007-10-16 17:10:21.000  E-mail 6D29D6D5  SYSTEM  1
COMPANY A  EMPLOYEE A  2007-10-15 17:10:21.000  E-mail 6D29D6D5  SYSTEM  1

I only want to return the result with the latest date (so the first one). I thought about putting the results into a temporary table and then querying that but just wondering if there’s a simpler, more efficient way?

SELECT * FROM (
    SELECT fc.accountidname, fc.owneridname, fap.actualend, fap.activitytypecodename, fap.createdby, fap.createdbyname,
    ROW_NUMBER() OVER (PARTITION BY fc.accountidname ORDER BY fap.actualend DESC) AS RN 
    FROM FilteredContact fc
    INNER JOIN FilteredActivityPointer fap ON fc.parentcustomerid = fap.regardingobjectid
    WHERE fc.statecodename = 'Active' 
        AND fap.ownerid = '0F995BDC' 
        AND fap.createdon < getdate()
) tmp WHERE RN = 1

SELECT * FROM (
    SELECT fa.name, fa.owneridname, fa.new_technicalaccountmanageridname, fa.new_customerid, fa.new_riskstatusname,  
    fa.new_numberofopencases, fa.new_numberofurgentopencases, fap.actualend, fap.activitytypecodename, fap.createdby, fap.createdbyname,
    ROW_NUMBER() OVER (PARTITION BY fa.name ORDER BY fap.actualend DESC) AS RN 
    FROM FilteredAccount fa 
    INNER JOIN FilteredActivityPointer fap ON fa.accountid = fap.regardingobjectid 
    WHERE fa.statecodename = 'Active' 
        AND fap.ownerid = '0F995BDC' 
        AND fap.createdon < getdate()
) tmp2 WHERE RN = 1
  • 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-23T18:00:23+00:00Added an answer on May 23, 2026 at 6:00 pm

    if the tables have the same structure (column count and column types to match), then you could just union the results of the two queries, then order by the date desc and then select the top 1.

    select top 1 * from
    (
       -- your first query
    union all
       -- your second query.
    ) T
    order by YourDateColumn1 desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have not done a lot with WinForms so I wonder if someone could
I wonder whether someone could help me please. I have to admit I'm relatively
I wonder whether someone could help me please. I have put together a form
I wonder if someone could help me figure out how to parse a string
I wonder if someone could be kind enough to explain the function.prototype thingie (thingie!!??)
I wonder what a repository means in doctrine? Could someone please explain?
wonder whether someone can help me with the following one... I have a struct
I wonder if someone could offer insight here: I work for an independent multimedia
I wonder if someone could quickly help me with the following, do I need
I'm a Wicket newbie. I wonder if someone could help me with the following:

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.