Calculating if a patient in a table has two unique rows with specific entries

I want a list of patients that take 2 (Or more) medication types at the same time.  The table I have;

 

Name                     Benzos____________Opioids___________Medication

Patient A                 Y                               N                         Xanax

Patient B                 Y                               N                         Xanax

Patient C                 Y                               N                         Xanax

Patient C                 N                              Y                         Lortab

Patient D                N                               Y                          Lortab

Patient E                Y                                N                         Xanax

Patient E                N                               Y                          Hydrocodone

 

 

Ideally, my visual would only display Patient C and Patient E (Those patients that had both a Benzo and Opioid medication, although different rows).  I can't group by--as that will break the rest of my report.  If I try a new Conditional Column (Or custom column with an IF statement), since it evaluates row by row without grouping by Patient--I end up with the same issue.  Thanks for any help!
CMCITDAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
try customize this:

;with yourTable as
(
	Select 'Patient A' Name, 'Y' Benzos, 'N' Opioids, 'Xanax' Medication union
	Select 'Patient B' Name, 'Y' Benzos, 'N' Opioids, 'Xanax' Medication union
	Select 'Patient C' Name, 'Y' Benzos, 'N' Opioids, 'Xanax' Medication union
	Select 'Patient C' Name, 'N' Benzos, 'Y' Opioids, 'Lortab' Medication union
	Select 'Patient D' Name, 'N' Benzos, 'Y' Opioids, 'Lortab' Medication union
	Select 'Patient E' Name, 'Y' Benzos, 'N' Opioids, 'Xanax' Medication union
	Select 'Patient E' Name, 'N' Benzos, 'Y' Opioids, 'Hydrocodone' Medication
)
Select a.* from yourTable a
inner join
(
	Select Name from yourTable where Benzos = 'Y' or Opioids = 'Y'
	Group By Name
	Having Count(Name) = 2
) b on a.Name = b.Name
Order By a.Name, a.Benzos, a.Opioids, a.Medication

Open in new window


OR

;with yourTable as
(
	Select 'Patient A' Name, 'Y' Benzos, 'N' Opioids, 'Xanax' Medication union
	Select 'Patient B' Name, 'Y' Benzos, 'N' Opioids, 'Xanax' Medication union
	Select 'Patient C' Name, 'Y' Benzos, 'N' Opioids, 'Xanax' Medication union
	Select 'Patient C' Name, 'N' Benzos, 'Y' Opioids, 'Lortab' Medication union
	Select 'Patient D' Name, 'N' Benzos, 'Y' Opioids, 'Lortab' Medication union
	Select 'Patient E' Name, 'Y' Benzos, 'N' Opioids, 'Xanax' Medication union
	Select 'Patient E' Name, 'N' Benzos, 'Y' Opioids, 'Hydrocodone' Medication
)
Select a.* from yourTable a
inner join
(
	Select Name
	from
	(
		Select Name from yourTable where Benzos = 'Y'
		union all
		Select Name from yourTable where Opioids = 'Y'
	) a
	Group By Name
	Having Count(Name) = 2
) b on a.Name = b.Name
Order By a.Name, a.Benzos, a.Opioids, a.Medication

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PortletPaulEE Topic AdvisorCommented:
>>"patients that take 2 (Or more) ..."

that requires use of (greater than or equal) e.g.       Having Count(Name) >= 2

an alternative, but it lists all details, not just a list of unique names:
select *
from (
   select *
    , count(*) over(partition by Name) as cn
  from yourtable
   ) d
where cn >= 2

Open in new window

0
awking00Information Technology SpecialistCommented:
select * from yourtable
where name in
(select name from yourtable where Benzos = 'Y'
 intersect
 select name from yourtable where Opioids = 'Y')
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
powerBI

From novice to tech pro — start learning today.