Solved

SQL Query - Select Top or Max

Posted on 2011-03-07
6
386 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 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 100 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:ewangoya
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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 40

Accepted Solution

by:
Sharath earned 400 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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.
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.​
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

770 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