Solved

Include group in data set if condition is met

Posted on 2013-10-23
3
135 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
3 Comments
 
LVL 65

Accepted Solution

by:
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 (
    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 69

Expert Comment

by:ScottPletcher
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now