住院患者医疗费用统计 SQL 语句解析 - 西药费、中成药费等费用分类统计
该 SQL 语句用于查询某个时间段内住院患者的各项医疗费用,并按照院区进行分组统计。
select nvl(sum(decode(fun_get_fee_stat_cate('ZY01', t.fee_code),
'01',
t.tot_cost,
0)),
0) 西药费,
nvl(sum(decode(fun_get_fee_stat_cate('ZY01', t.fee_code),
'02',
t.tot_cost,
0)),
0) 中成药费,
nvl(sum(decode(fun_get_fee_stat_cate('ZY01', t.fee_code),
'03',
t.tot_cost,
0)),
0) 中草药费,
nvl(sum(decode(fun_get_fee_stat_cate('ZY01', t.fee_code),
'04',
t.tot_cost,
0)),
0) 挂号费,
nvl(sum(decode(fun_get_fee_stat_cate('ZY01', t.fee_code),
'05',
t.tot_cost,
0)),
0) 床位费,
nvl(sum(decode(fun_get_fee_stat_cate('ZY01', t.fee_code),
'06',
t.tot_cost,
0)),
0) 诊疗费,
nvl(sum(decode(fun_get_fee_stat_cate('ZY01', t.fee_code),
'07',
t.tot_cost,
0)),
0) 检查费,
nvl(sum(decode(fun_get_fee_stat_cate('ZY01', t.fee_code),
'08',
t.tot_cost,
0)),
0) 治疗费,
nvl(sum(decode(fun_get_fee_stat_cate('ZY01', t.fee_code),
'09',
t.tot_cost,
0)),
0) 护理费,
nvl(sum(decode(fun_get_fee_stat_cate('ZY01', t.fee_code),
'10',
t.tot_cost,
0)),
0) 手术费,
nvl(sum(decode(fun_get_fee_stat_cate('ZY01', t.fee_code),
'11',
t.tot_cost,
0)),
0) 化验费,
nvl(sum(decode(fun_get_fee_stat_cate('ZY01', t.fee_code),
'12',
t.tot_cost,
0)),
0) 放射费,
nvl(sum(decode(fun_get_fee_stat_cate('ZY01', t.fee_code),
'14',
t.tot_cost,
0)),
0) 其他费,
DECODE(b.hospital_didt,'YLV21051','昆明医科大学第一附属医院','43120189-1-2','昆明医科大学第一附属医院呈贡医院') 院区
from fin_ipb_feeinfo t,fin_ipr_inmaininfo b
where t.balance_state = '1'
AND t.inpatient_no=b.inpatient_no
and t.fee_date >= to_date('2023-04-18 00:00:00','yyyy-mm-dd hh24:mi:ss')
and t.fee_date <= to_date('2023-04-18 10:00:00','yyyy-mm-dd hh24:mi:ss')
and t.balance_date > to_date('2023-04-18 10:00:00','yyyy-mm-dd hh24:mi:ss')
GROUP BY DECODE(b.hospital_didt,'YLV21051','昆明医科大学第一附属医院','43120189-1-2','昆明医科大学第一附属医院呈贡医院')
代码详解
1. 费用分类统计
例如:nvl(sum(decode(fun_get_fee_stat_cate('ZY01', t.fee_code), '01', t.tot_cost, 0)), 0) 西药费 这部分代码表示计算西药费用的部分,解释如下:
nvl()函数用于处理空值,如果计算结果为空,则返回 0。sum()函数用于对一列数值求和。decode()函数用于进行条件判断,判断t.fee_code是否等于 '01',如果是,则返回t.tot_cost,否则返回 0。fun_get_fee_stat_cate()是自定义的函数,用于获取费用类别。
因此,该部分的意思是,将所有费用类别为西药费的费用金额进行求和。
2. 院区分组统计
DECODE(b.hospital_didt,'YLV21051','昆明医科大学第一附属医院','43120189-1-2','昆明医科大学第一附属医院呈贡医院')表示根据b.hospital_didt字段的值,将院区分组,'YLV21051' 对应 '昆明医科大学第一附属医院','43120189-1-2' 对应 '昆明医科大学第一附属医院呈贡医院'。GROUP BY DECODE(b.hospital_didt,'YLV21051','昆明医科大学第一附属医院','43120189-1-2','昆明医科大学第一附属医院呈贡医院')表示按照院区进行分组统计。
3. 其他部分
from fin_ipb_feeinfo t,fin_ipr_inmaininfo b表示从两个表fin_ipb_feeinfo和fin_ipr_inmaininfo中获取数据。where t.balance_state = '1' AND t.inpatient_no=b.inpatient_no表示筛选条件,包括费用状态和住院号的匹配。t.fee_date >= to_date('2023-04-18 00:00:00','yyyy-mm-dd hh24:mi:ss') and t.fee_date <= to_date('2023-04-18 10:00:00','yyyy-mm-dd hh24:mi:ss') and t.balance_date > to_date('2023-04-18 10:00:00','yyyy-mm-dd hh24:mi:ss')表示筛选 2023-04-18 00:00:00 到 2023-04-18 10:00:00 之间的费用记录,并且费用结算时间大于 2023-04-18 10:00:00。
总结
该 SQL 语句使用了 nvl()、sum()、decode() 等函数,以及 group by 语句,实现了对住院患者医疗费用的分类统计和院区分组统计,可以方便地获取各个院区不同费用类别的总额。
原文地址: https://www.cveoy.top/t/topic/nwCq 著作权归作者所有。请勿转载和采集!