December 22, 2006 at 1:02 pm
Hi,
I was wondering how I can get this query to only return the rows from today ( these are Dr. Orders, and I just want to see the most recent ones)
SELECT OeOrders.SourceID, OeOrders.OrderID, OeOrders.CategoryName, OeOrders.ProviderID, OeOrders.ProviderName, OeOrders.ProviderOrder,
OeOrders.Meal, OeOrders.OrderDateTime, OeOrders.OrderNumber, OeOrders.OrderedProcedure, OeOrders.OrderedProcedureName, OeOrders.Priority,
OeOrders.ProcedureNumber, OeOrders.Quantity, OeOrders.ServiceDateTime, OeOrders.Status, OeOrders.VisitID, AdmVisits.PatientID
FROM OeOrders INNER JOIN
AdmVisits ON OeOrders.SourceID = AdmVisits.SourceID AND OeOrders.VisitID = AdmVisits.VisitID
WHERE (OeOrders.VisitID = @VisitID)
ORDER BY OeOrders.OrderDateTime
The OeOrders.OrderDateTime would be the field to compare against.. Im just not sure how.
December 22, 2006 at 1:24 pm
Join to a derived table that generates the most recent date:
SELECT OeOrders.SourceID, OeOrders.OrderID, OeOrders.CategoryName, OeOrders.ProviderID, OeOrders.ProviderName, OeOrders.ProviderOrder,
OeOrders.Meal, OeOrders.OrderDateTime, OeOrders.OrderNumber, OeOrders.OrderedProcedure, OeOrders.OrderedProcedureName, OeOrders.Priority,
OeOrders.ProcedureNumber, OeOrders.Quantity, OeOrders.ServiceDateTime, OeOrders.Status, OeOrders.VisitID, AdmVisits.PatientID
FROM OeOrders
INNER JOIN AdmVisits
ON OeOrders.SourceID = AdmVisits.SourceID AND
OeOrders.VisitID = AdmVisits.VisitID
INNER JOIN
(
SELECT MIN(OeOrders.OrderDateTime) As MostRecentDate
FROM OeOrders
) As dtRecent
ON dtRecent.MostRecentDate = OeOrders.OrderDateTime
WHERE (OeOrders.VisitID = @VisitID)
ORDER BY OeOrders.OrderDateTime
December 22, 2006 at 1:26 pm
AND OeOrders.OrderDateTime>CONVERT(DATETIME,CONVERT(VARCHAR(10),GETDATE(),120))
This conversion will remove the time from the current date.
Brian
December 22, 2006 at 1:34 pm
I dont get any rows returned, do I have to change anything?
December 22, 2006 at 1:48 pm
Let's try that again. Too much eggnog ...
My derived table was wrong, should be the MAX() not MIN() for most recent.
Also, as pointed out, I neglected the time portion of the date. What is the rule for "today" ? Everything since midnight ? Or everything in past 24 hours ?
January 5, 2007 at 1:05 pm
Ten Centuries,
THanks for you help... I forgot about this issue until today..
I guess "Today" should be within the last 24 hrs.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply