1

我的表有医生 ID、就诊 ID(一个医生可以多次就诊)和就诊原因,如下所示:

doctor_uid | visit_uid | visit_cause
11-11-11    22-22-11      1
22-22-22    44-44-22      2
11-11-11    23-23-23      1

我需要根据visit_cause(应该相等'1')计算每位医生的所有就诊次数。在计算每位医生的所有就诊次数之前visit_cause,我的查询中也必须包含以下内容:

SELECT 
   v.vra_uid, 
   COUNT(v.visit_uid) OVER (PARTITION BY v.vra_uid) AS number_of_visits
FROM visits v 

它返回:

doctor_uid | number_of_visits 
    11-11-11    2     
    22-22-22    1     

我如何计算visit_cause每个医生的访问次数 = '1'?使它看起来像:

    doctor_uid | number_of_visits | visits_with_cause_equal_1
    11-22-33    2                           2
    22-22-22    1                           0

提前致谢!

4

2 回答 2

3

这应该有效:

select v.vra_uid, COUNT(v.visit_uid) as number_of_visits , SUM(case when v.visit_cause = 1 then 1 else 0 end) as visits_with_cause_equal_1
from visits v 
group by v.vra_uid
于 2013-08-26T10:11:15.140 回答
0

你有没有尝试过CASE WHEN visit_cause = '1' THEN COUNT(*)

于 2013-08-26T10:10:53.093 回答