[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Get max date from sql query

Posted on 2007-11-26
4
Medium Priority
?
4,878 Views
Last Modified: 2008-02-01
I need to combine both these queries to get the max date if they have a entry in either the table.  This is part of a much larger query, but I am wondering how to combine these.  Thanks

'contactdate'=(Select max(dtmcontact) FROM tblMHCPContact B  where A.lngpatientid=B.lngpatientid and A.lngtype = @type  and dtmcontact between dateadd(day, -7, getdate()) and getdate()),        
       
 'encounterdate'=(Select Max(visitdate) FROM tblMHCPencounter B  where A.lngpatientid=B.lngpatientid  and visitdate between dateadd(day, -7, getdate()) and getdate()),          
0
Comment
Question by:running32
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
digital_thoughts earned 2000 total points
ID: 20352749
You should be able to use a subquery similar to the below:

SELECT
      MAX(VarDate)
FROM
(
      Select
            dtmcontact AS VarDate
      FROM
            tblMHCPContact B  
      where
            A.lngpatientid=B.lngpatientid
            and A.lngtype = @type  
            and dtmcontact between dateadd(day, -7, getdate()) and getdate()
      UNION
      Select
            visitdate AS VarDate
      FROM
            tblMHCPencounter B  
      where
            A.lngpatientid=B.lngpatientid  
            and visitdate between dateadd(day, -7, getdate())
            and getdate()
)
0
 
LVL 35

Expert Comment

by:David Todd
ID: 20354171
Hi,

A couple of observations on the above query

1.
What is the table alias A?

2.
Suggest using Union All instead of Union. Union implies distinct, which removes duplicate rows. This process involves a sort and a filter. The distinct operation doesn't add anything to this query except extra time and cost.

HTH
  David
0
 
LVL 8

Expert Comment

by:digital_thoughts
ID: 20395758
Any luck on your problem?
0
 

Author Comment

by:running32
ID: 20464468
Yes, thanks I have it working.  
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question