Oracle使用decode实现行转列

HIS开发中,在各种统计时经常会用到行转列,下面简单记录一下使用decode实现行转列


表:

住院号 费用 费用分类
001 10.00 西药费
001 20.00 中药费
001 30.00 西药费
002 10.00 检查费
002 20.00

西药费


要实现效果:

住院号 西药费 中药费 检查费
001 40.00 20.00 0
002 20.00 0 10.00


详细实现语句如下:


select

住院号,

sum(decode(费用分类,'西药费',费用,0)) 西药费,

sum(decode(费用分类,'中药费',费用,0)) 中药费,

sum(decode(费用分类,'检查费',费用,0)) 检查费

from 费用表

group by 住院号






你可能感兴趣的:(HIS)