We help IT Professionals succeed at work.
Get Started

Distinct Count in SQL with conditions

jul_to_cool
jul_to_cool asked
on
48 Views
Last Modified: 2015-08-31
Hi All,

I need to identify the PtID that have 2+ unique ORDER_MED_ID'S and the DISCON_TIME is null, I'll need to display the number of unique ORDER_MED_ID's that meet the condition and then limit the query to people that have 2+ unique ORDER_MED_ID'S that meet the condition.   How can I accomplish this?

select
orm.ORDER_MED_ID
, orm.DISCON_TIME

from PAT_ENC pae
inner join ORDER_MED orm ON pae.PAT_ENC_CSN_ID=orm.PAT_ENC_CSN_ID

where
#ofmeds >=2

From the example below, PtID 1234 and 3456 meet the condition.

The results look like this:
PtID                    ORDER_MED_ID               DISCON_TIME

1234                    0101233                          
1234                    0101234                          
1234                    0101235                          
1234                    0101236
2345                    0101237
3456                    0101238
3456                    0101239
3456                    0101240                           2015-07-20
4567                    0101241                           2015-07-20
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE