Here’s my data:
+----------+---------------+------------+---------------------+
| doctor | received_date | patient_id | accession_daily_key |
+----------+---------------+------------+---------------------+
| A | 1/1/2011 | ABC123 | 1 |
| A | 1/20/2011 | AAA123 | 2 |
| A | 1/21/2011 | AAA123 | 3 |
| | | | 4 |
| A | 2/1/2011 | ABC123 | 5 |
| A | 2/9/2011 | BBBYYY | 6 |
| | | | 7 |
| B | 1/2/2011 | ABC123 | 8 |
| B | 1/20/2011 | AXA435 | 9 |
| B | 1/19/2011 | AAA123 | 10 |
| | | | 11 |
| B | 2/1/2011 | ABC123 | 12 |
| B | 2/10/2011 | BBBYYY | 13 |
+----------+---------------+------------+---------------------+
Here’s the result I want:
+--------+-------+--------------------+
| doctor | month | count new patients |
+--------+-------+--------------------+
| A | 1 | 1 |
| A | 2 | 1 |
| B | 1 | 2 |
| B | 2 | 0 |
+--------+-------+--------------------+
I would like to count the new patients for a doctor every month.
The business rules I’ve been given are:
- a patient can be associated with only one doctor
- the doctor to which the patient is associated is the LAST doctor that saw him
Here’s what I have so far:
select patient_id,max(month(RECEIVED_DATE)) AS Mnth, max(year(RECEIVED_DATE)) AS Yr, ACCESSION_DAILY_KEY
FROM [F_ACCESSION_DAILY]
where RECEIVED_MLIS_INFORMATION=1
GROUP BY patient_id,ACCESSION_DAILY_KEY
This query will give me the primary key accession_daily_key of the row which contains the doctor to which the patient is associated with.
Can you please guide me on how I can get the number of patients every month that are NEW for each doctor in that specific month?
Edit by jcolebrand:
CREATE TABLE F_ACCESSION_DAILY (
doctor VARCHAR(10) NOT NULL
, received_date DATETIME NOT NULL
, patient_id VARCHAR(10) NOT NULL
, accession_daily_key INT NOT NULL
)
INSERT INTO F_ACCESSION_DAILY (doctor,received_date,patient_id,accession_daily_key) VALUES ('A','1/1/2011','ABC123','1')
INSERT INTO F_ACCESSION_DAILY (doctor,received_date,patient_id,accession_daily_key) VALUES ('A','1/20/2011','AAA123','2')
INSERT INTO F_ACCESSION_DAILY (doctor,received_date,patient_id,accession_daily_key) VALUES ('A','1/21/2011','AAA123','3')
INSERT INTO F_ACCESSION_DAILY (doctor,received_date,patient_id,accession_daily_key) VALUES ('A','2/1/2011','ABC123','5')
INSERT INTO F_ACCESSION_DAILY (doctor,received_date,patient_id,accession_daily_key) VALUES ('A','2/9/2011','BBBYYY','6')
INSERT INTO F_ACCESSION_DAILY (doctor,received_date,patient_id,accession_daily_key) VALUES ('B','1/2/2011','ABC123','8')
INSERT INTO F_ACCESSION_DAILY (doctor,received_date,patient_id,accession_daily_key) VALUES ('B','1/20/2011','AXA435','9')
INSERT INTO F_ACCESSION_DAILY (doctor,received_date,patient_id,accession_daily_key) VALUES ('B','1/19/2011','AAA123','10')
INSERT INTO F_ACCESSION_DAILY (doctor,received_date,patient_id,accession_daily_key) VALUES ('B','2/1/2011','ABC123','12')
INSERT INTO F_ACCESSION_DAILY (doctor,received_date,patient_id,accession_daily_key) VALUES ('B','2/10/2011','BBBYYY','13')
This seems like it should do what you need, I think