これが私のデータです:
+----------+---------------+------------+---------------------+
| 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 |
+----------+---------------+------------+---------------------+
これが私が望む結果です:
+--------+-------+--------------------+
| doctor | month | count new patients |
+--------+-------+--------------------+
| A | 1 | 1 |
| A | 2 | 1 |
| B | 1 | 2 |
| B | 2 | 0 |
+--------+-------+--------------------+
毎月、医師の新しい患者を数えたいと思います。
私が与えられたビジネスルールは次のとおりです。
- 患者は1人の医師にのみ関連付けることができます
- 患者が関連付けられている医師は、彼を見た最後の医師です
これが私がこれまでに持っているものです:
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
このクエリにより、患者が関連付けられている医師を含む行のprimary key
accession_daily_keyが得られます。
その特定の月の各医師の新しい患者数を毎月取得する方法を教えてください。
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')