Home All Groups Group Topic Archive Search About

Simple "AND" query - how to do it?

Author
8 Jul 2005 3:05 PM
BKE
I have a table with medication info for patients. Fields include record #,
medication name, dosage, start and stop dates, etc. If a patient is on 6
meds, there are 6 records, 2 meds - 2 records, etc., etc.

I want to run a query that lists patients that are on both Medication X AND
Medication Y. It seems like it should be easy enough, but I’m stumped.

select mrno, drugname, startyear from table where drugname= "Vioxx" OR
drugname = "Mobic" order by mrno
of course lists patients who are on one or the other drug. But how can I
whittle that down to those who are on BOTH?

Thanks.

Author
8 Jul 2005 3:13 PM
bagman3rd
select mrno, drugname, startyear from table where
mrno in
(select mrno from table where drugname= "Vioxx" AND drugname = "Mobic")
order by mrno

I am assuming that mrno is the patient name.

Archer

Show quote
"BKE" wrote:

> I have a table with medication info for patients. Fields include record #,
> medication name, dosage, start and stop dates, etc. If a patient is on 6
> meds, there are 6 records, 2 meds - 2 records, etc., etc.
>
> I want to run a query that lists patients that are on both Medication X AND
> Medication Y. It seems like it should be easy enough, but I’m stumped.
>
> select mrno, drugname, startyear from table where drugname= "Vioxx" OR
> drugname = "Mobic" order by mrno
> of course lists patients who are on one or the other drug. But how can I
> whittle that down to those who are on BOTH?
>
> Thanks.
>
Author
8 Jul 2005 3:50 PM
Sericinus hunter
bagman3rd wrote:
> select mrno, drugname, startyear from table where
> mrno in
> (select mrno from table where drugname= "Vioxx" AND drugname = "Mobic")
> order by mrno
>
> I am assuming that mrno is the patient name.

    It's medical record number.
    I think your inner select statement is not going to return anything.
To me the solution look something like this:

SELECT T1.mrno, T1.drugname FROM DataTable T1 INNER JOIN DataTable T2
    ON T1.mrno = T2.mrno
    WHERE T1.drugname = 'Vioxx' AND T2.drugname = 'Mobic'

Show quote
> Archer
>
> "BKE" wrote:
>
>
>>I have a table with medication info for patients. Fields include record #,
>>medication name, dosage, start and stop dates, etc. If a patient is on 6
>>meds, there are 6 records, 2 meds - 2 records, etc., etc.
>>
>>I want to run a query that lists patients that are on both Medication X AND
>>Medication Y. It seems like it should be easy enough, but I’m stumped.
>>
>>select mrno, drugname, startyear from table where drugname= "Vioxx" OR
>>drugname = "Mobic" order by mrno
>>of course lists patients who are on one or the other drug. But how can I
>>whittle that down to those who are on BOTH?
>>
>>Thanks.
>>
Author
8 Jul 2005 4:36 PM
BKE
Thanks very much for your help. It looks like this is going to work for me.
Billie
---------------------------

Show quote
"Sericinus hunter" wrote:

> bagman3rd wrote:
> > select mrno, drugname, startyear from table where
> > mrno in
> > (select mrno from table where drugname= "Vioxx" AND drugname = "Mobic")
> > order by mrno
> >
> > I am assuming that mrno is the patient name.
>
>     It's medical record number.
>     I think your inner select statement is not going to return anything.
> To me the solution look something like this:
>
> SELECT T1.mrno, T1.drugname FROM DataTable T1 INNER JOIN DataTable T2
>     ON T1.mrno = T2.mrno
>     WHERE T1.drugname = 'Vioxx' AND T2.drugname = 'Mobic'
>
> > Archer
> >
> > "BKE" wrote:
> >
> >
> >>I have a table with medication info for patients. Fields include record #,
> >>medication name, dosage, start and stop dates, etc. If a patient is on 6
> >>meds, there are 6 records, 2 meds - 2 records, etc., etc.
> >>
> >>I want to run a query that lists patients that are on both Medication X AND
> >>Medication Y. It seems like it should be easy enough, but I’m stumped.
> >>
> >>select mrno, drugname, startyear from table where drugname= "Vioxx" OR
> >>drugname = "Mobic" order by mrno
> >>of course lists patients who are on one or the other drug. But how can I
> >>whittle that down to those who are on BOTH?
> >>
> >>Thanks.
> >>
>
Author
8 Jul 2005 4:06 PM
BKE
Thanks, Archer. I appreciate your response. However, this code didn't work
for me. It returned no records.
Billie
PS. MRNO is the patient's ID number, same idea as last name.

----------------------------------------

Show quote
"bagman3rd" wrote:

