日期:2014-05-17 浏览次数:20888 次
SELECT T1.科室,
(SELECT SUM(费用)
FROM T
WHERE T.科室 = T1.科室
AND T.费用类别 = '药费') "药费",
(SELECT SUM(费用)
FROM T
WHERE T.科室 = T1.科室
AND T.费用类别 = '耗材') "耗材",
(SELECT SUM(费用)
FROM T
WHERE T.科室 = T1.科室
AND T.费用类别 NOT IN ('药费', '耗材')) "其他"
FROM (SELECT DISTINCT 科室 FROM T) T1
SELECT 科室,
SUM(CASE WHEN 费用类别 = '药费' THEN 费用 ELSE 0 END ) '药费',
SUM(CASE WHEN 费用类别 = '耗材' THEN 费用 ELSE 0 END ) '耗材',
SUM(CASE WHEN 费用类别 NOT IN ('药费','耗材') THEN 费用 ELSE 0 END ) '其它'
FROM TB
GROUP BY 科室
ORDER BY 科室
WITH test AS(
SELECT 'A' 科室,'药费' 费用类别,10 费用 FROM dual UNION ALL
SELECT 'B' ,'药费' ,20 FROM dual UNION ALL
SELECT 'C' ,'药费' ,20 FROM dual UNION ALL
SELECT 'A' ,'耗材' ,10 FROM dual UNION ALL
SELECT 'B' ,'耗材' ,20 FROM dual UNION ALL
SELECT 'C' ,'耗材' ,20 FROM dual UNION ALL
SELECT&nb