Include group in data set if condition is met

Posted on 2013-10-23
Last Modified: 2013-10-28

I need to show a group of data only if one of the records contains specific text.

In the raw data below there are two patients who have a medication list. If the data set for the patient contains the med Benedryll, then I need the set to show in the output.

select max( name, Max( med
from name a
left join medication b
on a.personID=b.PersonID
group by,

Open in new window

The script above acts like the script I've tried (excluding the where clause) and produces the data below.

Name             Med
Fryy, Peter      Advil
Fryy, Peter      Tylenol
Fryy, Peter      Alieve
Penn, Anna      Advil
Penn, Anna      Benedryll
Penn, Anna      Tylenol
Penn, Anna      Alieve

How would I modify the script so that I only show patients who use benedryll as one of their medications?

The desired result from the script would be to show:

Name             Med
Penn, Anna      Advil
Penn, Anna      Benedryll
Penn, Anna      Tylenol
Penn, Anna      Alieve
Question by:susnewyork
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 66

Accepted Solution

Jim Horn earned 500 total points
ID: 39595076
<air code.  I do my own stunts too>
SELECT n.Name, m.Med
FROM name n
    JOIN medication m on n.personID=m.PersonID
WHERE n.Name IN (
    FROM name n
       JOIN medication m on n.personID=m.PersonID
   WHERE m.Med='Benedryll')
ORDER BY n.Name, m.Med

Open in new window

LVL 69

Expert Comment

by:Scott Pletcher
ID: 39595204
Can't use name to do the match, because it might be duplicated by different personIDs.  Either way, there's no reason to do two name joins/lookups.  Also, I prefer to use EXISTS rather than IN to possibly get a better data plan:

    n.Name, m.Med
FROM dbo.medication m
    n.personID = m.personID
    EXISTS (
        SELECT 1
        FROM dbo.medication m2
            m2.personId = m.personId AND
   = 'benadryl'
    n.Name, m.Med

Author Closing Comment

ID: 39606745
This solution worked for me and produced accurate data based on my tests. Thanks.

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

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 we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

717 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