> select mrno, drugname, startyear from table where
> mrno in
> (select mrno from table where drugname= "Vioxx" AND drugname = "Mobic")
> order by mrno
>
> I am assuming that mrno is the patient name.
>
> Archer
>
> "BKE" wrote:
>
> > I have a table with medication info for patients. Fields include record #,
> > medication name, dosage, start and stop dates, etc. If a patient is on 6
> > meds, there are 6 records, 2 meds - 2 records, etc., etc.
> >
> > I want to run a query that lists patients that are on both Medication X AND
> > Medication Y. It seems like it should be easy enough, but I’m stumped.
> >
> > select mrno, drugname, startyear from table where drugname= "Vioxx" OR
> > drugname = "Mobic" order by mrno
> > of course lists patients who are on one or the other drug. But how can I
> > whittle that down to those who are on BOTH?
> >
> > Thanks.
> >
Author
8 Jul 2005 4:14 PM
Aaron Bertrand [SQL Server MVP]
> Thanks, Archer. I appreciate your response. However, this code didn't work
> for me. It returned no records.

ONCE AGAIN.
Please post DDL, sample data and desired results.
See http://www.aspfaq.com/5006 for info.
Author
8 Jul 2005 4:31 PM
BKE
Hi. I got your DDL, etc. message the first time. It takes time to get that
all together, especially the INSERT statements. I'll post it as soon as I get
it together.... unless someone else posts a solution in the meantime. Thanks
for your help.

--------------------------------------------------------------------

Show quote
"Aaron Bertrand [SQL Server MVP]" wrote:

> > Thanks, Archer. I appreciate your response. However, this code didn't work
> > for me. It returned no records.
>
> ONCE AGAIN.
> Please post DDL, sample data and desired results.
> See http://www.aspfaq.com/5006 for info.
>
>
>
Author
8 Jul 2005 4:19 PM
Armando Prato
I'm making a lot of assumptions about the data model and I'm not sure
how good this code is.   I could be way off base here. Posting DDL could
help here.

select m1.mrno, m1.drugname, m1.startyear
from mytable m1
join (select mrno from mytable where drugname = 'Vioxx') m2 on m1.mrno =
m2.mrno
join (select mrno from mytable where drugname = 'Mobic') m3 on m2.mrno =
m3.mrno
order by m1.mrno

Show quote
"BKE" <B**@discussions.microsoft.com> wrote in message
news:26933188-1A9E-490C-AAA5-B92C68400D28@microsoft.com...
> Thanks, Archer. I appreciate your response. However, this code didn't work
> for me. It returned no records.
> Billie
> PS. MRNO is the patient's ID number, same idea as last name.
>
> ----------------------------------------
>
> "bagman3rd" wrote:
>
> > select mrno, drugname, startyear from table where
> > mrno in
> > (select mrno from table where drugname= "Vioxx" AND drugname = "Mobic")
> > order by mrno
> >
> > I am assuming that mrno is the patient name.
> >
> > Archer
> >
> > "BKE" wrote:
> >
> > > I have a table with medication info for patients. Fields include
record #,
> > > medication name, dosage, start and stop dates, etc. If a patient is on
6
> > > meds, there are 6 records, 2 meds - 2 records, etc., etc.
> > >
> > > I want to run a query that lists patients that are on both Medication
X AND
> > > Medication Y. It seems like it should be easy enough, but I'm stumped.
> > >
> > > select mrno, drugname, startyear from table where drugname= "Vioxx" OR
> > > drugname = "Mobic" order by mrno
> > > of course lists patients who are on one or the other drug. But how can
I
> > > whittle that down to those who are on BOTH?
> > >
> > > Thanks.
> > >
Author
8 Jul 2005 3:20 PM
Aaron Bertrand [SQL Server MVP]
Please post DDL, sample data and desired results.
See http://www.aspfaq.com/5006 for info.




Show quote
"BKE" <B**@discussions.microsoft.com> wrote in message
news:75E4F6E7-5B56-48B4-865D-DF1EE00D04BF@microsoft.com...
>I have a table with medication info for patients. Fields include record #,
> medication name, dosage, start and stop dates, etc. If a patient is on 6
> meds, there are 6 records, 2 meds - 2 records, etc., etc.
>
> I want to run a query that lists patients that are on both Medication X
> AND
> Medication Y. It seems like it should be easy enough, but I’m stumped.
>
> select mrno, drugname, startyear from table where drugname= "Vioxx" OR
> drugname = "Mobic" order by mrno
> of course lists patients who are on one or the other drug. But how can I
> whittle that down to those who are on BOTH?
>
> Thanks.
>
Author
8 Jul 2005 5:19 PM
Steve Kass
select mrno from T
where drugname in ('Vioxx', 'Mobic')
group by mrno
having min(drugname) < max(drugname)

The following will generalize better, but may be less efficient for
this particular query:

....
having count(distinct drugname) = 2

Steve Kass
Drew University

BKE wrote:

Show quote
>I have a table with medication info for patients. Fields include record #,
>medication name, dosage, start and stop dates, etc. If a patient is on 6
>meds, there are 6 records, 2 meds - 2 records, etc., etc.
>
>I want to run a query that lists patients that are on both Medication X AND
>Medication Y. It seems like it should be easy enough, but I’m stumped.
>
>select mrno, drugname, startyear from table where drugname= "Vioxx" OR
>drugname = "Mobic" order by mrno
>of course lists patients who are on one or the other drug. But how can I
>whittle that down to those who are on BOTH?
>
>Thanks.
>

>
Author
8 Jul 2005 6:39 PM
--CELKO--
Look up the term Relational Division.  You can then use an list of
drugs as the divisor.

AddThis Social Bookmark Button