Solved

Get max date from sql query

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query Syntax Join 4 41
SQL Sub-Query Help 22 62
What is the proper way to use for criteria in left join? 7 28
Help Required 3 98
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…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

825 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