3

私のデータは次のようになります:

+--------+-----------+---------+
| doctor | datefield | patient |
+--------+-----------+---------+
| A      | 1/1/2011  | ABC123  |
| A      | 1/20/2011 | AAA123  |
| A      | 1/21/2011 | AAA123  |
|        |           |         |
| A      | 2/1/2011  | ABC123  |
| A      | 2/10/2011 | BBBYYY  |
|        |           |         |
| B      | 1/1/2011  | ABC123  |
| B      | 1/20/2011 | AXA435  |
| B      | 1/21/2011 | AAA123  |
|        |           |         |
| B      | 2/1/2011  | ABC123  |
| B      | 2/10/2011 | BBBYYY  |
+--------+-----------+---------+

医師ごとの新規患者数を計算したいas compared to the entire date range for that specific doctor

2011年1月が最初の月だったとしましょう。

論理:

  1. 医師Aは2011年1月に2人の新しい患者を抱えていました
  2. 医師Aは2011年2月に1人の新しい患者を抱えていました
  3. 医師Bには2011年1月に3人の新しい患者がいました
  4. 医師Bは2011年2月に1人の新しい患者を抱えていました

これが私が望む結果です:

+--------+-------+------+----------------+
| doctor | month | year | # new patients |
+--------+-------+------+----------------+
| A      |     1 | 2011 |              2 |
| A      |     2 | 2011 |              1 |
| B      |     1 | 2011 |              3 |
| B      |     2 | 2011 |              1 |
+--------+-------+------+----------------+

始めるのを手伝ってくれませんか。

4

3 に答える 3

2

それをいくつかのステップに分ける必要があります:

  • 特定の患者と医師の最初の月がどの月であったかを判別します(つまりselect doctor, patient, min(month) from mytable group by doctor, patient

  • 以前の結果を医師と月でグループ化して、新しい患者の数を数えます

サブクエリまたは一時テーブル/テーブル変数のいずれかを使用してこれを実行できる必要があります。

編集:私が書くクエリはおそらく次のようになります:

select doctor, year, month, count(1) [num_new]
from
(
    select doctor
           ,patient
           ,datepart(mm, min(datefield)) [month]
           ,datepart(yyyy, min(datefield)) [year]
    from mytable
    group by doctor, patient
) sub
group by doctor, year, month
于 2012-09-13T22:40:34.977 に答える
2

ジョーの答えの構文を修正

select doctor, year, month, count(patient) [num_new]
from (select doctor, patient, min(MONTH([datefield])) [month], min(YEAR([datefield]))  [year]
    from [dbo].[test_aakruti]
    group by doctor, patient) as table1
group by doctor, [year], [month]
于 2012-09-13T23:07:21.480 に答える
1

1か月の患者の総数を取得するのは非常に簡単です。

SELECT Doctor, YEAR(datef) AS yr, MONTH(datef) AS mnth, COUNT(patient) AS totPatients FROM ##doctors
GROUP BY Doctor, YEAR(datef), MONTH(datef)

Doctor  yr      mnth    totPatients
A       2011    1       3
A       2011    2       2
B       2011    1       3
B       2011    2       2

しかし、新しい患者の数を取得することは少し複雑です。このために、私たちは各患者の最初の訪問を取得する必要があります。これは次の方法で実行できます。

SELECT doctor, patient, MIN(MONTH(datef)) AS Mnth, MIN(YEAR(datef)) AS Yr FROM ##doctors GROUP BY doctor, patient

次に、この2つを組み合わせることで、目的の結果が得られます。

WITH fstVisit AS (
    SELECT doctor, patient, min(month(datef)) AS Mnth, min(year(datef)) AS Yr FROM ##doctors GROUP BY doctor, patient
    )
    SELECT Doctor,  yr, mnth, COUNT(patient) AS totPatients FROM fstVisit
    GROUP BY Doctor, yr, mnth

Doctor  yr      mnth    totPatients
A       2011    1       2
A       2011    2       1
B       2011    1       3
B       2011    2       1
于 2012-09-13T22:47:57.033 に答える