0

I'm trying to get pretty basic output that pulls unique NDC Codes for medications and counts the number of unique patients that take each drug. My dataset basically looks like this:

patient_id | drug_ndc
---------------------
01         | 250
02         | 725       
03         | 1075
04         | 1075
05         | 250
06         | 250

I want the output to look something like this:

NDC  | Patients
--------------
250  |  3
1075 |  2
725  |  1

I tried using some queries like this:

select distinct drug_ndc as NDC, count patient_id as Patients
from table 1
group by 1
order by 1

But I keep getting errors. I've tried with and without using an alias, but to no avail.

2 Answers 2

2

The correct syntax should be:

select drug_ndc as NDC, count(*) as Patients
from table 1
group by drug_ndc
order by 1;

SELECT DISTINCT is almost never appropriate with GROUP BY. And you can can use COUNT(*) unless the patient id can be NULL.

Sign up to request clarification or add additional context in comments.

3 Comments

but this is not counting the number of unique patients... just the total number
@RaphaelRoth . . . There are no duplicates in the data in the question -- or mentioned. count(distinct) is not appropriate.
yes, it is mentioned :"counts the number of unique patients"
1

to get the number of unique patients, you should do:

select drug_ndc as NDC, count(distinct patient_id) as Patients
from table 1
group by drug_ndc;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.