我的桌子上有医生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
提前致谢!
答案 0 :(得分:1)
这应该有效:
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
答案 1 :(得分:0)
你试过CASE WHEN visit_cause = '1' THEN COUNT(*)