Solved

Get max date from sql query

Posted on 2007-11-26
4
4,865 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 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now