题解 | 医院门诊复诊率与抗生素用药占比统计

医院门诊复诊率与抗生素用药占比统计

https://www.nowcoder.com/practice/7adcef0b1fb741fbba255870422cdb43

with feb_count_temp as (
select dept,count(distinct v.visit_id) as feb_2024_visits,
count(distinct patient_id ) as feb_2024_unique_patients,
coalesce(round(sum(is_antibiotic)/count(prescription_id)*100,2),0)
 as  feb_2024_antibiotic_rate
from  visits v
left join prescriptions p on p.visit_id=v.visit_id
where visit_date between '2024-02-01' and  '2024-02-29'
group by  dept
),
rev_temp as (
select dept,coalesce(round(sum(case when prev_date is not null and 
timestampdiff(day,prev_date,visit_date)<30 then 1 else 0 end )/ count(*)*100,2),0) as feb_2024_revisit_rate
from (select patient_id,visit_id,dept,
visit_date,lag(visit_date) over(partition by dept,patient_id order by visit_date)
as prev_date
 from visits ) t
where visit_date between '2024-02-01' and  '2024-02-29'
group by dept
)
select rt.dept,feb_2024_visits,feb_2024_unique_patients,feb_2024_revisit_rate,feb_2024_antibiotic_rate
from  feb_count_temp fct 
join rev_temp   rt on rt.dept=fct.dept;

全部评论

相关推荐

在改简历的大卫很认真:天天有面试 = 你已经在 offer 门口了。 海投能面成这样,说明你的简历、基础、学历都是过关的,缺的只是一次刚好匹配的缘分。 关于你说的 SQL 恐惧,我帮你捋一下: - 面试里考来考去,真就那几类: 分组、去重、关联、子查询、窗口函数(row_number、rank、sum 开窗) ​ - 面试官要的不是“写得花里胡哨”,而是思路稳、不出错。 你恐惧的本质不是不会, 是怕临场卡壳、怕写错、怕被追问。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务