Solved

Get max date from sql query

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

9 Experts available now in Live!

Get 1:1 Help Now