Solved

Get max date from sql query

Posted on 2007-11-26
4
4,870 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syntax for query to update table 2 43
SQL Pivot table 2 45
Converting Stored Procedure to SQL Statement 5 44
Error in sql query statment. 21 51
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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