[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Include group in data set if condition is met

Posted on 2013-10-23
3
Medium Priority
?
142 Views
Last Modified: 2013-10-28
Hi,

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

Ex:
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(a.name) name, Max(b.med) med
from name a
left join medication b
on a.personID=b.PersonID
group by a.name, b.med

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
0
Comment
Question by:susnewyork
[X]
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
3 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 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 (
    SELECT DISTINCT n.name
    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

0
 
LVL 70

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:


SELECT
    n.Name, m.Med
FROM dbo.medication m
INNER JOIN dbo.name n ON
    n.personID = m.personID
WHERE
    EXISTS (
        SELECT 1
        FROM dbo.medication m2
        WHERE
            m2.personId = m.personId AND
            m2.med = 'benadryl'
    )
ORDER BY
    n.Name, m.Med
0
 
LVL 1

Author Closing Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

649 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