Oracle数据库创建视图(用union/union all连接多表)并且调用

本文是关于在Oracle数据库中,实现用视图展示多表数据集合的方法。
具体如下所示:


实现过程

创建一个package,包中定义两个方法,一个set值的方法,一个get值的方法

--定义包
create or replace package v_param_date is
  --set start_date值方法
  function set_start_date(d_start date) return date;
  --get start_date值方法
  function get_start_date return date;

  --set end_date值方法
  function set_end_date(d_end date) return date;
  --get end_date值方法
  function get_end_date return date;
end v_param_date;

创建package body,实现package中的两个方法

--包方法实现
create or replace package body v_param_date is
  day_start date;
  day_end date;
  -- 给day_start赋值
  function set_start_date(d_start date) return date is
  begin
    day_start := d_start;
    return d_start;
  end;
 --返回day_start的值
  function get_start_date return date is
  begin
    return day_start;
  end;

  -- 给day_end赋值
  function set_end_date(d_end date) return date is
  begin
    day_end := d_end;
    return d_end;
  end;
  --返回day_end的值
  function get_end_date return date is
  begin
    return day_end;
  end;
end v_param_date;

创建带参数视图,通过v_param_date.get_start_date()或者v_param_date.get_end_date()方法获取传入的参数

--定义v_nursing_workload查询
create or replace view v_nursing_workload 
As 
select 科室名称,护理单元,sum(总床日数) as 总床日数,sum(出院人数) as 出院人数,sum(特护人次) as 特护人次,sum(一护人次) as 一护人次,sum(二护人次) as 二护人次,sum(计出入量) as 计出入量,sum(监测血压) as 监测血压,
       sum(测体重) as 测体重,sum(BD针) as BD针,0 as 质量考核扣分,sum(治疗护理量) as 治疗护理量,0 as 实得分值 from (
--科室名称、护理单元、总床日数、出院人数、特护人次、一护人次、二护人次
select 科室名称,护理单元,sum(总床日数) "总床日数",sum(出院人数) "出院人数",sum(特护人次) "特护人次",sum(一护人次) "一护人次",sum(二护人次) "二护人次",0 as 计出入量,0 as 监测血压,0 as 测体重,
        0 as BD针,0 as 治疗护理量
  from (
        select 
               --v.dept_code "科室编码",
               (select dept_dict.dept_name from dept_dict where dept_dict.dept_code = v.dept_code) "科室名称",
               --d.dept_code "护理单元编码",
               t.dept_name "护理单元",
               --to_char(t.send_date, 'yyyy-mm-dd') "护理日期",
               t.kfcw "总床日数",
               t.cyrs "出院人数",
               t.tlrs "特护人次",
               t.yjhl "一护人次",
               t.ejhl "二护人次"
          from tdhis_send_data t, dept_dict d, dept_vs_ward v
         where 
           t.send_date between v_param_date.get_start_date and v_param_date.get_end_date
           and t.dept_name = d.dept_name
           and d.dept_code = v.ward_code)
 group by 科室名称, 护理单元
having count(*) > 1
union all
select t1.科室名称,(select dept_dict.dept_name from dept_dict where dept_dict.dept_code = v1.ward_code) as 护理单元,
       0 as 总床日数,0 as 出院人数,0 as 特护人次,0 as 一护人次,0 as 二护人次,t1.计出入量,t1.监测血压,t1.测体重,t1.BD针,t1.治疗护理量 from (
select 科室名称,sum(计出入量) as 计出入量,sum(监测血压) as 监测血压,sum(测体重) as 测体重,sum(BD针) as BD针,sum(治疗护理量) as 治疗护理量 from (
--计出入量、监测血压、测体重
select 科室名称,sum(jcrl) as 计出入量,sum(jcxy) as 监测血压,sum(ctz) as 测体重,0 as BD针,0 as 治疗护理量
  from (select b.dept_name "科室名称",
               sum(ceil(a.stop_date_time - a.start_date_time)) as jcrl,--ceil()获取两时间的相差天数
               0 as jcxy,
               0 as ctz
          from orders a, dept_dict b
         where a.ordering_dept = b.dept_code
           and a.start_date_time between v_param_date.get_start_date and v_param_date.get_end_date
           and a.order_text like '%出入量%'
         group by b.dept_name
        union
        select b.dept_name "科室名称",
               0 as jcrl,
               sum(ceil(a.stop_date_time - a.start_date_time)) as jcxy,
               0 as ctz
          from orders a, dept_dict b, dept_vs_ward v
         where a.ordering_dept = b.dept_code
           and a.start_date_time between v_param_date.get_start_date and v_param_date.get_end_date
           and a.order_text like '%监测血压%'
         group by b.dept_name
        union
        select b.dept_name "科室名称",
               0 as jcrl,
               0 as jcxy,
               sum(ceil(a.stop_date_time - a.start_date_time)) as ctz
          from orders a, dept_dict b
         where a.ordering_dept = b.dept_code
           and a.start_date_time between v_param_date.get_start_date and v_param_date.get_end_date
           and a.order_text like '%体重%'
         group by b.dept_name)
group by 科室名称
union all
--BD针、治疗护理量
select 科室名称,0 as 计出入量,0 as 监测血压,0 as 测体重,sum(BD针) as BD针,sum(治疗护理量) as 治疗护理量 from (
--BD留置针
select d.dept_name "科室名称",sum(i.amount) "BD针",0 as 治疗护理量
  from inp_bill_detail i, dept_dict d,dept_vs_ward v
 where i.ward_code = v.ward_code and v.dept_code = d.dept_code
   and i.billing_date_time between v_param_date.get_start_date and v_param_date.get_end_date
   and i.item_name like '%BD静脉留置针%'
group by d.dept_name
union
--治疗护理量
SELECT d.dept_name "科室名称",0 as BD针,       
       --sum(decode(r.class_name,'护理费',i.charges,0)) as 护理费,
       --sum(decode(r.class_name,'治疗费',i.charges,0)) as 治疗费,
       --sum(decode(i.item_code,'230600015',i.charges,0)) as 云克收入,
       sum(decode(r.class_name,'护理费',i.charges,0))+sum(decode(r.class_name,'治疗费',i.charges,0))-sum(decode(i.item_code,'230600015',i.charges,0)) as 治疗护理量
  FROM dept_dict d,
       reck_item_class_dict r,
       inp_bill_detail i
 WHERE i.class_on_reckoning = r.class_code(+)
   and d.dept_code(+)=i.ordered_by
   --and to_char(i.billing_date_time, 'yyyy-mm-dd') >= '2018-06-26'
   --and to_char(i.billing_date_time, 'yyyy-mm-dd') <= '2018-07-25'
   and i.billing_date_time between v_param_date.get_start_date and v_param_date.get_end_date
 GROUP BY d.dept_name
)group by 科室名称)group by 科室名称) t1,dept_dict d1,dept_vs_ward v1
where t1.科室名称=d1.dept_name and d1.dept_code=v1.dept_code)
group by 科室名称,护理单元;

调用视图查询报表

--查询传入参数day_start和day_end,调用v_param_date视图
select *
  from v_nursing_workload
 where v_param_date.set_start_date(to_date('2018-06-25', 'yyyy-mm-dd'))= to_date('2018-06-25', 'yyyy-mm-dd')
       and v_param_date.set_end_date(to_date('2018-07-25', 'yyyy-mm-dd'))= to_date('2018-07-25', 'yyyy-mm-dd')

参考文章:
ORACLE 创建带参数视图-实践
在Oracle中如何创建带参数的视图

猜你喜欢

转载自blog.csdn.net/phoenixmajie/article/details/81865751