Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Server MIN Funtion

Posted on 2013-12-26
6
Medium Priority
?
341 Views
Last Modified: 2013-12-30
Hi,

I have the SQL below. I am trying to get only one row with MiniMum Date (First Med Given Date and time). But it is returning multiple rows. Therefore, I am using Top 1 Function. Is there any other way to do this without Top Funtion? Please advise. Thanks

SELECT TOP 1
            DIS.EID,
            DIS.Institution,
            DIS.MRN,
            DIS.Account,
            DIS.AdmitDateTime,
            DIS.ERAdmitDtTm AS 'ED Received Date Time',
            Meds.[First Abx Admin Date],
            Meds.DrugName AS 'Antibiotic Name',
            (CASE      
                  WHEN (Meds.[First Abx Admin Date]) <= dateadd(hh,3, (CASE WHEN DIS.ERAdmitDtTm IS NULL THEN Dis.AdmitDateTime ELSE DIS.ERAdmitDtTm END))
                  THEN 'Y'
                  Else 'N'
            END) as ArrivalAbx
FROM      
            VISITPT_ALL_DIS AS DIS
            LEFT OUTER JOIN
                  (      SELECT
                              Med.EID,
                              Med.Account,
                              Med.DrugName,
                              MIN(MED.LastAdminDateTime)AS 'First Abx Admin Date'
                        FROM
                              MedAdminMAR_All_Adm AS MED
                        WHERE
                              Med.DrugID IN ( 'ACYC500I','AMIK500I','AMPH50I','AMPH50IL')
                        GROUP BY
                              Med.EID,
                              Med.Account,
                              Med.AdmitDateTime,
                              Med.DrugName
                        )Meds
            ON DIS.Account = Meds.Account
WHERE
      DIS.PatientType = 'IN'
      AND DIS.AccountStatus = 'DIS'  
ORDER BY
      DIS.Institution,
      DIS.MRN,
      DIS.Account,
      DIS.AdmitDateTime ,
      Meds.[First Abx Admin Date]
0
Comment
Question by:rustypoot
6 Comments
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 1000 total points
ID: 39741056
The only Min() I see here is in a subquery, which is on the right side of a LEFT JOIN, which means all rows on the LEFT side (VISITPT_ALL_DIS AS DIS) will be returned in the result set.

So ... if you only want to return the one row, for starters change the LEFT JOIN to an INNER JOIN (or just JOIN).

Also, if DIS has a date that is the equivalent of MED.LastAdminDateTime, then you'll have to add that relationship to the JOIN clause, so the line below needs to change to the line below that:

             ON DIS.Account = Meds.Account

Open in new window

             ON DIS.Account = Meds.Account AND DIS.??Date = Meds.[First Abx Admin Date'] 

Open in new window

0
 
LVL 13

Expert Comment

by:AielloJ
ID: 39741751
rustypoot

Without knowing more about your data model, it appears the root cause of the problem is a lack of the proper clauses in the WHERE clause.  I'm assuming 'Account' is the patient you are trying to get the one row for.

Since 'Account' is not in the WHERE clause, the first row for some random patient is probably being returned.

Best regards,

AielloJ
0
 
LVL 32

Expert Comment

by:awking00
ID: 39742414
select whatever from
(select whatever, row_number() over (partition by eid order by [first med given date and time] desc) rn
from yourtablejoin)
where rn = 1
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:rustypoot
ID: 39746541
Jim,

Thanks for the reply. But even if I change the JOIN to INNER JOIN, I get same # of rows. Also, there is no equivalent date to join on..
0
 
LVL 32

Expert Comment

by:awking00
ID: 39746636
Can you provide some relevant sample data (i.e. eliminate the extraneous fields and only include data meeting your filtering requirements other than the dates in question) and your expected output.
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 1000 total points
ID: 39746642
Since you are getting multiple rows, they are probably coming from the JOIN'd table.  So you need to ask yourself
1. Why you are including Med.EID when you are not using it anywhere, either in the SELECT or JOIN.
2. Since you are selecting DrugName, which one do you want returned if there are multiple rows with a different DrugName?  The one with the oldest LastAdminDateTime?  If so what happens if there are two with the same LastAdminDateTime?
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Loops Section Overview
Screencast - Getting to Know the Pipeline

972 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