[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL Query - Select Top or Max

Posted on 2011-03-07
6
Medium Priority
?
392 Views
Last Modified: 2012-05-11
Per my client, they need the last HGBA1C Value (which is the o.OBSVALUE) along with the last date that corresponds with this (o.ObsDate). In my query below, I am getting more then one record for a specific patient. How could I just return the patients one date and value versus them all? SELECT TOP 1 maybe? Not 100% sure.

---- Last HGBA1C Value / Date Last HGBA1C Value / (HGBA1C is obs.HDID = 28)

SELECT  o.pid,
        pp.PatientId,
        pp.PatientProfileId,
        dbo.FormatName('', pp.First, pp.Middle, pp.Last, pp.Suffix) AS PatientName,
        MAX(o.OBSDate) AS obsdate,
        o.OBSVALUE
FROM    OBS AS o
        JOIN OBSHEAD AS oh ON o.HDID = oh.HDID
        JOIN PatientProfile AS pp ON o.PID = pp.PId
WHERE   o.HDID = 28
GROUP BY o.PID,
        pp.PatientId,
        pp.PatientProfileId,
        dbo.FormatName('', pp.First, pp.Middle, pp.Last, pp.Suffix),
        o.OBSVALUE

Open in new window

0
Comment
Question by:Jeff S
6 Comments
 
LVL 4

Expert Comment

by:Alex Matzinger
ID: 35060962
Use

Select Top 1 [columns you want]
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 400 total points
ID: 35060975
you might want to read this article:
http://www.experts-exchange.com/A_3203.html
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35061070
SELECT  top 1 o.pid,
        pp.PatientId,
        pp.PatientProfileId,
        dbo.FormatName('', pp.First, pp.Middle, pp.Last, pp.Suffix) AS PatientName,
        MAX(o.OBSDate) AS obsdate,
        o.OBSVALUE
FROM    OBS AS o
        JOIN OBSHEAD AS oh ON o.HDID = oh.HDID
        JOIN PatientProfile AS pp ON o.PID = pp.PId
WHERE   o.HDID = 28
GROUP BY o.PID,
        pp.PatientId,
        pp.PatientProfileId,
        dbo.FormatName('', pp.First, pp.Middle, pp.Last, pp.Suffix),
        o.OBSVALUE
ORDER BY o.OBSVALUE DESC
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.

 
LVL 7

Author Comment

by:Jeff S
ID: 35061154
ewangoya -

I started out with 28 records in my data set with multiple patients (some having more then one OBSVALUE. When I use your example, I just get the most recent overall. I want the most recent per distinct patient. I am reading through Angels post and think this may be more in line with what I need, just need to read through it all.
0
 
LVL 41

Accepted Solution

by:
Sharath earned 1600 total points
ID: 35063266
try this.
SELECT * 
  FROM (SELECT *, 
               ROW_NUMBER() 
                 OVER(PARTITION BY PatientId ORDER BY obsdate DESC) rn 
          FROM (  SELECT o.pid, 
                         pp.PatientId, 
                         pp.PatientProfileId, 
                         dbo.FORMATNAME('',pp.FIRST,pp.Middle,pp.LAST,pp.Suffix) AS PatientName,
                         MAX(o.OBSDate)                                          AS obsdate, 
                         o.OBSVALUE 
                    FROM OBS AS o 
                         JOIN OBSHEAD AS oh 
                           ON o.HDID = oh.HDID 
                         JOIN PatientProfile AS pp 
                           ON o.PID = pp.PId 
                   WHERE o.HDID = 28 
                GROUP BY o.PID, 
                         pp.PatientId, 
                         pp.PatientProfileId, 
                         dbo.FORMATNAME('',pp.FIRST,pp.Middle,pp.LAST,pp.Suffix), 
                         o.OBSVALUE) t1) t2 
 WHERE rn = 1

Open in new window

0
 
LVL 7

Author Closing Comment

by:Jeff S
ID: 35063355
Thanks!!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Integration Management Part 2
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

873 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