create or replace view v8_sys_hs_arr_exproom as
select wdat_date,sum(exproom) exproom ,sum(exproomDayUse) exproomDayUse from (
select 
   wdat_date
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
    ) exproom
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)
    ) exproomDayUse
from wdat,ydet yd,yres y,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is null
and nvl(ydet_yrms_id,0)=0
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and (nvl(ydet_sharenum,0)= 0
     or
	 not exists (select d.ydet_sharenum 
	                       from ydet d,yres r 
				where d.ydet_yres_id=r.yres_id
							 and d.ydet_id<>yd.ydet_id 
							 and (y.yres_exparrtime<>r.yres_exparrtime or r.yres_actarrtime is not null)
							 and d.ydet_sharenum=yd.ydet_sharenum
				)
	)
group by wdat_date,ydet_sharenum
) group by wdat_date;

CREATE OR REPLACE VIEW V8_SYS_HS_ARR_EXPROOM_CAD
AS 
select wdat_date,ycad_id,sum(exproom) exproom,sum(exproomDayUse) exproomDayUse from (
select 
   wdat_date,
   ycat_sum_roomtype_ycad_id ycad_id
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
    ) exproom
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)
    ) exproomDayUse
from wdat,ydet yd,yres y,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is null
and nvl(ydet_yrms_id,0)=0
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and (nvl(ydet_sharenum,0)= 0
     or
	 not exists (select d.ydet_sharenum 
	                       from ydet d,yres r 
				where d.ydet_yres_id=r.yres_id
							 and d.ydet_id<>yd.ydet_id 
							 and (y.yres_exparrtime<>r.yres_exparrtime or r.yres_actarrtime is not null)
							 and d.ydet_sharenum=yd.ydet_sharenum
				)
	)
group by wdat_date,ycat_sum_roomtype_ycad_id,ydet_sharenum
) group by wdat_date,ycad_id;

create or replace view v8_sys_hs_arr_exproom_xcma as
select wdat_date, wdat_date day,ydet_xcma_id xcma_id,sum(exproom) exproom,sum(exproomDayUse) exproomDayUse from (
select 
   wdat_date
   ,ydet_xcma_id
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
    ) exproom
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)
    ) exproomDayUse
from wdat,ydet yd,yres y,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is null
--and nvl(ydet_yrms_id,0)=0 in this view there is no separate exproom/preroom version needed.  
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and (nvl(ydet_sharenum,0)= 0
     or
	 not exists (select d.ydet_sharenum 
	                       from ydet d,yres r 
				where d.ydet_yres_id=r.yres_id
							 and d.ydet_id<>yd.ydet_id 
							 and (y.yres_exparrtime<>r.yres_exparrtime or r.yres_actarrtime is not null)
							 and d.ydet_sharenum=yd.ydet_sharenum
							 and d.ydet_xcma_id=yd.ydet_xcma_id
				)
	)
group by wdat_date,ydet_sharenum,ydet_xcma_id
) group by wdat_date,ydet_xcma_id;

create or replace view v8_sys_hs_arr_allroom as
select wdat_date
      ,sum(allexproom) allexproom,sum(defexproom) defexproom,sum(tentexproom) tentexproom  
      ,sum(allexproomDayUse) allexproomDayUse,sum(defexproomDayUse) defexproomDayUse,sum(tentexproomDayUse) tentexproomDayUse  
from (
select 
   wdat_date
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) allexproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) defexproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) tentexproom
   ,
    decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) allexproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) defexproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) tentexproomDayUse
   from wdat,ydet yd,yres y,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and (nvl(ydet_sharenum,0)=0
     or
	 not exists (select d.ydet_sharenum 
	                       from ydet d,yres r 
				where d.ydet_yres_id=r.yres_id
							 and d.ydet_id<>yd.ydet_id 
							 and (y.yres_exparrtime<>r.yres_exparrtime)
							 and d.ydet_sharenum=yd.ydet_sharenum
				)
	)
group by wdat_date,ydet_sharenum
) group by wdat_date;


create or replace view v8_sys_hs_arr_allroom_xcma as
select wdat_date,xcma_id
      ,sum(allexproom) allexproom,sum(defexproom) defexproom,sum(tentexproom) tentexproom  
      ,sum(allexproomDayUse) allexproomDayUse,sum(defexproomDayUse) defexproomDayUse,sum(tentexproomDayUse) tentexproomDayUse  
from (
select 
   wdat_date
   ,ydet_xcma_id xcma_id
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) allexproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) defexproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) tentexproom
   ,
    decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) allexproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) defexproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) tentexproomDayUse
from wdat,ydet yd,yres y,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and (nvl(ydet_sharenum,0)=0
     or
	 not exists (select d.ydet_sharenum 
	                       from ydet d,yres r 
				where d.ydet_yres_id=r.yres_id
							 and d.ydet_id<>yd.ydet_id 
							 and (y.yres_exparrtime<>r.yres_exparrtime)
							 and d.ydet_sharenum=yd.ydet_sharenum
				)
	)
group by wdat_date,ydet_sharenum,ydet_xcma_id
) group by wdat_date,xcma_id;

create or replace view v8_sys_hs_arr_allroom_source as
select wdat_date,source_id
      ,sum(allexproom) allexproom,sum(defexproom) defexproom,sum(tentexproom) tentexproom  
      ,sum(allexproomDayUse) allexproomDayUse,sum(defexproomDayUse) defexproomDayUse,sum(tentexproomDayUse) tentexproomDayUse  
from (
select 
   wdat_date
   ,yral_yras_id source_id
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) allexproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) defexproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) tentexproom
   ,
    decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) allexproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) defexproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) tentexproomDayUse
from wdat,ydet yd,yres y,ycat,yral
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and yral_ydet_id=ydet_id
and yral_yrac_id=1
and (nvl(ydet_sharenum,0)=0
     or
	 not exists (select d.ydet_sharenum 
	                       from ydet d,yres r 
				where d.ydet_yres_id=r.yres_id
							 and d.ydet_id<>yd.ydet_id 
							 and (y.yres_exparrtime<>r.yres_exparrtime)
							 and d.ydet_sharenum=yd.ydet_sharenum
				)
	)
group by wdat_date,ydet_sharenum,yral_yras_id
) group by wdat_date,source_id;

--per roomtype
create or replace view v8_sys_hs_arr_allroom_prt as
select wdat_date,ydet_ycat_id
      ,sum(allexproom) allexproom,sum(defexproom) defexproom,sum(tentexproom) tentexproom  
      ,sum(allexproomDayUse) allexproomDayUse,sum(defexproomDayUse) defexproomDayUse,sum(tentexproomDayUse) tentexproomDayUse  
from (
select 
   wdat_date
   ,ydet_ycat_id
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) allexproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) defexproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) tentexproom
   ,
    decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) allexproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) defexproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) tentexproomDayUse
from wdat,ydet yd,yres y
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and ydet_ycat_id>0
and (nvl(ydet_sharenum,0)=0
     or
	 not exists (select d.ydet_sharenum 
	                       from ydet d,yres r 
				where d.ydet_yres_id=r.yres_id
							 and d.ydet_id<>yd.ydet_id 
							 and (y.yres_exparrtime<>r.yres_exparrtime)
							 and d.ydet_sharenum=yd.ydet_sharenum
				)
	)
group by wdat_date,ydet_sharenum,ydet_ycat_id
) group by wdat_date,ydet_ycat_id;

CREATE OR REPLACE VIEW V8_SYS_HS_ARR_ALLROOM_CAD
AS 
SELECT wdat_date,ycat_sum_roomtype_ycad_id YCAD_ID
      ,sum(allexproom) allexproom,sum(defexproom) defexproom,sum(tentexproom) tentexproom  
      ,sum(allexproomDayUse) allexproomDayUse,sum(defexproomDayUse) defexproomDayUse,sum(tentexproomDayUse) tentexproomDayUse  
from (
SELECT 
   wdat_date
   ,ycat_sum_roomtype_ycad_id
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) allexproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) defexproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) tentexproom
   ,
    decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) allexproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) defexproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) tentexproomDayUse
FROM WDAT,YDET yd,YRES y,YCAT
WHERE
    ydet_date=wdat_date 
AND yres_exparrtime=wdat_date
AND ydet_yres_id=yres_id
AND ydet_ycat_id=ycat_id
AND ycat_notinavl=0
AND yres_resstatus<>3
AND (NVL(ydet_sharenum,0)=0
     OR
  NOT EXISTS (SELECT d.ydet_sharenum 
                        FROM YDET d,YRES r 
         WHERE d.ydet_yres_id=r.yres_id
        AND d.ydet_id<>yd.ydet_id 
        AND (y.yres_exparrtime<>r.yres_exparrtime)
        AND d.ydet_sharenum=yd.ydet_sharenum
         )
 )
GROUP BY wdat_date,ydet_sharenum,ycat_sum_roomtype_ycad_id
) GROUP BY wdat_date,ycat_sum_roomtype_ycad_id;


create or replace view v8_sys_hs_arr_expadult as
select 
   wdat_date
   ,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expadult
   ,sum(ydet_adultno*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expadultDayUse
from wdat,ydet,yres,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is null
and nvl(ydet_yrms_id,0)=0
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date;

create or replace view v8_sys_hs_arr_expadult_bed as
select 
   wdat_date
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expadult
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expadultDayUse
from wdat,ydet,yres,ycat,yrpe,ydrb
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_actarrtime is null
and yrpe_yres_id=yres_id
and yrpe_xcac_Id=0
and ydrb_ydet_id=ydet_id
and ydrb_xcac_id=yrpe_xcac_id
and ydrb_xcacindex=yrpe_xcacindex
and ydrb_yrms_id is null
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
group by wdat_date;

create or replace view v8_sys_hs_arr_grp_expadult as
select 
   wdat_date
   ,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expadult
   ,sum(ydet_adultno*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expadultDayUse
from wdat,ydet,yres,ycat, ygrp
where
    ydet_date=wdat_date 
and yres_ygrp_id  = ygrp_id     
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is null
and nvl(ydet_yrms_id,0)=0
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date;

create or replace view v8_sys_hs_arr_grp_expadult_bed as
select 
   wdat_date
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expadult
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expadultDayUse
from wdat,ydet,yres,ycat,ygrp,yrpe,ydrb
where
    ydet_date=wdat_date 
and yres_ygrp_id=ygrp_id     
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_actarrtime is null
and yrpe_yres_id=yres_id
and yrpe_xcac_Id=0
and ydrb_ydet_id=ydet_id
and ydrb_xcac_id=yrpe_xcac_id
and ydrb_xcacindex=yrpe_xcacindex
and ydrb_yrms_id is null
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
group by wdat_date;

CREATE OR REPLACE VIEW V8_SYS_HS_ARR_EXPADULT_CAD
AS 
select 
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expadult
   ,sum(ydet_adultno*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expadultDayUse
from wdat,ydet,yres,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is null
and nvl(ydet_yrms_id,0)=0
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date, ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_arr_expadult_bed_cad
as 
select 
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expadult
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expadultdayuse
from wdat,ydet,yres,ycat,yrpe,ydrb
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_actarrtime is null
and yrpe_yres_id=yres_id
and yrpe_xcac_Id=0
and ydrb_ydet_id=ydet_id
and ydrb_xcac_id=yrpe_xcac_id
and ydrb_xcacindex=yrpe_xcacindex
and ydrb_yrms_id is null
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
group by wdat_date, ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_arr_expchild as
select 
   wdat_date
   ,sum(yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expchild
   ,sum(yrgl_number*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expchildDayUse
from wdat,ydet,yres,yrgl,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is null
and nvl(ydet_yrms_id,0)=0
and ydet_id=yrgl_ydet_id
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date;

create or replace view v8_sys_hs_arr_expchild_bed as
select 
   wdat_date
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expchild
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expchildDayUse
from wdat,ydet,yres,ycat,yrpe,ydrb
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_actarrtime is null
and yrpe_yres_id=yres_id
and yrpe_xcac_Id>0
and ydrb_ydet_id=ydet_id
and ydrb_xcac_id=yrpe_xcac_id
and ydrb_xcacindex=yrpe_xcacindex
and ydrb_yrms_id is null
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
group by wdat_date;

create or replace view v8_sys_hs_arr_expchild_prt as
select 
   wdat_date
   ,ycat_id
   ,sum(yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expchild
   ,sum(yrgl_number*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expchildDayUse
from wdat,ydet,yres,yrgl,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is null
and nvl(ydet_yrms_id,0)=0
and ydet_id=yrgl_ydet_id
and ycat_id=ydet_ycat_id
and yres_bed=0
group by wdat_date,ycat_id;

create or replace view v8_sys_hs_arr_expchild_bed_prt as
select 
   wdat_date
   ,ycat_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expchild
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expchildDayUse
from wdat,ydet,yres,ycat,yrpe,ydrb
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_actarrtime is null
and yrpe_yres_id=yres_id
and yrpe_xcac_Id>0
and ydrb_ydet_id=ydet_id
and ydrb_xcac_id=yrpe_xcac_id
and ydrb_xcacindex=yrpe_xcacindex
and ydrb_yrms_id is null
and ycat_id=ydet_bed_ycat_id
and yres_bed=1
group by wdat_date,ycat_id;

create or replace view v8_sys_hs_arr_expchild_prt_rb as
select day,ycat_id,sum(expChild) expChild,sum(expchildDayUse) expchildDayUse 
from (
select wdat_date day,ycat_id,expChild,expchildDayUse from v8_sys_hs_arr_expchild_prt
union 
select wdat_date day,ycat_id,expChild,expchildDayUse from v8_sys_hs_arr_expchild_bed_prt
) group by day,ycat_id;

create or replace view v8_sys_hs_arr_expchild_cad as
select 
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expchild
   ,sum(yrgl_number*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expchildDayUse
from wdat,ydet,yres,yrgl,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is null
and nvl(ydet_yrms_id,0)=0
and ydet_id=yrgl_ydet_id
and ycat_id=ydet_ycat_id
AND ycat_notinavl=0
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_arr_expchild_bed_cad as
select 
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expchild
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expchildDayUse
from wdat,ydet,yres,ycat,yrpe,ydrb
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_actarrtime is null
and yrpe_yres_id=yres_id
and yrpe_xcac_Id>0
and ydrb_ydet_id=ydet_id
and ydrb_xcac_id=yrpe_xcac_id
and ydrb_xcacindex=yrpe_xcacindex
and ydrb_yrms_id is null
and ycat_id=ydet_bed_ycat_id
AND ycat_notinavl=0
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_arr_alladult as
select wdat_date
   ,sum(allexpadult) allexpadult,sum(defexpadult) defexpadult,sum(tentexpadult) tentexpadult  
   ,sum(allexpadultDayUse) allexpadultDayUse,sum(defexpadultDayUse) defexpadultDayUse,sum(tentexpadultDayUse) tentexpadultDayUse  
from (
select 
   wdat_date
   ,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) allexpadult
   ,decode(yres_resstatus,1,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) defexpadult
   ,decode(yres_resstatus,0,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) tentexpadult
   ,
    sum(ydet_adultno*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) allexpadultDayUse
   ,decode(yres_resstatus,1,sum(ydet_adultno*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) defexpadultDayUse
   ,decode(yres_resstatus,0,sum(ydet_adultno*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) tentexpadultDayUse
from wdat,ydet,yres,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date,yres_resstatus
) group by wdat_date;

create or replace view v8_sys_hs_arr_alladult_prt as
select wdat_date,ydet_ycat_id
   ,sum(allexpadult) allexpadult,sum(defexpadult) defexpadult,sum(tentexpadult) tentexpadult  
   ,sum(allexpadultDayUse) allexpadultDayUse,sum(defexpadultDayUse) defexpadultDayUse,sum(tentexpadultDayUse) tentexpadultDayUse  
from (
select 
   wdat_date
   ,ydet_ycat_id
   ,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) allexpadult
   ,decode(yres_resstatus,1,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) defexpadult
   ,decode(yres_resstatus,0,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) tentexpadult
   ,
    sum(ydet_adultno*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) allexpadultDayUse
   ,decode(yres_resstatus,1,sum(ydet_adultno*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) defexpadultDayUse
   ,decode(yres_resstatus,0,sum(ydet_adultno*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) tentexpadultDayUse
from wdat,ydet,yres,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date,yres_resstatus,ydet_ycat_id
) group by wdat_date,ydet_ycat_id;

create or replace view v8_sys_hs_arr_alladult_bed as
select wdat_date
   ,sum(allexpadult) allexpadult,sum(defexpadult) defexpadult,sum(tentexpadult) tentexpadult  
   ,sum(allexpadultDayUse) allexpadultDayUse,sum(defexpadultDayUse) defexpadultDayUse,sum(tentexpadultDayUse) tentexpadultDayUse  
from (
select 
   wdat_date
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) allexpadult
   ,decode(yres_resstatus,1,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) defexpadult
   ,decode(yres_resstatus,0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) tentexpadult
   ,
    sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) allexpadultDayUse
   ,decode(yres_resstatus,1,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) defexpadultDayUse
   ,decode(yres_resstatus,0,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) tentexpadultDayUse
from wdat,ydet,yres,ycat,yrpe
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
and yrpe_yres_id=yres_id
and yrpe_xcac_id=0
and yrpe_noshow=0
group by wdat_date,yres_resstatus
) group by wdat_date;

create or replace view v8_sys_hs_arr_alladult_bed_prt as
select wdat_date,ydet_ycat_id
   ,sum(allexpadult) allexpadult,sum(defexpadult) defexpadult,sum(tentexpadult) tentexpadult  
   ,sum(allexpadultDayUse) allexpadultDayUse,sum(defexpadultDayUse) defexpadultDayUse,sum(tentexpadultDayUse) tentexpadultDayUse  
from (
select 
   wdat_date
   ,ydet_any_ycat_id ydet_ycat_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) allexpadult
   ,decode(yres_resstatus,1,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) defexpadult
   ,decode(yres_resstatus,0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) tentexpadult
   ,
    sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) allexpadultDayUse
   ,decode(yres_resstatus,1,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) defexpadultDayUse
   ,decode(yres_resstatus,0,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) tentexpadultDayUse
from wdat,ydet,yres,yrpe
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_yres_id=yres_id
and yrpe_xcac_id=0
and yrpe_noshow=0
group by wdat_date,yres_resstatus,ydet_any_ycat_id
) group by wdat_date,ydet_ycat_id;

CREATE OR REPLACE VIEW V8_SYS_HS_ARR_ALLADULT_CAD
AS 
SELECT wdat_date,ycat_sum_roomtype_ycad_id YCAD_ID
   ,sum(allexpadult) allexpadult,sum(defexpadult) defexpadult,sum(tentexpadult) tentexpadult  
   ,sum(allexpadultDayUse) allexpadultDayUse,sum(defexpadultDayUse) defexpadultDayUse,sum(tentexpadultDayUse) tentexpadultDayUse  
from (
SELECT 
   wdat_date
   ,ycat_sum_roomtype_ycad_id
   ,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) allexpadult
   ,decode(yres_resstatus,1,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) defexpadult
   ,decode(yres_resstatus,0,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) tentexpadult
   ,
    sum(ydet_adultno*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) allexpadultDayUse
   ,decode(yres_resstatus,1,sum(ydet_adultno*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) defexpadultDayUse
   ,decode(yres_resstatus,0,sum(ydet_adultno*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) tentexpadultDayUse
FROM WDAT,YDET,YRES,YCAT
WHERE
    ydet_date=wdat_date 
AND yres_exparrtime=wdat_date
AND ydet_yres_id=yres_id
AND ydet_ycat_id=ycat_id
AND ycat_notinavl=0
AND yres_resstatus<>3
GROUP BY wdat_date,yres_resstatus,ycat_sum_roomtype_ycad_id
) GROUP BY wdat_date,ycat_sum_roomtype_ycad_id;

CREATE OR REPLACE VIEW V8_SYS_HS_ARR_ALLADULT_bed_CAD
AS 
SELECT wdat_date,ycat_sum_roomtype_ycad_id YCAD_ID
   ,sum(allexpadult) allexpadult,sum(defexpadult) defexpadult,sum(tentexpadult) tentexpadult  
   ,sum(allexpadultDayUse) allexpadultDayUse,sum(defexpadultDayUse) defexpadultDayUse,sum(tentexpadultDayUse) tentexpadultDayUse  
from (
SELECT 
   wdat_date
   ,ycat_sum_roomtype_ycad_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) allexpadult
   ,decode(yres_resstatus,1,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) defexpadult
   ,decode(yres_resstatus,0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) tentexpadult
   ,
    sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) allexpadultDayUse
   ,decode(yres_resstatus,1,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) defexpadultDayUse
   ,decode(yres_resstatus,0,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) tentexpadultDayUse
FROM WDAT,YDET,YRES,YCAT,yrpe
WHERE
    ydet_date=wdat_date 
AND yres_exparrtime=wdat_date
AND ydet_yres_id=yres_id
AND ydet_bed_ycat_id=ycat_id
AND ycat_notinavl=0
AND yres_resstatus<>3
and yrpe_yres_id=yres_id
and yrpe_xcac_id=0
and yrpe_noshow=0
GROUP BY wdat_date,yres_resstatus,ycat_sum_roomtype_ycad_id
) GROUP BY wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_arr_allchild as
select 
    wdat_date
   ,sum(allarrchild) allarrchild,sum(defarrchild) defarrchild,sum(tentarrchild) tentarrchild 
   ,sum(allarrdomesticchild) allarrdomesticchild,sum(defarrdomesticchild) defarrdomesticchild,sum(tentarrdomesticchild) tentarrdomesticchild
   ,sum(allarrchildDayUse) allarrchildDayUse,sum(defarrchildDayUse) defarrchildDayUse,sum(tentarrchildDayUse) tentarrchildDayUse 
   ,sum(allarrdomesticchildDayUse) allarrdomesticchildDayUse,sum(defarrdomesticchildDayUse) defarrdomesticchildDayUse,sum(tentarrdomesticchildDayUse) tentarrdomesticchildDayUse
   ,sum(allarrfreeofchargechild) allarrfreeofchargechild, sum(defarrfreeofchargechild) defarrfreeofchargechild , sum(tentarrfreeofchargechild) tentarrfreeofchargechild   
   ,sum(allarrfreeofchargechildDayUse) allarrfreeofchargechildDayUse,sum(defarrfreeofchargechildDayUse) defarrfreeofchargechildDayUse,sum(tentarrfreeofchargechildDayUse) tentarrfreeofchargechildDayUse
from (
select 
    wdat_date
   ,sum(yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) allarrchild
   ,sum(decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) allarrdomesticchild
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,0,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end))) defarrchild
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,0,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end))) tentarrchild
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) defarrdomesticchild
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) tentarrdomesticchild
   ,
    sum(yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) allarrchildDayUse
   ,sum(decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0)) allarrdomesticchildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,0,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end))) defarrchildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,0,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end))) tentarrchildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) defarrdomesticchildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) tentarrdomesticchildDayUse
   ,sum(decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) allarrfreeofchargechild
   ,sum(decode(yres_resstatus,1,decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) defarrfreeofchargechild
   ,sum(decode(yres_resstatus,0,decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) tentarrfreeofchargechild
   ,sum(decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0)) allarrfreeofchargechildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) defarrfreeofchargechildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) tentarrfreeofchargechildDayUse
from wdat,ydet,yres,yrgl,xcac,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrgl_ydet_id=ydet_id
and yrgl_xcac_id=xcac_id
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date,yres_resstatus
) group by wdat_date;

create or replace view v8_sys_hs_arr_allchild_prt as
select 
    wdat_date
   ,ydet_ycat_id
   ,sum(allarrchild) allarrchild,sum(defarrchild) defarrchild,sum(tentarrchild) tentarrchild 
   ,sum(allarrdomesticchild) allarrdomesticchild,sum(defarrdomesticchild) defarrdomesticchild,sum(tentarrdomesticchild) tentarrdomesticchild
   ,sum(allarrchildDayUse) allarrchildDayUse,sum(defarrchildDayUse) defarrchildDayUse,sum(tentarrchildDayUse) tentarrchildDayUse 
   ,sum(allarrdomesticchildDayUse) allarrdomesticchildDayUse,sum(defarrdomesticchildDayUse) defarrdomesticchildDayUse,sum(tentarrdomesticchildDayUse) tentarrdomesticchildDayUse
   ,sum(allarrfreeofchargechild) allarrfreeofchargechild, sum(defarrfreeofchargechild) defarrfreeofchargechild , sum(tentarrfreeofchargechild) tentarrfreeofchargechild   
   ,sum(allarrfreeofchargechildDayUse) allarrfreeofchargechildDayUse,sum(defarrfreeofchargechildDayUse) defarrfreeofchargechildDayUse,sum(tentarrfreeofchargechildDayUse) tentarrfreeofchargechildDayUse
from (
select 
    wdat_date
   ,ydet_ycat_id
   ,sum(yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) allarrchild
   ,sum(decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) allarrdomesticchild
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,0,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end))) defarrchild
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,0,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end))) tentarrchild
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) defarrdomesticchild
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) tentarrdomesticchild
   ,
    sum(yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) allarrchildDayUse
   ,sum(decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0)) allarrdomesticchildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,0,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end))) defarrchildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,0,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end))) tentarrchildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) defarrdomesticchildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) tentarrdomesticchildDayUse
   ,sum(decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) allarrfreeofchargechild
   ,sum(decode(yres_resstatus,1,decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) defarrfreeofchargechild
   ,sum(decode(yres_resstatus,0,decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) tentarrfreeofchargechild
   ,sum(decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0)) allarrfreeofchargechildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) defarrfreeofchargechildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) tentarrfreeofchargechildDayUse
from wdat,ydet,yres,yrgl,xcac,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrgl_ydet_id=ydet_id
and yrgl_xcac_id=xcac_id
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date,yres_resstatus,ydet_ycat_id
) group by wdat_date,ydet_ycat_id;

create or replace view v8_sys_hs_arr_allchild_bed as
select wdat_date
   ,sum(allarrchild) allarrchild,sum(defarrchild) defarrchild,sum(tentarrchild) tentarrchild 
   ,sum(allarrdomesticchild) allarrdomesticchild,sum(defarrdomesticchild) defarrdomesticchild,sum(tentarrdomesticchild) tentarrdomesticchild
   ,sum(allarrchildDayUse) allarrchildDayUse,sum(defarrchildDayUse) defarrchildDayUse,sum(tentarrchildDayUse) tentarrchildDayUse 
   ,sum(allarrdomesticchildDayUse) allarrdomesticchildDayUse,sum(defarrdomesticchildDayUse) defarrdomesticchildDayUse,sum(tentarrdomesticchildDayUse) tentarrdomesticchildDayUse
   ,sum(allarrfreeofchargechild) allarrfreeofchargechild, sum(defarrfreeofchargechild) defarrfreeofchargechild , sum(tentarrfreeofchargechild) tentarrfreeofchargechild   
   ,sum(allarrfreeofchargechildDayUse) allarrfreeofchargechildDayUse,sum(defarrfreeofchargechildDayUse) defarrfreeofchargechildDayUse,sum(tentarrfreeofchargechildDayUse) tentarrfreeofchargechildDayUse
from (
select 
   wdat_date
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) allarrchild
   ,sum(decode(xcac_domestic,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) allarrdomesticchild
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end))) defarrchild
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end))) tentarrchild
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) defarrdomesticchild
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) tentarrdomesticchild
   ,
    sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) allarrchildDayUse
   ,sum(decode(xcac_domestic,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0)) allarrdomesticchildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end))) defarrchildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end))) tentarrchildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) defarrdomesticchildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) tentarrdomesticchildDayUse
   ,sum(decode(xcac_freeofcharge,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) allarrfreeofchargechild
   ,sum(decode(yres_resstatus,1,decode(xcac_freeofcharge,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) defarrfreeofchargechild
   ,sum(decode(yres_resstatus,0,decode(xcac_freeofcharge,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) tentarrfreeofchargechild
   ,sum(decode(xcac_freeofcharge,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0)) allarrfreeofchargechildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_freeofcharge,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) defarrfreeofchargechildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_freeofcharge,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) tentarrfreeofchargechildDayUse
from wdat,ydet,yres,xcac,ycat,yrpe
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
and yrpe_yres_id=yres_id
and yrpe_noshow=0
and yrpe_xcac_id=xcac_id
group by wdat_date,yres_resstatus
) group by wdat_date;

create or replace view v8_sys_hs_arr_allchild_bed_prt as
select wdat_date,ydet_ycat_id   
   ,sum(allarrchild) allarrchild,sum(defarrchild) defarrchild,sum(tentarrchild) tentarrchild 
   ,sum(allarrdomesticchild) allarrdomesticchild,sum(defarrdomesticchild) defarrdomesticchild,sum(tentarrdomesticchild) tentarrdomesticchild
   ,sum(allarrchildDayUse) allarrchildDayUse,sum(defarrchildDayUse) defarrchildDayUse,sum(tentarrchildDayUse) tentarrchildDayUse 
   ,sum(allarrdomesticchildDayUse) allarrdomesticchildDayUse,sum(defarrdomesticchildDayUse) defarrdomesticchildDayUse,sum(tentarrdomesticchildDayUse) tentarrdomesticchildDayUse
   ,sum(allarrfreeofchargechild) allarrfreeofchargechild,sum(defarrfreeofchargechild) defarrfreeofchargechild,sum(tentarrfreeofchargechild) tentarrfreeofchargechild
   ,sum(allarrfreeofchargechildDayUse) allarrfreeofchargechildDayUse,sum(defarrfreeofchargechildDayUse) defarrfreeofchargechildDayUse,sum(tentarrfreeofchargechildDayUse) tentarrfreeofchargechildDayUse
from (
select 
   wdat_date
   ,ydet_any_ycat_id ydet_ycat_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) allarrchild
   ,sum(decode(xcac_domestic,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) allarrdomesticchild
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end))) defarrchild
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end))) tentarrchild
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) defarrdomesticchild
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) tentarrdomesticchild
   ,
    sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) allarrchildDayUse
   ,sum(decode(xcac_domestic,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0)) allarrdomesticchildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end))) defarrchildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end))) tentarrchildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) defarrdomesticchildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) tentarrdomesticchildDayUse
   ,sum(decode(xcac_freeofcharge,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) allarrfreeofchargechild
   ,sum(decode(yres_resstatus,1,decode(xcac_freeofcharge,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) defarrfreeofchargechild
   ,sum(decode(yres_resstatus,0,decode(xcac_freeofcharge,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) tentarrfreeofchargechild
   ,sum(decode(xcac_freeofcharge,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0)) allarrfreeofchargechildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_freeofcharge,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) defarrfreeofchargechildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_freeofcharge,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) tentarrfreeofchargechildDayUse
from wdat,ydet,yres,xcac,yrpe
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_xcac_id=xcac_id
and yrpe_yres_id=yres_id
and yrpe_noshow=0
group by wdat_date,yres_resstatus,ydet_any_ycat_id
) group by wdat_date,ydet_ycat_id;

create or replace view v8_sys_hs_arr_actroom as
select wdat_date,sum(actroom) actroom,sum(actroomDayUse) actroomDayUse
from (
select 
   wdat_date
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) actroom
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)) actroomDayUse
from wdat,ydet yd,yres y,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is not null
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and (nvl(ydet_sharenum,0)= 0
     or
	 not exists (select d.ydet_sharenum 
	                       from ydet d,yres r 
				where d.ydet_yres_id=r.yres_id
							 and d.ydet_id<>yd.ydet_id 
							 and y.yres_exparrtime<>r.yres_exparrtime 
							 and d.ydet_sharenum=yd.ydet_sharenum
				)
	)
group by wdat_date,ydet_sharenum
) group by wdat_date;

create or replace view v8_sys_hs_arr_actroom_prt as
select ydet_ycat_id,wdat_date,sum(actroom) actroom,sum(actroomDayUse) actroomDayUse
from (
select 
   wdat_date
   ,ydet_ycat_id
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) actroom
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)) actroomDayUse
from wdat,ydet yd,yres y
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is not null
and (nvl(ydet_sharenum,0)= 0
     or
	 not exists (select d.ydet_sharenum 
	                       from ydet d,yres r 
				where d.ydet_yres_id=r.yres_id
							 and d.ydet_id<>yd.ydet_id 
							 and y.yres_exparrtime<>r.yres_exparrtime 
							 and d.ydet_sharenum=yd.ydet_sharenum
				)
	)
group by wdat_date,ydet_sharenum,ydet_ycat_id
) group by wdat_date,ydet_ycat_id;

CREATE OR REPLACE VIEW V8_SYS_HS_ARR_ACTROOM_CAD AS 
SELECT ycat_sum_roomtype_ycad_id YCAD_ID,wdat_date
      ,sum(actroom) actroom,sum(actroomDayUse) actroomDayUse
from (
SELECT 
   wdat_date
   ,ycat_sum_roomtype_ycad_id
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) actroom
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)) actroomDayUse
FROM WDAT,YDET yd,YRES y,YCAT
WHERE
    ydet_date=wdat_date 
AND yres_exparrtime=wdat_date
AND ydet_yres_id=yres_id
AND ydet_ycat_id=ycat_id
AND ycat_notinavl=0
AND yres_resstatus<>3
AND yres_actarrtime IS NOT NULL
AND (NVL(ydet_sharenum,0)= 0
     OR
  NOT EXISTS (SELECT d.ydet_sharenum 
                        FROM YDET d,YRES r 
         WHERE d.ydet_yres_id=r.yres_id
        AND d.ydet_id<>yd.ydet_id 
        AND y.yres_exparrtime<>r.yres_exparrtime 
        AND d.ydet_sharenum=yd.ydet_sharenum
         )
 )
GROUP BY wdat_date,ydet_sharenum,ycat_sum_roomtype_ycad_id
) GROUP BY wdat_date,ycat_sum_roomtype_ycad_id;


create or replace view v8_sys_hs_arr_actadult as
select 
   wdat_date
   ,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actadult
   ,sum(ydet_adultno*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) actadultDayUse
from wdat,ydet,yres,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is not null
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date;

create or replace view v8_sys_hs_arr_actadult_bed as
select 
   wdat_date
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actadult
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) actadultDayUse
from wdat,ydet,yres,ycat,yrpe
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
and yrpe_yres_id=yres_id
and yrpe_actarrtime is not null
and yrpe_xcac_id=0
group by wdat_date;

create or replace view v8_sys_hs_arr_actadult_prt as
select 
   wdat_date
   ,ydet_ycat_id
   ,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actadult
   ,sum(ydet_adultno*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) actadultDayUse
from wdat,ydet,yres
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is not null
and yres_bed=0
group by wdat_date,ydet_ycat_id;

create or replace view v8_sys_hs_arr_actadult_bed_prt as
select 
   wdat_date
   ,ydet_bed_ycat_id ydet_bed_ycat_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actadult
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) actadultDayUse
from wdat,ydet,yres,yrpe
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_yres_id=yres_id
and yrpe_actarrtime is not null
and yrpe_xcac_id=0
and yres_bed=1
group by wdat_date,ydet_bed_ycat_id;

create or replace view v8_sys_hs_arr_actadult_prt_rb as
select wdat_date day,ycat_id,sum(actAdult) actAdult,sum(actAdultDayUse) actAdultDayUse
from ( 
select wdat_date,ydet_ycat_id ycat_id,actAdult,actAdultDayUse from v8_sys_hs_arr_actadult_prt
union
select wdat_date,ydet_bed_ycat_id ycat_id,actadult,actAdultDayUse from v8_sys_hs_arr_actadult_bed_prt
) group by wdat_date,ycat_id;


CREATE OR REPLACE VIEW V8_SYS_HS_ARR_ACTADULT_CAD
AS 
SELECT 
   wdat_date
   ,ycat_sum_roomtype_ycad_id YCAD_ID
   ,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actadult
   ,sum(ydet_adultno*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) actadultDayUse
FROM WDAT,YDET,YRES,YCAT
WHERE
    ydet_date=wdat_date 
AND yres_exparrtime=wdat_date
AND ydet_yres_id=yres_id
AND ydet_ycat_id=ycat_id
AND ycat_notinavl=0
AND yres_resstatus<>3
AND yres_actarrtime IS NOT NULL
GROUP BY wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_arr_actadult_bed_cad
as 
select 
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actadult
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) actadultdayuse
from wdat,ydet,yres,ycat,yrpe
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and ydet_bed_ycat_id=ycat_id
and ycat_notinavl=0
and yres_resstatus<>3
and yrpe_yres_id=yres_id
and yrpe_actarrtime is not null
and yrpe_xcac_id=0
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_arr_actchild as
select 
   wdat_date
   ,sum(yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actchild
   ,sum(yrgl_number*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) actchildDayUse
   ,sum(decode(xcac_domestic,1,yrgl_number,0)*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actdomesticchild
   ,sum(decode(xcac_domestic,1,yrgl_number,0)*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) actdomesticchildDayUse
   ,sum(decode(xcac_freeofcharge,1,yrgl_number,0)*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actfreeofchargechild
   ,sum(decode(xcac_freeofcharge,1,yrgl_number,0)*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) actfreeofchargechildDayUse
from wdat,ydet,yres,yrgl,xcac,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is not null
and yrgl_ydet_id=ydet_id
and yrgl_xcac_id=xcac_id
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date;

create or replace view v8_sys_hs_arr_actchild_bed as
select 
   wdat_date
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actchild
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) actchildDayUse
   ,sum(decode(xcac_domestic,1,1,0)*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actdomesticchild
   ,sum(decode(xcac_domestic,1,1,0)*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) actdomesticchildDayUse
   ,sum(decode(xcac_freeofcharge,1,1,0)*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actfreeofchargechild
   ,sum(decode(xcac_freeofcharge,1,1,0)*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) actfreeofchargechildDayUse
from wdat,ydet,yres,xcac,ycat,yrpe
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_yres_id=yres_id
and yrpe_actarrtime is not null
and yrpe_xcac_id=xcac_id
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
group by wdat_date;

create or replace view v8_sys_hs_arr_actchild_prt as
select 
   wdat_date
   ,ydet_ycat_id
   ,sum(yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actchild
   ,sum(decode(xcac_domestic,1,yrgl_number,0)*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actdomesticchild
   ,sum(yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) actchildDayUse
   ,sum(decode(xcac_domestic,1,yrgl_number,0)*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) actdomesticchildDayUse
   ,sum(decode(xcac_freeofcharge,1,yrgl_number,0)*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actfreeofchargechild
   ,sum(decode(xcac_freeofcharge,1,yrgl_number,0)*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) actfreeofchargechildDayUse
from wdat,ydet,yres,yrgl,xcac
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is not null
and yrgl_ydet_id=ydet_id
and yrgl_xcac_id=xcac_id
and yres_bed=0
group by wdat_date,ydet_ycat_id;

create or replace view v8_sys_hs_arr_actchild_bed_prt as
select 
   wdat_date
   ,ydet_bed_ycat_id ydet_ycat_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actchild
   ,sum(decode(xcac_domestic,1,1,0)*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actdomesticchild
   ,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) actchildDayUse
   ,sum(decode(xcac_domestic,1,1,0)*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) actdomesticchildDayUse
   ,sum(decode(xcac_freeofcharge,1,1,0)*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actfreeofchargechild
   ,sum(decode(xcac_freeofcharge,1,1,0)*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) actfreeofchargechildDayUse
from wdat,ydet,yres,xcac,yrpe
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_yres_id=yres_id
and yrpe_actarrtime is not null
and yrpe_xcac_id=xcac_id
and yres_bed=1
group by wdat_date,ydet_bed_ycat_id;

create or replace view v8_sys_hs_arr_actchild_prt_rb as
select day,ycat_id ycat_id
  ,sum(actchild) actchild
  ,sum(actdomesticchild) actdomesticchild
  ,sum(actchildDayUse) actchildDayUse
  ,sum(actdomesticchildDayUse) actdomesticchildDayUse
  ,sum(actfreeofchargechild) actfreeofchargechild
  ,sum(actfreeofchargechildDayUse) actfreeofchargechildDayUse
from (
select wdat_date day,ydet_ycat_id ycat_id,actchild,actdomesticchild,actchildDayUse,actdomesticchildDayUse,actfreeofchargechild,actfreeofchargechildDayUse
from v8_sys_hs_arr_actchild_prt
union
select wdat_date day,ydet_ycat_id ycat_id,actchild,actdomesticchild,actchildDayUse,actdomesticchildDayUse,actfreeofchargechild,actfreeofchargechildDayUse
from v8_sys_hs_arr_actchild_bed_prt
) group by day,ycat_id;

CREATE OR REPLACE VIEW V8_SYS_HS_ARR_ACTCHILD_CAD
AS 
SELECT 
   wdat_date
   ,ycat_sum_roomtype_ycad_id YCAD_ID
   ,sum(yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actchild
   ,sum(decode(xcac_domestic,1,yrgl_number,0)*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actdomesticchild
   ,sum(yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) actchildDayUse
   ,sum(decode(xcac_domestic,1,yrgl_number,0)*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) actdomesticchildDayUse
   ,sum(decode(xcac_freeofcharge,1,yrgl_number,0)*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actfreeofchargechild
   ,sum(decode(xcac_freeofcharge,1,yrgl_number,0)*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) actfreeofchargechildDayUse
FROM WDAT,YDET,YRES,YRGL,XCAC,YCAT
WHERE
    ydet_date=wdat_date 
AND yres_exparrtime=wdat_date
AND ydet_yres_id=yres_id
AND ydet_ycat_id=ycat_id
AND ycat_notinavl=0
AND yres_resstatus<>3
AND yres_actarrtime IS NOT NULL
AND yrgl_ydet_id=ydet_id
AND yrgl_xcac_id=xcac_id
GROUP BY wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_arr_actchild_bed_cad
as 
select 
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actchild
   ,sum(decode(xcac_domestic,1,1,0)*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actdomesticchild
   ,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) actchilddayuse
   ,sum(decode(xcac_domestic,1,1,0)*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) actdomesticchilddayuse
   ,sum(decode(xcac_freeofcharge,1,1,0)*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actfreeofchargechild
   ,sum(decode(xcac_freeofcharge,1,1,0)*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) actfreeofchargechilddayuse
from wdat,ydet,yres,xcac,ycat,yrpe
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and ydet_bed_ycat_id=ycat_id
and ycat_notinavl=0
and yres_resstatus<>3
and yrpe_yres_id=yres_id
and yrpe_actarrtime is not null
and yrpe_xcac_id=xcac_id
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_arr_preroom as
select wdat_date,sum(preroom) preroom,sum(preroomDayUse) preroomDayUse  from (
select 
   wdat_date
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) preroom
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)) preroomDayUse
from wdat,ydet yd,yres y,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is null
and nvl(ydet_yrms_id,0)<>0
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and (nvl(ydet_sharenum,0)= 0
     or
	 not exists (select d.ydet_sharenum 
	                       from ydet d,yres r 
				where d.ydet_yres_id=r.yres_id
							 and d.ydet_id<>yd.ydet_id 
							 and (y.yres_exparrtime<>r.yres_exparrtime or r.yres_actarrtime is not null)
							 and d.ydet_sharenum=yd.ydet_sharenum
				)
	)
group by wdat_date,ydet_sharenum
)group by wdat_date;

CREATE OR REPLACE VIEW V8_SYS_HS_ARR_PREROOM_CAD
AS 
select wdat_date,ycad_id,sum(preroom) preroom,sum(preroomDayUse) preroomDayUse  from (
select 
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) preroom
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)) preroomDayUse
from wdat,ydet yd,yres y,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is null
and nvl(ydet_yrms_id,0)<>0
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and (nvl(ydet_sharenum,0)= 0
     or
	 not exists (select d.ydet_sharenum 
	                       from ydet d,yres r 
				where d.ydet_yres_id=r.yres_id
							 and d.ydet_id<>yd.ydet_id 
							 and (y.yres_exparrtime<>r.yres_exparrtime or r.yres_actarrtime is not null)
							 and d.ydet_sharenum=yd.ydet_sharenum
				)
	)
group by wdat_date,ycat_sum_roomtype_ycad_id,ydet_sharenum
)group by wdat_date,ycad_id;

create or replace view v8_sys_hs_arr_preadult as
select 
   wdat_date
   ,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) preadult
   ,sum(ydet_adultno*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) preadultDayUse
from wdat,ydet,yres,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is null
and ydet_yrms_id<>0
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date;

create or replace view v8_sys_hs_arr_preadult_bed as
select 
   wdat_date
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) preadult
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) preadultDayUse
from wdat,ydet,yres,ycat,yrpe,ydrb
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_yres_id=yres_id
and yrpe_xcac_id=0
and yrpe_actarrtime is null
and yrpe_noshow=0
and ydrb_ydet_id=ydet_id
and ydrb_xcac_id=yrpe_xcac_id
and ydrb_xcacindex=yrpe_xcacindex
and ydrb_yrms_id>0
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
group by wdat_date;

CREATE OR REPLACE VIEW V8_SYS_HS_ARR_PREADULT_CAD
AS 
select 
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) preadult
   ,sum(ydet_adultno*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) preadultDayUse
from wdat,ydet,yres,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is null
and trunc(ydet_yrms_id,0)<>0
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_arr_preadult_bed_cad
as 
select 
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) preadult
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) preadultdayuse
from wdat,ydet,yres,ycat,yrpe,ydrb
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_yres_id=yres_id
and yrpe_xcac_id=0
and yrpe_actarrtime is null
and yrpe_noshow=0
and ydrb_ydet_id=ydet_id
and ydrb_xcac_id=yrpe_xcac_id
and ydrb_xcacindex=yrpe_xcacindex
and ydrb_yrms_id>0
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_arr_prechild as
select 
   wdat_date
   ,sum(yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) prechild
   ,sum(yrgl_number*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) prechildDayUse
from wdat,ydet,yres,yrgl,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is null
and trunc(ydet_yrms_id,0)<>0
and ydet_id=yrgl_ydet_id
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date;

create or replace view v8_sys_hs_arr_prechild_bed as
select 
   wdat_date
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) prechild
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) prechildDayUse
from wdat,ydet,yres,ycat,yrpe,ydrb
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_yres_id=yres_id
and yrpe_xcac_id>0
and yrpe_actarrtime is null
and yrpe_noshow=0
and ydrb_ydet_id=ydet_id
and ydrb_xcac_id=yrpe_xcac_id
and ydrb_xcacindex=yrpe_xcacindex
and ydrb_yrms_id>0
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
group by wdat_date;

CREATE OR REPLACE VIEW V8_SYS_HS_ARR_PRECHILD_CAD
AS 
select 
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) prechild
   ,sum(yrgl_number*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) prechildDayUse
from wdat,ydet,yres,yrgl,ycat
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is null
and trunc(ydet_yrms_id,0)<>0
and ydet_id=yrgl_ydet_id
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_arr_prechild_bed_cad
as 
select 
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) prechild
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) prechilddayuse
from wdat,ydet,yres,ycat,yrpe,ydrb
where
    ydet_date=wdat_date 
and yres_exparrtime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_yres_id=yres_id
and yrpe_xcac_id>0
and yrpe_actarrtime is null
and yrpe_noshow=0
and ydrb_ydet_id=ydet_id
and ydrb_xcac_id=yrpe_xcac_id
and ydrb_xcacindex=yrpe_xcacindex
and ydrb_yrms_id>0
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_dep_exproom as
select wdat_date,sum(exproom) exproom,sum(exproomDayUse) exproomDayUse from (
select 
   wdat_date
   ,ydet_sharenum
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) exproom
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)) exproomDayUse
from wdat,ydet yd,yres y,ycat
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and yres_resstatus<>3
and yres_actdeptime is null
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and (nvl(ydet_sharenum,0)= 0
     or
	 not exists (select d.ydet_sharenum 
	                       from ydet d,yres r 
				where d.ydet_yres_id=r.yres_id
							 and d.ydet_id<>yd.ydet_id 
							 and y.yres_expdeptime<>r.yres_expdeptime 
							 and d.ydet_sharenum=yd.ydet_sharenum
				)
	)
group by wdat_date,ydet_sharenum)
group by wdat_date;

create or replace view v8_sys_hs_dep_exproom_prt as
select ydet_ycat_id,wdat_date,sum(exproom) exproom,sum(exproomDayUse) exproomDayUse from (
select 
   wdat_date
   ,ydet_ycat_id 
   ,ydet_sharenum
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) exproom
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)) exproomDayUse
from wdat,ydet yd,yres y
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and yres_resstatus<>3
and yres_actdeptime is null
and (nvl(ydet_sharenum,0)= 0
     or
	 not exists (select d.ydet_sharenum 
	                       from ydet d,yres r 
				where d.ydet_yres_id=r.yres_id
							 and d.ydet_id<>yd.ydet_id 
							 and y.yres_expdeptime<>r.yres_expdeptime 
							 and d.ydet_sharenum=yd.ydet_sharenum
				)
	)
group by wdat_date,ydet_sharenum,ydet_ycat_id)
group by wdat_date,ydet_ycat_id;

CREATE OR REPLACE VIEW V8_SYS_HS_DEP_EXPROOM_CAD
AS 
SELECT ycat_sum_roomtype_ycad_id YCAD_ID,wdat_date,sum(exproom) exproom,sum(exproomDayUse) exproomDayUse from (
SELECT 
   wdat_date
   ,ycat_sum_roomtype_ycad_id
   ,ydet_sharenum
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) exproom
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)) exproomDayUse
FROM WDAT,YDET yd,YRES y,YCAT
WHERE
ydet_id=yres_last_ydet_id
AND ydet_ycat_id=ycat_id
AND ycat_notinavl=0
AND yres_expdeptime=wdat_date  
AND yres_resstatus<>3
AND yres_actdeptime IS NULL
AND (NVL(ydet_sharenum,0)= 0
     OR
  NOT EXISTS (SELECT d.ydet_sharenum 
                        FROM YDET d,YRES r 
         WHERE d.ydet_yres_id=r.yres_id
        AND d.ydet_id<>yd.ydet_id 
        AND y.yres_expdeptime<>r.yres_expdeptime 
        AND d.ydet_sharenum=yd.ydet_sharenum
         )
 )
GROUP BY wdat_date,ydet_sharenum,ycat_sum_roomtype_ycad_id)
GROUP BY wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_dep_exproom_xcma as
select wdat_date, wdat_date day,ydet_xcma_id xcma_id,sum(exproom) exproom,sum(exproomDayUse) exproomDayUse from (
select 
   wdat_date
   ,ydet_xcma_id
   ,ydet_sharenum
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) exproom
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)) exproomDayUse
from wdat,ydet yd,yres y,ycat
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and yres_resstatus<>3
and yres_actdeptime is null
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and (nvl(ydet_sharenum,0)= 0
     or
	 not exists (select d.ydet_sharenum 
	                       from ydet d,yres r 
				where d.ydet_yres_id=r.yres_id
							 and d.ydet_id<>yd.ydet_id 
							 and y.yres_expdeptime<>r.yres_expdeptime 
							 and d.ydet_sharenum=yd.ydet_sharenum
							 and d.ydet_xcma_id=yd.ydet_xcma_id
				)
	)
group by wdat_date,ydet_sharenum,ydet_xcma_id)
group by wdat_date,ydet_xcma_id;

create or replace view v8_sys_hs_dep_allroom as
select wdat_date
   ,sum(alldeproom) alldeproom,sum(defdeproom) defdeproom ,sum(tentdeproom) tentdeproom  
   ,sum(alldeproomDayUse) alldeproomDayUse,sum(defdeproomDayUse) defdeproomDayUse,sum(tentdeproomDayUse) tentdeproomDayUse  
from (
select 
   wdat_date
   ,ydet_sharenum
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) alldeproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) defdeproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) tentdeproom
   ,
    decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) alldeproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) defdeproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) tentdeproomDayUse
from wdat,ydet yd,yres y,ycat
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and yres_resstatus<>3
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and (nvl(ydet_sharenum,0)= 0
     or
	 not exists (select d.ydet_sharenum 
	                       from ydet d,yres r 
				where d.ydet_yres_id=r.yres_id
							 and d.ydet_id<>yd.ydet_id 
							 and y.yres_expdeptime<>r.yres_expdeptime 
							 and d.ydet_sharenum=yd.ydet_sharenum
				)
	)
group by wdat_date,ydet_sharenum)
group by wdat_date;

create or replace view v8_sys_hs_dep_allroom_xcma as
select wdat_date,xcma_id   
   ,sum(alldeproom) alldeproom,sum(defdeproom) defdeproom ,sum(tentdeproom) tentdeproom  
   ,sum(alldeproomDayUse) alldeproomDayUse,sum(defdeproomDayUse) defdeproomDayUse,sum(tentdeproomDayUse) tentdeproomDayUse  
from (
select 
   wdat_date
   ,ydet_xcma_id xcma_id
   ,ydet_sharenum
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) alldeproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) defdeproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) tentdeproom
   ,
    decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) alldeproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) defdeproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) tentdeproomDayUse
from wdat,ydet yd,yres y,ycat
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and yres_resstatus<>3
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and (nvl(ydet_sharenum,0)= 0
     or
	 not exists (select d.ydet_sharenum 
	                       from ydet d,yres r 
				where d.ydet_yres_id=r.yres_id
							 and d.ydet_id<>yd.ydet_id 
							 and y.yres_expdeptime<>r.yres_expdeptime 
							 and d.ydet_sharenum=yd.ydet_sharenum
				)
	)
group by wdat_date,ydet_sharenum,ydet_xcma_id)
group by wdat_date,xcma_id;

create or replace view v8_sys_hs_dep_allroom_source as
select wdat_date,source_id   
   ,sum(alldeproom) alldeproom,sum(defdeproom) defdeproom ,sum(tentdeproom) tentdeproom  
   ,sum(alldeproomDayUse) alldeproomDayUse,sum(defdeproomDayUse) defdeproomDayUse,sum(tentdeproomDayUse) tentdeproomDayUse  
from (
select 
   wdat_date
   ,yral_yras_id source_id
   ,ydet_sharenum
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) alldeproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) defdeproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) tentdeproom
   ,
    decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) alldeproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) defdeproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) tentdeproomDayUse
from wdat,ydet yd,yres y,ycat,yral
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and yres_resstatus<>3
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and yral_ydet_id=ydet_id
and yral_yrac_id=1
and (nvl(ydet_sharenum,0)= 0
     or
	 not exists (select d.ydet_sharenum 
	                       from ydet d,yres r 
				where d.ydet_yres_id=r.yres_id
							 and d.ydet_id<>yd.ydet_id 
							 and y.yres_expdeptime<>r.yres_expdeptime 
							 and d.ydet_sharenum=yd.ydet_sharenum
				)
	)
group by wdat_date,ydet_sharenum,yral_yras_id)
group by wdat_date,source_id;

create or replace view v8_sys_hs_dep_allroom_prt as
select wdat_date,ydet_ycat_id
   ,sum(alldeproom) alldeproom,sum(defdeproom) defdeproom ,sum(tentdeproom) tentdeproom  
   ,sum(alldeproomDayUse) alldeproomDayUse,sum(defdeproomDayUse) defdeproomDayUse,sum(tentdeproomDayUse) tentdeproomDayUse  
from (
select 
   wdat_date
   ,ydet_ycat_id
   ,ydet_sharenum
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) alldeproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) defdeproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) tentdeproom
   ,
    decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) alldeproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) defdeproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) tentdeproomDayUse
from wdat,ydet yd,yres y
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and yres_resstatus<>3
and ydet_ycat_id>0
and (nvl(ydet_sharenum,0)= 0
     or
	 not exists (select d.ydet_sharenum 
	                       from ydet d,yres r 
				where d.ydet_yres_id=r.yres_id
							 and d.ydet_id<>yd.ydet_id 
							 and y.yres_expdeptime<>r.yres_expdeptime 
							 and d.ydet_sharenum=yd.ydet_sharenum
				)
	)
group by wdat_date,ydet_sharenum,ydet_ycat_id)
group by wdat_date,ydet_ycat_id;

create or replace view v8_sys_hs_dep_allroom_cad as
select wdat_date,ycad_id
   ,sum(alldeproom) alldeproom,sum(defdeproom) defdeproom ,sum(tentdeproom) tentdeproom  
   ,sum(alldeproomDayUse) alldeproomDayUse,sum(defdeproomDayUse) defdeproomDayUse,sum(tentdeproomDayUse) tentdeproomDayUse  
from (
select 
   wdat_date
   ,YCAT_SUM_ROOMTYPE_YCAD_ID ycad_id
   ,ydet_sharenum
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) alldeproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) defdeproom
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) tentdeproom
   ,
    decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) alldeproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),1,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) defdeproomDayUse
   ,decode(nvl(ydet_sharenum,0),0,sum(decode(yres_resstatus,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))
                                 ,decode(max(yres_resstatus),0,avg(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) tentdeproomDayUse
from wdat,ydet yd,yres y,ycat
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and yres_resstatus<>3
and (nvl(ydet_sharenum,0)= 0
     or
	 not exists (select d.ydet_sharenum 
	                       from ydet d,yres r 
				where d.ydet_yres_id=r.yres_id
							 and d.ydet_id<>yd.ydet_id 
							 and y.yres_expdeptime<>r.yres_expdeptime 
							 and d.ydet_sharenum=yd.ydet_sharenum
				)
	)
and ycat_id=ydet_ycat_id
group by wdat_date,ydet_sharenum,YCAT_SUM_ROOMTYPE_YCAD_ID)
group by wdat_date,ycad_id;

create or replace view v8_sys_hs_dep_expadult as
select 
   wdat_date
   ,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expadult
   ,sum(ydet_adultno*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expadultDayUse 
from wdat,ydet,yres,ycat
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actdeptime is null
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date;

create or replace view v8_sys_hs_dep_expadult_bed as
select 
   wdat_date
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expadult
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expadultDayUse 
from wdat,ydet,yres,ycat,yrpe
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_actdeptime is null
and yrpe_yres_id=yres_id
and yrpe_noshow=0
and yrpe_xcac_id=0
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
group by wdat_date;

create or replace view v8_sys_hs_dep_grp_expadult as
select 
   wdat_date
   ,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expadult
   ,sum(ydet_adultno*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expadultDayUse 
from wdat,ydet,yres,ycat, ygrp
where
ydet_id=yres_last_ydet_id
and YRES_YGRP_ID = ygrp_id 
and yres_expdeptime=wdat_date     
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actdeptime is null
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date;

create or replace view v8_sys_hs_dep_expchild as
select 
   wdat_date
   ,sum(yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expchild
   ,sum(yrgl_number*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expchildDayUse 
from wdat,ydet,yres,yrgl,ycat
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actdeptime is null
and ydet_id=yrgl_ydet_id
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date;

create or replace view v8_sys_hs_dep_expchild_bed as
select 
   wdat_date
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expchild
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expchildDayUse 
from wdat,ydet,yres,ycat,yrpe
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_actdeptime is null
and yrpe_yres_id=yres_id
and yrpe_xcac_Id>0
and yrpe_noshow=0
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
group by wdat_date;

create or replace view v8_sys_hs_dep_expchild_prt as
select 
   wdat_date
   ,ycat_id
   ,sum(yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expchild
   ,sum(yrgl_number*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expchildDayUse 
from wdat,ydet,yres,yrgl,ycat
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actdeptime is null
and ydet_id=yrgl_ydet_id
and ydet_ycat_id=ycat_id
and yres_bed=0
group by wdat_date,ycat_id;

create or replace view v8_sys_hs_dep_expchild_bed_prt as
select 
   wdat_date
   ,ycat_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expchild
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expchildDayUse 
from wdat,ydet,yres,ycat,yrpe
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_actdeptime is null
and yrpe_yres_id=yres_id
and yrpe_xcac_id>0
and yrpe_noshow=0
and ydet_bed_ycat_id=ycat_id
and yres_bed=1
group by wdat_date,ycat_id;

create or replace view v8_sys_hs_dep_expchild_prt_rb as
select day,ycat_id,sum(expchild) expchild,sum(expchildDayUse) expchildDayUse
from (
select wdat_date day,ycat_id,expchild,expchildDayUse from v8_sys_hs_dep_expchild_prt
union 
select wdat_date day,ycat_id,expchild,expchildDayUse from v8_sys_hs_dep_expchild_bed_prt
) group by day,ycat_id;

create or replace view v8_sys_hs_dep_expchild_cad as
select 
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expchild
   ,sum(yrgl_number*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expchildDayUse 
from wdat,ydet,yres,yrgl,ycat
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actdeptime is null
and ydet_id=yrgl_ydet_id
and ydet_ycat_id=ycat_id
AND ycat_notinavl=0
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_dep_expchild_bed_cad as
select 
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expchild
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expchildDayUse 
from wdat,ydet,yres,ycat,yrpe
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_actdeptime is null
and yrpe_yres_id=yres_id
and yrpe_xcac_id>0
and yrpe_noshow=0
and ydet_bed_ycat_id=ycat_id
AND ycat_notinavl=0
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_dep_expadult_prt as
select 
   wdat_date
   ,ydet_ycat_id
   ,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expadult
   ,sum(ydet_adultno*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expadultDayUse 
from wdat,ydet,yres
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actdeptime is null
and yres_bed=0
group by wdat_date,ydet_ycat_id;

create or replace view v8_sys_hs_dep_expadult_bed_prt as
select 
   wdat_date
   ,ydet_bed_ycat_id ydet_ycat_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expadult
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expadultDayUse 
from wdat,ydet,yres,yrpe
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_actdeptime is null
and yrpe_yres_id=yres_id
and yrpe_xcac_id=0
and yrpe_noshow=0
and yres_bed=1
group by wdat_date,ydet_bed_ycat_id;

create or replace view v8_sys_hs_dep_expadult_prt_rb as
select day,ycat_id,sum(expadult) expadult,sum(expadultDayUse) expadultDayUse
from (
select wdat_date day,ydet_ycat_id ycat_id,expadult,expadultDayUse from v8_sys_hs_dep_expadult_prt
union  
select wdat_date day,ydet_ycat_id ycat_id,expadult,expadultDayUse from v8_sys_hs_dep_expadult_bed_prt
) group by day,ycat_id;

CREATE OR REPLACE VIEW V8_SYS_HS_DEP_EXPADULT_CAD
AS 
SELECT 
   wdat_date
   ,ycat_sum_roomtype_ycad_id YCAD_ID
   ,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expadult
   ,sum(ydet_adultno*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expadultDayUse 
FROM WDAT,YDET,YRES,YCAT
WHERE
ydet_id=yres_last_ydet_id
AND yres_expdeptime=wdat_date  
AND ydet_yres_id=yres_id
AND ydet_ycat_id=ycat_id
AND ycat_notinavl=0
AND yres_resstatus<>3
AND yres_actdeptime IS NULL
GROUP BY wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_dep_expadult_bed_cad
as 
select 
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) expadult
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) expadultdayuse 
from wdat,ydet,yres,ycat,yrpe
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date  
and ydet_yres_id=yres_id
and ydet_bed_ycat_id=ycat_id
and ycat_notinavl=0
and yres_resstatus<>3
and yrpe_actdeptime is null
and yrpe_yres_id=yres_id
and yrpe_xcac_id=0
and yrpe_noshow=0
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_dep_alladult as
select wdat_date
   ,sum(alldepadult) alldepadult,sum(defdepadult) defdepadult,sum(tentdepadult) tentdepadult  
   ,sum(alldepadultDayUse) alldepadultDayUse,sum(defdepadultDayUse) defdepadultDayUse,sum(tentdepadultDayUse) tentdepadultDayUse  
from (
select 
   wdat_date
   ,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) alldepadult
   ,decode(yres_resstatus,1,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) defdepadult
   ,decode(yres_resstatus,0,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) tentdepadult
   ,
    sum(ydet_adultno*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) alldepadultDayUse
   ,decode(yres_resstatus,1,sum(ydet_adultno*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) defdepadultDayUse
   ,decode(yres_resstatus,0,sum(ydet_adultno*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) tentdepadultDayUse
from wdat,ydet,yres,ycat
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and yres_resstatus<>3
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date,yres_resstatus
) group by wdat_date;

create or replace view v8_sys_hs_dep_alladult_bed as
select wdat_date
   ,sum(alldepadult) alldepadult,sum(defdepadult) defdepadult,sum(tentdepadult) tentdepadult  
   ,sum(alldepadultDayUse) alldepadultDayUse,sum(defdepadultDayUse) defdepadultDayUse,sum(tentdepadultDayUse) tentdepadultDayUse  
from (
select 
   wdat_date
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) alldepadult
   ,decode(yres_resstatus,1,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) defdepadult
   ,decode(yres_resstatus,0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) tentdepadult
   ,
    sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) alldepadultDayUse
   ,decode(yres_resstatus,1,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) defdepadultDayUse
   ,decode(yres_resstatus,0,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) tentdepadultDayUse
from wdat,ydet,yres,ycat,yrpe
where
ydet_id=yres_last_ydet_id
and yres_resstatus<>3
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
and yrpe_yres_id=yres_id
and yrpe_xcac_id=0
and yrpe_noshow=0
and nvl(trunc(yrpe_actdeptime),yres_expdeptime)=wdat_date
group by wdat_date,yres_resstatus
) group by wdat_date;

create or replace view v8_sys_hs_dep_alladult_prt as
select wdat_date,ydet_ycat_id
   ,sum(alldepadult) alldepadult,sum(defdepadult) defdepadult,sum(tentdepadult) tentdepadult  
   ,sum(alldepadultDayUse) alldepadultDayUse,sum(defdepadultDayUse) defdepadultDayUse,sum(tentdepadultDayUse) tentdepadultDayUse  
from (
select 
   wdat_date
   ,ydet_ycat_id
   ,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) alldepadult
   ,decode(yres_resstatus,1,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) defdepadult
   ,decode(yres_resstatus,0,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) tentdepadult
   ,
    sum(ydet_adultno*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) alldepadultDayUse
   ,decode(yres_resstatus,1,sum(ydet_adultno*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) defdepadultDayUse
   ,decode(yres_resstatus,0,sum(ydet_adultno*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) tentdepadultDayUse
from wdat,ydet,yres
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and yres_resstatus<>3
and yres_bed=0
group by wdat_date,yres_resstatus,ydet_ycat_id
) group by wdat_date,ydet_ycat_id;

create or replace view v8_sys_hs_dep_alladult_bed_prt as
select wdat_date,ydet_ycat_id
   ,sum(alldepadult) alldepadult,sum(defdepadult) defdepadult,sum(tentdepadult) tentdepadult  
   ,sum(alldepadultDayUse) alldepadultDayUse,sum(defdepadultDayUse) defdepadultDayUse,sum(tentdepadultDayUse) tentdepadultDayUse  
from (
select 
   wdat_date
   ,ydet_bed_ycat_id ydet_ycat_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) alldepadult
   ,decode(yres_resstatus,1,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) defdepadult
   ,decode(yres_resstatus,0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) tentdepadult
   ,
    sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) alldepadultDayUse
   ,decode(yres_resstatus,1,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) defdepadultDayUse
   ,decode(yres_resstatus,0,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) tentdepadultDayUse
from wdat,ydet,yres,yrpe
where
ydet_id=yres_last_ydet_id
and yres_resstatus<>3
and yrpe_yres_id=yres_id
and yrpe_xcac_id=0
and yrpe_noshow=0 
and nvl(trunc(yrpe_actdeptime),yres_expdeptime)=wdat_date
and yres_bed=1
group by wdat_date,yres_resstatus,ydet_bed_ycat_id
) group by wdat_date,ydet_ycat_id;

create or replace view v8_sys_hs_dep_alladult_cad as
select wdat_date,ycad_id
   ,sum(alldepadult) alldepadult,sum(defdepadult) defdepadult,sum(tentdepadult) tentdepadult  
   ,sum(alldepadultDayUse) alldepadultDayUse,sum(defdepadultDayUse) defdepadultDayUse,sum(tentdepadultDayUse) tentdepadultDayUse  
from (
select 
   wdat_date
   ,YCAT_SUM_ROOMTYPE_YCAD_ID ycad_id
   ,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) alldepadult
   ,decode(yres_resstatus,1,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) defdepadult
   ,decode(yres_resstatus,0,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) tentdepadult
   ,
    sum(ydet_adultno*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) alldepadultDayUse
   ,decode(yres_resstatus,1,sum(ydet_adultno*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) defdepadultDayUse
   ,decode(yres_resstatus,0,sum(ydet_adultno*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) tentdepadultDayUse
from wdat,ydet,yres,ycat
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and yres_resstatus<>3
and ycat_id=ydet_ycat_id
group by wdat_date,yres_resstatus,YCAT_SUM_ROOMTYPE_YCAD_ID
) group by wdat_date,ycad_id;

create or replace view v8_sys_hs_dep_alladult_bed_cad as
select wdat_date,ycad_id
   ,sum(alldepadult) alldepadult,sum(defdepadult) defdepadult,sum(tentdepadult) tentdepadult  
   ,sum(alldepadultDayUse) alldepadultDayUse,sum(defdepadultDayUse) defdepadultDayUse,sum(tentdepadultDayUse) tentdepadultDayUse  
from (
select 
   wdat_date
   ,YCAT_SUM_ROOMTYPE_YCAD_ID ycad_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) alldepadult
   ,decode(yres_resstatus,1,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) defdepadult
   ,decode(yres_resstatus,0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) tentdepadult
   ,
    sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) alldepadultDayUse
   ,decode(yres_resstatus,1,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) defdepadultDayUse
   ,decode(yres_resstatus,0,sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) tentdepadultDayUse
from wdat,ydet,yres,ycat,yrpe
where
ydet_id=yres_last_ydet_id
and yres_resstatus<>3
and ycat_id=ydet_bed_ycat_id
and yrpe_yres_id=yres_id
and yrpe_xcac_id=0
and yrpe_noshow=0
and nvl(trunc(yrpe_actdeptime),yres_expdeptime)=wdat_date
group by wdat_date,yres_resstatus,YCAT_SUM_ROOMTYPE_YCAD_ID
) group by wdat_date,ycad_id;

create or replace view v8_sys_hs_dep_allchild as
select wdat_date
   ,sum(alldepchild) alldepchild,sum(defdepchild) defdepchild,sum(tentdepchild) tentdepchild 
   ,sum(alldepdomesticchild) alldepdomesticchild,sum(defdepdomesticchild) defdepdomesticchild,sum(tentdepdomesticchild) tentdepdomesticchild
   ,sum(alldepchildDayUse) alldepchildDayUse,sum(defdepchildDayUse) defdepchildDayUse,sum(tentdepchildDayUse) tentdepchildDayUse 
   ,sum(alldepdomesticchildDayUse) alldepdomesticchildDayUse,sum(defdepdomesticchildDayUse) defdepdomesticchildDayUse,sum(tentdepdomesticchildDayUse) tentdepdomesticchildDayUse
   ,sum(alldepfreeofchargechild) alldepfreeofchargechild,sum(defdepfreeofchargechild) defdepfreeofchargechild,sum(tentdepfreeofchargechild) tentdepfreeofchargechild
   ,sum(alldepfreeofchargechildDayUse) alldepfreeofchargechildDayUse,sum(defdepfreeofchargechildDayUse) defdepfreeofchargechildDayUse,sum(tentdepfreeofchargechildDayUse) tentdepfreeofchargechildDayUse
from (
select 
   wdat_date
   ,sum(yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) alldepchild
   ,sum(decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) alldepdomesticchild
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,0,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end))) defdepchild
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,0,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end))) tentdepchild
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) defdepdomesticchild
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) tentdepdomesticchild
   ,
    sum(yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) alldepchildDayUse
   ,sum(decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0)) alldepdomesticchildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,0,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end))) defdepchildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,0,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end))) tentdepchildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) defdepdomesticchildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) tentdepdomesticchildDayUse
   ,sum(decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) alldepfreeofchargechild
   ,sum(decode(yres_resstatus,1,decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) defdepfreeofchargechild
   ,sum(decode(yres_resstatus,0,decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) tentdepfreeofchargechild
   ,sum(decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0)) alldepfreeofchargechildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) defdepfreeofchargechildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) tentdepfreeofchargechildDayUse
from wdat,ydet,yres,yrgl,xcac,ycat
where
    ydet_id=yres_last_ydet_id 
and yres_expdeptime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrgl_ydet_id=ydet_id
and yrgl_xcac_id=xcac_id
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date,yres_resstatus
) group by wdat_date;

create or replace view v8_sys_hs_dep_allchild_bed as
select wdat_date
   ,sum(alldepchild) alldepchild,sum(defdepchild) defdepchild,sum(tentdepchild) tentdepchild 
   ,sum(alldepdomesticchild) alldepdomesticchild,sum(defdepdomesticchild) defdepdomesticchild,sum(tentdepdomesticchild) tentdepdomesticchild
   ,sum(alldepchildDayUse) alldepchildDayUse,sum(defdepchildDayUse) defdepchildDayUse,sum(tentdepchildDayUse) tentdepchildDayUse 
   ,sum(alldepdomesticchildDayUse) alldepdomesticchildDayUse,sum(defdepdomesticchildDayUse) defdepdomesticchildDayUse,sum(tentdepdomesticchildDayUse) tentdepdomesticchildDayUse
   ,sum(alldepfreeofchargechild) alldepfreeofchargechild,sum(defdepfreeofchargechild) defdepfreeofchargechild,sum(tentdepfreeofchargechild) tentdepfreeofchargechild
   ,sum(alldepfreeofchargechildDayUse) alldepfreeofchargechildDayUse,sum(defdepfreeofchargechildDayUse) defdepfreeofchargechildDayUse,sum(tentdepfreeofchargechildDayUse) tentdepfreeofchargechildDayUse
from (
select 
   wdat_date
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) alldepchild
   ,sum(decode(xcac_domestic,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) alldepdomesticchild
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end))) defdepchild
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end))) tentdepchild
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) defdepdomesticchild
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) tentdepdomesticchild
   ,
    sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) alldepchildDayUse
   ,sum(decode(xcac_domestic,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0)) alldepdomesticchildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end))) defdepchildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end))) tentdepchildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) defdepdomesticchildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) tentdepdomesticchildDayUse
   ,sum(decode(xcac_freeofcharge,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) alldepfreeofchargechild
   ,sum(decode(yres_resstatus,1,decode(xcac_freeofcharge,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) defdepfreeofchargechild
   ,sum(decode(yres_resstatus,0,decode(xcac_freeofcharge,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) tentdepfreeofchargechild
   ,sum(decode(xcac_freeofcharge,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0)) alldepfreeofchargechildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_freeofcharge,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) defdepfreeofchargechildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_freeofcharge,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) tentdepfreeofchargechildDayUse
from wdat,ydet,yres,xcac,ycat,yrpe
where
    ydet_id=yres_last_ydet_id 
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_yres_id=yres_id
and nvl(trunc(yrpe_actdeptime),yres_expdeptime)=wdat_date
and yrpe_xcac_id=xcac_id
and yrpe_noshow=0
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
group by wdat_date,yres_resstatus
) group by wdat_date;

create or replace view v8_sys_hs_dep_allchild_prt as
select wdat_date,ydet_ycat_id
   ,sum(alldepchild) alldepchild,sum(defdepchild) defdepchild,sum(tentdepchild) tentdepchild 
   ,sum(alldepdomesticchild) alldepdomesticchild,sum(defdepdomesticchild) defdepdomesticchild,sum(tentdepdomesticchild) tentdepdomesticchild
   ,sum(alldepchildDayUse) alldepchildDayUse,sum(defdepchildDayUse) defdepchildDayUse,sum(tentdepchildDayUse) tentdepchildDayUse 
   ,sum(alldepdomesticchildDayUse) alldepdomesticchildDayUse,sum(defdepdomesticchildDayUse) defdepdomesticchildDayUse,sum(tentdepdomesticchildDayUse) tentdepdomesticchildDayUse
   ,sum(alldepfreeofchargechild) alldepfreeofchargechild,sum(defdepfreeofchargechild) defdepfreeofchargechild,sum(tentdepfreeofchargechild) tentdepfreeofchargechild
   ,sum(alldepfreeofchargechildDayUse) alldepfreeofchargechildDayUse,sum(defdepfreeofchargechildDayUse) defdepfreeofchargechildDayUse,sum(tentdepfreeofchargechildDayUse) tentdepfreeofchargechildDayUse
from (
select 
   wdat_date
   ,ydet_ycat_id
   ,sum(yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) alldepchild
   ,sum(decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) alldepdomesticchild
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,0,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end))) defdepchild
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,0,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end))) tentdepchild
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) defdepdomesticchild
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) tentdepdomesticchild
   ,
    sum(yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) alldepchildDayUse
   ,sum(decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0)) alldepdomesticchildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,0,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end))) defdepchildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,0,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end))) tentdepchildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) defdepdomesticchildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) tentdepdomesticchildDayUse
   ,sum(decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) alldepfreeofchargechild
   ,sum(decode(yres_resstatus,1,decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) defdepfreeofchargechild
   ,sum(decode(yres_resstatus,0,decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) tentdepfreeofchargechild
   ,sum(decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0)) alldepfreeofchargechildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) defdepfreeofchargechildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) tentdepfreeofchargechildDayUse
from wdat,ydet,yres,yrgl,xcac
where
    ydet_id=yres_last_ydet_id 
and yres_expdeptime=wdat_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrgl_ydet_id=ydet_id
and yrgl_xcac_id=xcac_id
and yres_bed=0
group by wdat_date,yres_resstatus,ydet_ycat_id
) group by wdat_date,ydet_ycat_id;

create or replace view v8_sys_hs_dep_allchild_bed_prt as
select wdat_date,ydet_ycat_id
   ,sum(alldepchild) alldepchild,sum(defdepchild) defdepchild,sum(tentdepchild) tentdepchild 
   ,sum(alldepdomesticchild) alldepdomesticchild,sum(defdepdomesticchild) defdepdomesticchild,sum(tentdepdomesticchild) tentdepdomesticchild
   ,sum(alldepchildDayUse) alldepchildDayUse,sum(defdepchildDayUse) defdepchildDayUse,sum(tentdepchildDayUse) tentdepchildDayUse 
   ,sum(alldepdomesticchildDayUse) alldepdomesticchildDayUse,sum(defdepdomesticchildDayUse) defdepdomesticchildDayUse,sum(tentdepdomesticchildDayUse) tentdepdomesticchildDayUse
   ,sum(alldepfreeofchargechild) alldepfreeofchargechild,sum(defdepfreeofchargechild) defdepfreeofchargechild,sum(tentdepfreeofchargechild) tentdepfreeofchargechild
   ,sum(alldepfreeofchargechildDayUse) alldepfreeofchargechildDayUse,sum(defdepfreeofchargechildDayUse) defdepfreeofchargechildDayUse,sum(tentdepfreeofchargechildDayUse) tentdepfreeofchargechildDayUse
from (
select 
   wdat_date
   ,ydet_bed_ycat_id ydet_ycat_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) alldepchild
   ,sum(decode(xcac_domestic,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) alldepdomesticchild
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end))) defdepchild
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end))) tentdepchild
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) defdepdomesticchild
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) tentdepdomesticchild
   ,
    sum(case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end) alldepchildDayUse
   ,sum(decode(xcac_domestic,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0)) alldepdomesticchildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end))) defdepchildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end))) tentdepchildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_domestic,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) defdepdomesticchildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_domestic,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) tentdepdomesticchildDayUse
   ,sum(decode(xcac_freeofcharge,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) alldepfreeofchargechild
   ,sum(decode(yres_resstatus,1,decode(xcac_freeofcharge,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) defdepfreeofchargechild
   ,sum(decode(yres_resstatus,0,decode(xcac_freeofcharge,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0))) tentdepfreeofchargechild
   ,sum(decode(xcac_freeofcharge,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0)) alldepfreeofchargechildDayUse
   ,sum(decode(yres_resstatus,1,decode(xcac_freeofcharge,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) defdepfreeofchargechildDayUse
   ,sum(decode(yres_resstatus,0,decode(xcac_freeofcharge,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0))) tentdepfreeofchargechildDayUse
from wdat,ydet,yres,xcac,yrpe
where
    ydet_id=yres_last_ydet_id 
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_yres_id=yres_id
and nvl(trunc(yrpe_actdeptime),yres_expdeptime)=wdat_date
and yrpe_xcac_id=xcac_id
and yrpe_noshow=0
and yres_bed=1
group by wdat_date,yres_resstatus,ydet_bed_ycat_id
) group by wdat_date,ydet_ycat_id;

create or replace view v8_sys_hs_dep_actroom as
select wdat_date,sum(actroom) actroom,sum(actroomDayUse) actroomDayUse from (
select 
   wdat_date
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) actroom
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)) actroomDayUse
from wdat,ydet yd,yres y,ycat
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and yres_resstatus<>3
and yres_actdeptime is not null
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and (nvl(ydet_sharenum,0)= 0
     or
	 not exists (select d.ydet_sharenum 
	                       from ydet d,yres r 
				where d.ydet_yres_id=r.yres_id
							 and d.ydet_id<>yd.ydet_id 
							 and (y.yres_expdeptime<>r.yres_expdeptime or r.yres_actdeptime is null) 
							 and d.ydet_sharenum=yd.ydet_sharenum
				)
	)
group by wdat_date,ydet_sharenum
) group by wdat_date;

create or replace view v8_sys_hs_dep_actroom_prt as
select wdat_date,ydet_ycat_id,sum(actroom) actroom,sum(actroomDayUse) actroomDayUse from (
select 
   wdat_date
   ,ydet_ycat_id
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) actroom
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)) actroomDayUse
from wdat,ydet yd,yres y
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and yres_resstatus<>3
and yres_actdeptime is not null
and (nvl(ydet_sharenum,0)= 0
     or
	 not exists (select d.ydet_sharenum 
	                       from ydet d,yres r 
				where d.ydet_yres_id=r.yres_id
							 and d.ydet_id<>yd.ydet_id 
							 and (y.yres_expdeptime<>r.yres_expdeptime or r.yres_actdeptime is null) 
							 and d.ydet_sharenum=yd.ydet_sharenum
				)
	)
group by wdat_date,ydet_sharenum,ydet_ycat_id
) group by wdat_date,ydet_ycat_id;

CREATE OR REPLACE VIEW V8_SYS_HS_DEP_ACTROOM_CAD
AS 
SELECT wdat_date,ycat_sum_roomtype_ycad_id YCAD_ID,sum(actroom) actroom,sum(actroomDayUse) actroomDayUse from (
SELECT 
   wdat_date
   ,ycat_sum_roomtype_ycad_id
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) actroom
   ,decode(nvl(ydet_sharenum,0),0,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)
                                 ,avg(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end)) actroomDayUse
FROM WDAT,YDET yd,YRES y,YCAT
WHERE
ydet_id=yres_last_ydet_id
AND ydet_ycat_id=ycat_id
AND ycat_notinavl=0
AND yres_expdeptime=wdat_date  
AND yres_resstatus<>3
AND yres_actdeptime IS NOT NULL
AND (NVL(ydet_sharenum,0)= 0
     OR
  NOT EXISTS (SELECT d.ydet_sharenum 
                        FROM YDET d,YRES r 
         WHERE d.ydet_yres_id=r.yres_id
        AND d.ydet_id<>yd.ydet_id 
        AND (y.yres_expdeptime<>r.yres_expdeptime OR r.yres_actdeptime IS NULL) 
        AND d.ydet_sharenum=yd.ydet_sharenum
         )
 )
GROUP BY wdat_date,ydet_sharenum,ycat_sum_roomtype_ycad_id
) GROUP BY wdat_date,ycat_sum_roomtype_ycad_id;


create or replace view v8_sys_hs_dep_actadult as
select 
   wdat_date
   ,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actadult
   ,sum(ydet_adultno*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) actadultDayUse
from wdat,ydet,yres,ycat
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and yres_resstatus<>3
and yres_actdeptime is not null
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date;

create or replace view v8_sys_hs_dep_actadult_bed as
select 
   wdat_date
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actadult
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) actadultDayUse
from wdat,ydet,yres,ycat,yrpe
where
ydet_id=yres_last_ydet_id
and yres_resstatus<>3
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
and yrpe_yres_id=yres_id
and yrpe_xcac_id=0
and trunc(yrpe_actdeptime)=wdat_date
group by wdat_date;

create or replace view v8_sys_hs_dep_actadult_prt as
select 
   wdat_date
   ,ydet_ycat_id
   ,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actadult
   ,sum(ydet_adultno*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) actadultDayUse
from wdat,ydet,yres
where
ydet_id=yres_last_ydet_id
and yres_expdeptime=wdat_date 	
and yres_resstatus<>3
and yres_actdeptime is not null
and yres_bed=0
group by wdat_date,ydet_ycat_id;

create or replace view v8_sys_hs_dep_actadult_bed_prt as
select 
   wdat_date
   ,ydet_bed_ycat_id ydet_ycat_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actadult
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) actadultDayUse
from wdat,ydet,yres,yrpe
where
ydet_id=yres_last_ydet_id
and yres_resstatus<>3
and yrpe_yres_id=yres_id
and yrpe_xcac_id=0
and trunc(yrpe_actdeptime)=wdat_date
and yres_bed=1
group by wdat_date,ydet_bed_ycat_id;

create or replace view v8_sys_hs_dep_actadult_prt_rb as
select day,ycat_id,sum(actAdult) actAdult,sum(actAdultDayUse) actAdultDayYse
from ( 
select wdat_date day,ydet_ycat_id ycat_id,actAdult,actAdultDayUse from v8_sys_hs_dep_actadult_prt
union   
select wdat_date day,ydet_ycat_id ycat_id,actAdult,actAdultDayUse from v8_sys_hs_dep_actadult_bed_prt
) group by day,ycat_id;

CREATE OR REPLACE VIEW V8_SYS_HS_DEP_ACTADULT_CAD
AS 
SELECT 
   wdat_date
   ,ycat_sum_roomtype_ycad_id YCAD_ID
   ,sum(ydet_adultno*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actadult
   ,sum(ydet_adultno*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) actadultDayUse
FROM WDAT,YDET,YRES,YCAT
WHERE
ydet_id=yres_last_ydet_id
AND ydet_ycat_id=ycat_id
AND ycat_notinavl=0
AND yres_expdeptime=wdat_date  
AND yres_resstatus<>3
AND yres_actdeptime IS NOT NULL
GROUP BY wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_dep_actadult_bed_cad
as 
select 
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actadult
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) actadultdayuse
from wdat,ydet,yres,ycat,yrpe
where
ydet_id=yres_last_ydet_id
and ydet_bed_ycat_id=ycat_id
and ycat_notinavl=0
and yres_resstatus<>3
and yrpe_yres_id=yres_id
and yrpe_xcac_id=0
and trunc(yrpe_actdeptime)=wdat_date
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_dep_actchild as
select 
   wdat_date
   ,sum(yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actchild
   ,sum(yrgl_number*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) actchildDayUse
   ,sum(decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) actdomesticchild
   ,sum(decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end,0)) actdomesticchildDayUse
   ,sum(decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) actfreeofchargechild
   ,sum(decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end,0)) actfreeofchargechildDayUse
from wdat,ydet,yres,yrgl,xcac,ycat
where
ydet_id=yres_last_ydet_id
and yrgl_ydet_id=ydet_id
and yres_expdeptime=wdat_date 	
and yres_resstatus<>3
and yres_actdeptime is not null
and yrgl_xcac_id=xcac_id
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date;

create or replace view v8_sys_hs_dep_actchild_bed as
select 
   wdat_date
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actchild
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) actchildDayUse
   ,sum(decode(xcac_domestic,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) actdomesticchild
   ,sum(decode(xcac_domestic,1,case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end,0)) actdomesticchildDayUse
   ,sum(decode(xcac_freeofcharge,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) actfreeofchargechild
   ,sum(decode(xcac_freeofcharge,1,case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end,0)) actfreeofchargechildDayUse
from wdat,ydet,yres,xcac,ycat,yrpe
where
ydet_id=yres_last_ydet_id
and yres_resstatus<>3
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
and yrpe_yres_id=yres_id
and trunc(yrpe_actdeptime)=wdat_date
and yrpe_xcac_id=xcac_id
group by wdat_date;

create or replace view v8_sys_hs_dep_actchild_prt as
select 
   wdat_date
   ,ydet_ycat_id
   ,sum(yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actchild
   ,sum(yrgl_number*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) actchildDayUse
   ,sum(decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) actdomesticchild
   ,sum(decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end,0)) actdomesticchildDayUse
   ,sum(decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) actfreeofchargechild
   ,sum(decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end,0)) actfreeofchargechildDayUse
from wdat,ydet,yres,yrgl,xcac
where
ydet_id=yres_last_ydet_id
and yrgl_ydet_id=ydet_id
and yres_expdeptime=wdat_date 	
and yres_resstatus<>3
and yres_actdeptime is not null
and yrgl_xcac_id=xcac_id
and yres_bed=0
group by wdat_date,ydet_ycat_id;

create or replace view v8_sys_hs_dep_actchild_bed_prt as
select 
   wdat_date
   ,ydet_bed_ycat_id ydet_ycat_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actchild
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) actchildDayUse
   ,sum(decode(xcac_domestic,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) actdomesticchild
   ,sum(decode(xcac_domestic,1,case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end,0)) actdomesticchildDayUse
   ,sum(decode(xcac_freeofcharge,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) actfreeofchargechild
   ,sum(decode(xcac_freeofcharge,1,case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end,0)) actfreeofchargechildDayUse
from wdat,ydet,yres,xcac,yrpe
where
ydet_id=yres_last_ydet_id
and yres_resstatus<>3
and yrpe_yres_id=yres_id
and trunc(yrpe_actdeptime)=wdat_date
and yrpe_xcac_id=xcac_id
and yres_bed=1
group by wdat_date,ydet_bed_ycat_id;

create or replace view v8_sys_hs_dep_actchild_prt_rb as
select day,ycat_id,sum(actchild) actchild,sum(actchildDayUse) actchildDayUse,sum(actdomesticchild) actdomesticchild,sum(actdomesticchildDayUse) actdomesticchildDayUse,sum(actfreeofchargechild) actfreeofchargechild,sum(actfreeofchargechildDayUse) actfreeofchargechildDayUse 
from (
select wdat_date day,ydet_ycat_id ycat_id,actchild,actchildDayUse,actdomesticchild,actdomesticchildDayUse,actfreeofchargechild,actfreeofchargechildDayUse from v8_sys_hs_dep_actchild_prt 
union
select wdat_date day,ydet_ycat_id ycat_id,actchild,actchildDayUse,actdomesticchild,actdomesticchildDayUse,actfreeofchargechild,actfreeofchargechildDayUse from v8_sys_hs_dep_actchild_bed_prt
) group by day,ycat_id; 
 
CREATE OR REPLACE VIEW V8_SYS_HS_DEP_ACTCHILD_CAD
AS 
SELECT 
   wdat_date
   ,ycat_sum_roomtype_ycad_id YCAD_ID
   ,sum(yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actchild
   ,sum(yrgl_number*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) actchildDayUse
   ,sum(decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) actdomesticchild
   ,sum(decode(xcac_domestic,1,yrgl_number*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end,0)) actdomesticchildDayUse
   ,sum(decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) actfreeofchargechild
   ,sum(decode(xcac_freeofcharge,1,yrgl_number*case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end,0)) actfreeofchargechildDayUse
FROM WDAT,YDET,YRES,YRGL,XCAC,YCAT
WHERE
ydet_id=yres_last_ydet_id
AND ydet_ycat_id=ycat_id
AND ycat_notinavl=0
AND yrgl_ydet_id=ydet_id
AND yres_expdeptime=wdat_date  
AND yres_resstatus<>3
AND yres_actdeptime IS NOT NULL
AND yrgl_xcac_id=xcac_id
GROUP BY wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_dep_actchild_bed_cad
as 
select 
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end) actchild
   ,sum(case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end) actchilddayuse
   ,sum(decode(xcac_domestic,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) actdomesticchild
   ,sum(decode(xcac_domestic,1,case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end,0)) actdomesticchilddayuse
   ,sum(decode(xcac_freeofcharge,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) actfreeofchargechild
   ,sum(decode(xcac_freeofcharge,1,case when yres_exparrtime= yres_expdeptime then ydet_noofrooms else 0 end,0)) actfreeofchargechilddayuse
from wdat,ydet,yres,xcac,ycat,yrpe
where
ydet_id=yres_last_ydet_id
and ydet_bed_ycat_id=ycat_id
and ycat_notinavl=0
and yres_resstatus<>3
and yrpe_yres_id=yres_id
and trunc(yrpe_actdeptime)=wdat_date
and yrpe_xcac_id=xcac_id
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_dayroom_room as
select wdat_date,sum(noofrooms) noofrooms from (
select
   wdat_date
   ,decode(nvl(ydet_sharenum,0),0,sum(ydet_noofrooms),avg(ydet_noofrooms)) noofrooms
from yres,wdat,ydet,ycat
where
    ydet_yres_id=yres_id
and yres_exparrtime=yres_expdeptime
and ydet_date=wdat_date   
and yres_resstatus<> 3	
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date,ydet_sharenum
) group by wdat_date;

CREATE OR REPLACE VIEW V8_SYS_HS_DAYROOM_ROOM_CAD
AS 
select wdat_date,ycad_id,sum(noofrooms) noofrooms from (
select
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,decode(nvl(ydet_sharenum,0),0,sum(ydet_noofrooms),avg(ydet_noofrooms)) noofrooms
from yres,wdat,ydet,ycat
where
    ydet_yres_id=yres_id
and yres_exparrtime=yres_expdeptime
and ydet_date=wdat_date   
and yres_resstatus<> 3	
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date,ycat_sum_roomtype_ycad_id,ydet_sharenum
) group by wdat_date,ycad_id;

create or replace view v8_sys_hs_dayroom_adult as
select
   wdat_date
   ,sum(ydet_adultno*ydet_noofrooms) noofadults
from yres,wdat,ydet,ycat
where
    ydet_yres_id=yres_id
and yres_exparrtime=yres_expdeptime
and ydet_date=wdat_date   
and yres_resstatus<> 3	
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date;

create or replace view v8_sys_hs_dayroom_adult_bed as
select
   wdat_date
   ,sum(ydet_noofrooms) noofadults
from yres,wdat,ydet,ycat,yrpe
where
    ydet_yres_id=yres_id
and yres_exparrtime=yres_expdeptime
and ydet_date=wdat_date   
and yres_resstatus<> 3	
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
and yrpe_yres_id=yres_id
and yrpe_xcac_id=0
and yrpe_noshow=0
group by wdat_date;

CREATE OR REPLACE VIEW V8_SYS_HS_DAYROOM_ADULT_CAD
AS 
select
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(ydet_adultno*ydet_noofrooms) noofadults
from yres,wdat,ydet,ycat
where
    ydet_yres_id=yres_id
and yres_exparrtime=yres_expdeptime
and ydet_date=wdat_date   
and yres_resstatus<> 3	
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_dayroom_adult_bedcad
as 
select
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(ydet_noofrooms) noofadults
from yres,wdat,ydet,ycat,yrpe
where
    ydet_yres_id=yres_id
and yres_exparrtime=yres_expdeptime
and ydet_date=wdat_date   
and yres_resstatus<> 3	
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
and yrpe_yres_id=yres_id
and yrpe_xcac_id=0
and yrpe_noshow=0
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_dayroom_child as
select
   wdat_date
   ,sum(yrgl_number*ydet_noofrooms) noofchild
from yres,wdat,ydet,yrgl,ycat
where
    ydet_yres_id=yres_id
and yres_exparrtime=yres_expdeptime
and ydet_date=wdat_date   
and yres_resstatus<> 3	
and ydet_id=yrgl_ydet_id
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date;

create or replace view v8_sys_hs_dayroom_child_bed as
select
   wdat_date
   ,sum(ydet_noofrooms) noofchild
from yres,wdat,ydet,ycat,yrpe
where
    ydet_yres_id=yres_id
and yres_exparrtime=nvl(trunc(yrpe_actdeptime),yres_expdeptime)
and ydet_date=wdat_date   
and yres_resstatus<> 3	
and yrpe_yres_id=yres_id
and yrpe_xcac_id>0
and yrpe_noshow=0
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
group by wdat_date;

CREATE OR REPLACE VIEW V8_SYS_HS_DAYROOM_CHILD_CAD
AS 
select
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(yrgl_number*ydet_noofrooms) noofchild
from yres,wdat,ydet,yrgl,ycat
where
    ydet_yres_id=yres_id
and yres_exparrtime=yres_expdeptime
and ydet_date=wdat_date   
and yres_resstatus<> 3	
and ydet_id=yrgl_ydet_id
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_dayroom_child_bedcad
as 
select
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(ydet_noofrooms) noofchild
from yres,wdat,ydet,ycat,yrpe
where
    ydet_yres_id=yres_id
and yres_exparrtime=nvl(trunc(yrpe_actdeptime),yres_expdeptime)
and ydet_date=wdat_date   
and yres_resstatus<> 3	
and yrpe_yres_id=yres_id
and yrpe_xcac_id>0
and yrpe_noshow=0
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_walkin_room as
select wdat_date,sum(noofrooms) noofrooms from (
select
   wdat_date
   ,decode(nvl(ydet_sharenum,0),0,sum(ydet_noofrooms),avg(ydet_noofrooms)) noofrooms
from yres,wdat,ydet,ycat
where
    ydet_yres_id=yres_id
and ydet_date=wdat_date
and trunc(yres.YRES_ACTARRTIME)=ydet.YDET_DATE 
and YRES_WALKIN=1   
and yres_resstatus<> 3	
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date,ydet_sharenum
) group by wdat_date;

create or replace view v8_sys_hs_walkin_room_prt as
select wdat_date,ydet_ycat_id,sum(noofrooms) noofrooms from (
select
   wdat_date
   ,ydet_ycat_id
   ,decode(nvl(ydet_sharenum,0),0,sum(ydet_noofrooms),avg(ydet_noofrooms)) noofrooms
from yres,wdat,ydet
where
    ydet_yres_id=yres_id
and ydet_date=wdat_date
and trunc(yres.YRES_ACTARRTIME)=ydet.YDET_DATE 
and YRES_WALKIN=1   
and yres_resstatus<> 3	
group by wdat_date,ydet_sharenum,ydet_ycat_id
) group by wdat_date,ydet_ycat_id;

CREATE OR REPLACE VIEW V8_SYS_HS_WALKIN_ROOM_CAD
AS 
SELECT wdat_date,ycat_sum_roomtype_ycad_id YCAD_ID,SUM(noofrooms) noofrooms FROM (
SELECT
   wdat_date
   ,ycat_sum_roomtype_ycad_id
   ,DECODE(NVL(ydet_sharenum,0),0,SUM(ydet_noofrooms),AVG(ydet_noofrooms)) noofrooms
FROM YRES,WDAT,YDET,YCAT
WHERE
    ydet_yres_id=yres_id
AND ydet_ycat_id=ycat_id
AND ycat_notinavl=0	
AND ydet_date=wdat_date
AND TRUNC(YRES.YRES_ACTARRTIME)=YDET.YDET_DATE 
AND YRES_WALKIN=1   
AND yres_resstatus<> 3 
GROUP BY wdat_date,ydet_sharenum,ycat_sum_roomtype_ycad_id
) GROUP BY wdat_date,ycat_sum_roomtype_ycad_id;

CREATE OR REPLACE VIEW V8_SYS_HS_WALKIN_ROOM_ADULTS
AS 
select wdat_date,sum(noofrooms) noofrooms, sum(noofadults) noofadults from (
select
   wdat_date
   ,decode(nvl(ydet_sharenum,0),0,sum(ydet_noofrooms),avg(ydet_noofrooms)) noofrooms
   ,sum(ydet_adultno*ydet_noofrooms) noofadults
from yres,wdat,ydet,ycat
where
    ydet_yres_id=yres_id
and ydet_date=wdat_date
and trunc(yres.YRES_ACTARRTIME)=ydet.YDET_DATE
and YRES_WALKIN=1
and yres_resstatus<> 3
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date,ydet_sharenum
) group by wdat_date;

CREATE OR REPLACE VIEW V8_SYS_HS_WALKIN_ROOM_ADU_XCMA
AS 
select wdat_date,ydet_xcma_id xcma_id,sum(noofrooms) noofrooms, sum(noofadults) noofadults from (
select
   wdat_date
   ,ydet_xcma_id
   ,decode(nvl(ydet_sharenum,0),0,sum(ydet_noofrooms),avg(ydet_noofrooms)) noofrooms
   ,sum(ydet_adultno*ydet_noofrooms) noofadults
from yres,wdat,ydet,ycat
where
    ydet_yres_id=yres_id
and ydet_date=wdat_date
and trunc(yres.YRES_ACTARRTIME)=ydet.YDET_DATE
and YRES_WALKIN=1
and yres_resstatus<> 3
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date,ydet_sharenum,ydet_xcma_id
) group by wdat_date,ydet_xcma_id;

create or replace view v8_sys_hs_walkinRoomAduBedXcma
as 
select
   wdat_date
   ,ydet_xcma_id
   ,sum(ydet_noofrooms) noofadults
from yres,wdat,ydet,ycat,yrpe
where
    ydet_yres_id=yres_id
and ydet_date=wdat_date
and yres_walkin=1
and yres_resstatus<> 3
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
and yres_exparrtime=ydet_date
and yrpe_yres_id=yres_id
and yrpe_actarrtime is not null
and yrpe_xcac_id=0
group by wdat_date,ydet_xcma_id;

CREATE OR REPLACE VIEW V8_SYS_HS_WALKIN_ROOMADULT_PRT
AS 
select wdat_date,ydet_ycat_id,sum(noofrooms) noofrooms, sum(noofadults) noofadults from (
select
   wdat_date
   ,ydet_ycat_id
   ,decode(nvl(ydet_sharenum,0),0,sum(ydet_noofrooms),avg(ydet_noofrooms)) noofrooms
   ,sum(ydet_adultno*ydet_noofrooms) noofadults   
from yres,wdat,ydet
where
    ydet_yres_id=yres_id
and ydet_date=wdat_date
and trunc(yres.YRES_ACTARRTIME)=ydet.YDET_DATE
and YRES_WALKIN=1
and yres_resstatus<> 3
and yres_bed=0
group by wdat_date,ydet_sharenum,ydet_ycat_id
) group by wdat_date,ydet_ycat_id;

create or replace view v8_sys_hs_walkinRoomAduBedPrt
as 
select
   wdat_date
   ,ydet_bed_ycat_id ydet_ycat_id
   ,sum(ydet_noofrooms) noofadults   
from yres,wdat,ydet,yrpe
where
    ydet_yres_id=yres_id
and ydet_date=wdat_date
and yres_walkin=1
and yres_resstatus<> 3
and yres_exparrtime=ydet_date
and yrpe_yres_id=yres_id
and yrpe_actarrtime is not null
and yrpe_xcac_id=0
and yres_bed=1
group by wdat_date,ydet_bed_ycat_id;

CREATE OR REPLACE VIEW V8_SYS_HS_WALKIN_RMADU_PRT_rb as
select day,ycat_id,sum(noofrooms) noofrooms,sum(noofadults) noofadults
from (
select wdat_date day,ydet_ycat_id ycat_id,noofrooms,noofadults from V8_SYS_HS_WALKIN_ROOMADULT_PRT
union
select wdat_date day,ydet_ycat_id ycat_id,0 noofrooms,noofadults from v8_sys_hs_walkinRoomAduBedPrt
) group by day,ycat_id;

CREATE OR REPLACE VIEW V8_SYS_HS_WALKIN_RMADU_PRTRC --Room adult per room type and rate code
AS 
select wdat_date,ydet_ycat_id ycat_id,ydet_yrch_id yrch_id,sum(noofrooms) noofrooms, sum(noofadults) noofadults from (
select
   wdat_date
   ,ydet_ycat_id
   ,ydet_yrch_id
   ,decode(nvl(ydet_sharenum,0),0,sum(ydet_noofrooms),avg(ydet_noofrooms)) noofrooms
   ,sum(ydet_adultno*ydet_noofrooms) noofadults   
from yres,wdat,ydet
where
    ydet_yres_id=yres_id
and ydet_date=wdat_date
and trunc(yres.YRES_ACTARRTIME)=ydet.YDET_DATE
and YRES_WALKIN=1
and yres_resstatus<> 3
group by wdat_date,ydet_sharenum,ydet_ycat_id,ydet_yrch_id
) group by wdat_date,ydet_ycat_id,ydet_yrch_id;

create or replace view v8_sys_hs_walkin_RmAduBedPrtRc --bed room adult per room type and rate code
as 
select
   wdat_date
   ,ydet_bed_ycat_id ydet_ycat_id
   ,ydet_yrch_id
   ,sum(ydet_noofrooms) noofadults   
from yres,wdat,ydet,yrpe
where
    ydet_yres_id=yres_id
and ydet_date=wdat_date
and yres_walkin=1
and yres_resstatus<> 3
and yres_exparrtime=ydet_date
and yrpe_yres_id=yres_id
and yrpe_actarrtime is not null
and yrpe_xcac_id=0
group by wdat_date,ydet_bed_ycat_id,ydet_yrch_id;

create or replace view v8_sys_hs_walkin_adult as
select
   wdat_date
   ,sum(ydet_adultno*ydet_noofrooms) noofadults
from yres,wdat,ydet,ycat
where
    ydet_yres_id=yres_id
and ydet_date=wdat_date
and YRES_WALKIN=1   
and trunc(yres.YRES_ACTARRTIME)=ydet.YDET_DATE
and yres_resstatus<> 3	
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and yres_bed=0
group by wdat_date;

create or replace view v8_sys_hs_walkin_adult_bed
as 
select
   wdat_date
   ,sum(ydet_noofrooms) noofadults
from yres,wdat,ydet,ycat,yrpe
where
    ydet_yres_id=yres_id
and ydet_date=wdat_date
and yres_walkin=1
and yres_resstatus<> 3
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
and yres_exparrtime=ydet_date
and yrpe_yres_id=yres_id
and yrpe_actarrtime is not null
and yrpe_xcac_id=0
and yres_bed=1
group by wdat_date;

CREATE OR REPLACE VIEW V8_SYS_HS_WALKIN_ADULT_CAD
AS 
select
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(ydet_adultno*ydet_noofrooms) noofadults
from yres,wdat,ydet,ycat
where
    ydet_yres_id=yres_id
and ydet_date=wdat_date
and YRES_WALKIN=1   
and trunc(yres.YRES_ACTARRTIME)=ydet.YDET_DATE
and yres_resstatus<> 3	
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_walkin_adult_bed_cad
as 
select
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(ydet_noofrooms) noofadults
from yres,wdat,ydet,ycat,yrpe
where
    ydet_yres_id=yres_id
and ydet_date=wdat_date
and yres_walkin=1   
and yres_resstatus<> 3	
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
and yres_exparrtime=ydet_date
and yrpe_yres_id=yres_id
and yrpe_actarrtime is not null
and yrpe_xcac_id=0
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_walkin_child as
select
   wdat_date
   ,sum(yrgl_number*ydet_noofrooms) noofchild
from yres,wdat,ydet,yrgl,ycat
where
    ydet_yres_id=yres_id
and ydet_date=wdat_date
and YRES_WALKIN=1   
and trunc(yres.YRES_ACTARRTIME)=ydet.YDET_DATE
and yres_resstatus<> 3
and ydet_id=yrgl_ydet_id	
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date;

create or replace view v8_sys_hs_walkin_child_bed as
select
   wdat_date
   ,sum(ydet_noofrooms) noofchild
from yres,wdat,ydet,ycat,yrpe
where
    ydet_yres_id=yres_id
and ydet_date=wdat_date
and YRES_WALKIN=1   
and yres_resstatus<> 3
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
and yres_exparrtime=ydet_date
and yrpe_yres_id=yres_id
and yrpe_actarrtime is not null
and yrpe_xcac_id>0
group by wdat_date;

CREATE OR REPLACE VIEW V8_SYS_HS_WALKIN_CHILD_CAD
AS 
select
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(yrgl_number*ydet_noofrooms) noofchild
from yres,wdat,ydet,yrgl,ycat
where
    ydet_yres_id=yres_id
and ydet_date=wdat_date
and YRES_WALKIN=1   
and trunc(yres.YRES_ACTARRTIME)=ydet.YDET_DATE
and yres_resstatus<> 3
and ydet_id=yrgl_ydet_id	
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_walkin_child_cad_bed
as 
select
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(ydet_noofrooms) noofchild
from yres,wdat,ydet,ycat,yrpe
where
    ydet_yres_id=yres_id
and ydet_date=wdat_date
and yres_walkin=1   
and yres_resstatus<> 3
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
and yres_exparrtime=ydet_date
and yrpe_yres_id=yres_id
and yrpe_actarrtime is not null
and yrpe_xcac_id>0
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_sameday_room as
select wdat_date,sum(noofrooms) noofrooms from (
select
   wdat_date
   ,decode(nvl(ydet_sharenum,0),0,sum(ydet_noofrooms),avg(ydet_noofrooms)) noofrooms
from yres,wdat,wmlg,ydet,ycat
where
    wmlg_tablename='YRES' 
and wmlg_table_id=yres_id
and ydet_yres_id=yres_id
and ydet_date=wdat_date
and trunc(WMLG_TIME_CREATION)=wdat_Date
and yres_exparrtime=wdat_Date
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and yres_resstatus<> 3
group by wdat_date,ydet_sharenum
) group by wdat_date;

CREATE OR REPLACE VIEW V8_SYS_HS_SAMEDAY_ROOM_CAD
AS 
select wdat_date,sum(noofrooms) noofrooms,ycad_id from (
select
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,decode(nvl(ydet_sharenum,0),0,sum(ydet_noofrooms),avg(ydet_noofrooms)) noofrooms
from yres,wdat,wmlg,ydet,ycat
where
    wmlg_tablename='YRES' 
and wmlg_table_id=yres_id
and ydet_yres_id=yres_id
and ydet_date=wdat_date
and trunc(WMLG_TIME_CREATION)=wdat_Date
and yres_exparrtime=wdat_Date
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and yres_resstatus<> 3
group by wdat_date,ycat_sum_roomtype_ycad_id,ydet_sharenum
) group by wdat_date,ycad_id;

create or replace view v8_sys_hs_sameday_adult as
select
   wdat_date
   ,sum(ydet_adultno*ydet_noofrooms) noofadults
from yres,wdat,wmlg,ydet,ycat
where
    wmlg_tablename='YRES' 
and wmlg_table_id=yres_id
and ydet_yres_id=yres_id
and ydet_date=wdat_date
and trunc(WMLG_TIME_CREATION)=wdat_Date
and yres_exparrtime=wdat_Date
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and yres_resstatus<> 3
group by wdat_date;

create or replace view v8_sys_hs_sameday_adult_bed as
select
   wdat_date
   ,sum(ydet_noofrooms) noofadults
from yres,wdat,wmlg,ydet,ycat,yrpe
where
    wmlg_tablename='YRES' 
and wmlg_table_id=yres_id
and ydet_yres_id=yres_id
and ydet_date=wdat_date
and trunc(WMLG_TIME_CREATION)=wdat_Date
and yres_exparrtime=wdat_Date
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
and yres_resstatus<> 3
and yrpe_yres_id=yres_id
and yrpe_xcac_id=0
and yrpe_noshow=0
group by wdat_date;

CREATE OR REPLACE VIEW V8_SYS_HS_SAMEDAY_ADULT_CAD
AS 
select
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(ydet_adultno*ydet_noofrooms) noofadults
from yres,wdat,wmlg,ydet,ycat
where
    wmlg_tablename='YRES' 
and wmlg_table_id=yres_id
and ydet_yres_id=yres_id
and ydet_date=wdat_date
and trunc(WMLG_TIME_CREATION)=wdat_Date
and yres_exparrtime=wdat_Date
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and yres_resstatus<> 3
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_sameday_adult_bedcad
as 
select
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(ydet_noofrooms) noofadults
from yres,wdat,wmlg,ydet,ycat,yrpe
where
    wmlg_tablename='YRES' 
and wmlg_table_id=yres_id
and ydet_yres_id=yres_id
and ydet_date=wdat_date
and trunc(WMLG_TIME_CREATION)=wdat_Date
and yres_exparrtime=wdat_Date
and ycat_id=ydet_any_ycat_id
and ycat_notinavl=0
and yres_resstatus<> 3
and yrpe_yres_id=yres_id
and yrpe_xcac_id=0
and yrpe_noshow=0
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_sameday_child as
select
   wdat_date
   ,sum(yrgl_number*ydet_noofrooms) noofchild
from yres,wdat,wmlg,ydet,yrgl,ycat
where
    wmlg_tablename='YRES' 
and wmlg_table_id=yres_id
and ydet_yres_id=yres_id
and ydet_date=wdat_date
and trunc(WMLG_TIME_CREATION)=wdat_Date
and yres_exparrtime=wdat_Date
and ydet_id=yrgl_ydet_id
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and yres_resstatus<> 3
group by wdat_date;

create or replace view v8_sys_hs_sameday_child_bed as
select
   wdat_date
   ,sum(ydet_noofrooms) noofchild
from yres,wdat,wmlg,ydet,ycat,yrpe
where
    wmlg_tablename='YRES' 
and wmlg_table_id=yres_id
and ydet_yres_id=yres_id
and ydet_date=wdat_date
and trunc(WMLG_TIME_CREATION)=wdat_Date
and yres_exparrtime=wdat_Date
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
and yres_resstatus<> 3
and yrpe_yres_id=yres_id
and yrpe_xcac_id>0
and yrpe_noshow=0
group by wdat_date;

CREATE OR REPLACE VIEW V8_SYS_HS_SAMEDAY_CHILD_CAD
AS 
select
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(yrgl_number*ydet_noofrooms) noofchild
from yres,wdat,wmlg,ydet,yrgl,ycat
where
    wmlg_tablename='YRES' 
and wmlg_table_id=yres_id
and ydet_yres_id=yres_id
and ydet_date=wdat_date
and trunc(WMLG_TIME_CREATION)=wdat_Date
and yres_exparrtime=wdat_Date
and ydet_id=yrgl_ydet_id
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
and yres_resstatus<> 3
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_sameday_child_bedcad
as 
select
   wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(ydet_noofrooms) noofchild
from yres,wdat,wmlg,ydet,ycat,yrpe
where
    wmlg_tablename='YRES' 
and wmlg_table_id=yres_id
and ydet_yres_id=yres_id
and ydet_date=wdat_date
and trunc(WMLG_TIME_CREATION)=wdat_Date
and yres_exparrtime=wdat_Date
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
and yres_resstatus<> 3
and yrpe_yres_id=yres_id
and yrpe_xcac_id>0
and yrpe_noshow=0
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_hs_arr_dep_combined as
select /*+ RULE */ wdat_date
       ,sum(decode(vtype,1,num,0)) arrexproom
       ,sum(decode(vtype,2,num,0)) arrexpadult
       ,sum(decode(vtype,22,num,0)) arrexpchild
       ,sum(decode(vtype,3,num,0)) arractroom
       ,sum(decode(vtype,4,num,0)) arractadult
       ,sum(decode(vtype,44,num,0)) arractchild
       ,sum(decode(vtype,5,num,0)) arrpreroom
       ,sum(decode(vtype,6,num,0)) arrpreadult
       ,sum(decode(vtype,66,num,0)) arrprechild
       ,sum(decode(vtype,7,num,0)) depexproom
       ,sum(decode(vtype,8,num,0)) depexpadult
       ,sum(decode(vtype,88,num,0)) depexpchild
       ,sum(decode(vtype,9,num,0)) depactroom
       ,sum(decode(vtype,10,num,0)) depactadult
       ,sum(decode(vtype,1010,num,0)) depactchild
       ,sum(decode(vtype,11,num,0)) dayroomroom
       ,sum(decode(vtype,12,num,0)) dayroomadult
       ,sum(decode(vtype,1212,num,0)) dayroomchild
       ,sum(decode(vtype,13,num,0)) walkinroom
       ,sum(decode(vtype,14,num,0)) walkinadult
       ,sum(decode(vtype,1414,num,0)) walkinchild
       ,sum(decode(vtype,15,num,0)) samedayroom
       ,sum(decode(vtype,16,num,0)) samedayadult
       ,sum(decode(vtype,1616,num,0)) samedaychild
       ,
        sum(decode(vtype, 1,numdu,0)) arrexproomDayUse
       ,sum(decode(vtype, 2,numdu,0)) arrexpadultDayUse
       ,sum(decode(vtype,22,numdu,0)) arrexpchildDayUse
       ,sum(decode(vtype, 3,numdu,0)) arractroomDayUse
       ,sum(decode(vtype, 4,numdu,0)) arractadultDayUse
       ,sum(decode(vtype,44,numdu,0)) arractchildDayUse
       ,sum(decode(vtype, 5,numdu,0)) arrpreroomDayUse
       ,sum(decode(vtype, 6,numdu,0)) arrpreadultDayUse
       ,sum(decode(vtype,66,numdu,0)) arrprechildDayUse
       ,sum(decode(vtype, 7,numdu,0)) depexproomDayUse
       ,sum(decode(vtype, 8,numdu,0)) depexpadultDayUse
       ,sum(decode(vtype,88,numdu,0)) depexpchildDayUse
       ,sum(decode(vtype, 9,numdu,0)) depactroomDayUse
       ,sum(decode(vtype,10,numdu,0)) depactadultDayUse
       ,sum(decode(vtype,1010,numdu,0)) depactchildDayUse
from (
select wdat_date,   1 vtype,exproom num,exproomdayuse numdu    from v8_sys_hs_arr_exproom 
union
select wdat_date,   2 vtype,expadult num,expadultdayuse numdu    from v8_sys_hs_arr_expadult 
union
select wdat_date,  22 vtype,expchild num,expchilddayuse numdu    from v8_sys_hs_arr_expchild 
union
select wdat_date,   3 vtype,actroom num ,actroomdayuse numdu    from v8_sys_hs_arr_actroom 
union                                   
select wdat_date,   4 vtype,actadult num,actadultdayuse numdu    from v8_sys_hs_arr_actadult
union                                   
select wdat_date,  44 vtype,actchild num,actchilddayuse numdu    from v8_sys_hs_arr_actchild
union                                   
select wdat_date,   5 vtype,preroom num ,preroomdayuse numdu    from v8_sys_hs_arr_preroom 
union
select wdat_date,   6 vtype,preadult num  ,preadultDayUse numdu    from v8_sys_hs_arr_preadult
union                                     
select wdat_date,  66 vtype,prechild num  ,prechildDayUse numdu    from v8_sys_hs_arr_prechild
union                                     
select wdat_date,   7 vtype,exproom num   ,exproomDayUse numdu    from v8_sys_hs_dep_exproom
union                                     
select wdat_date,   8 vtype,expadult num  ,expadultDayUse numdu    from v8_sys_hs_dep_expadult
union                                     
select wdat_date,  88 vtype,expchild num  ,expchildDayUse numdu    from v8_sys_hs_dep_expchild
union                                     
select wdat_date,   9 vtype,actroom num   ,actroomDayUse numdu    from v8_sys_hs_dep_actroom
union                                     
select wdat_date,  10 vtype,actadult num  ,actadultDayUse numdu    from v8_sys_hs_dep_actadult
union                                     
select wdat_date,1010 vtype,actchild num  ,actchildDayUse numdu    from v8_sys_hs_dep_actchild
union                                     
select wdat_date,  11 vtype,noofrooms num ,0 numdu    from v8_sys_hs_dayroom_room
union                                     
select wdat_date,  12 vtype,noofadults num ,0 numdu  from v8_sys_hs_dayroom_adult
union                                     
select wdat_date,1212 vtype,noofchild num  ,0 numdu  from v8_sys_hs_dayroom_child
union                                     
select wdat_date,  13 vtype,noofrooms num  ,0 numdu  from v8_sys_hs_walkin_room
union                                     
select wdat_date,  14 vtype,noofadults num ,0 numdu  from v8_sys_hs_walkin_adult
union 
select wdat_date,1414 vtype,noofchild num  ,0 numdu  from v8_sys_hs_walkin_child
union                                     
select wdat_date,  15 vtype,noofrooms num  ,0 numdu  from v8_sys_hs_sameday_room
union                                     
select wdat_date,  16 vtype,noofadults num ,0 numdu  from v8_sys_hs_sameday_adult
union                                     
select wdat_date,1616 vtype,noofchild num  ,0 numdu  from v8_sys_hs_sameday_child
union 
select wdat_date,  0 vtype,0 num,0 numdu   from wdat
)
group by wdat_date;

create or replace view v8_sys_hs_arr_dep_combined_bed as
select /*+ RULE */ wdat_date
       ,sum(decode(vtype,1,num,0))      arrexproom
       ,sum(decode(vtype,2,num,0))      arrexpadult
       ,sum(decode(vtype,22,num,0))     arrexpchild
       ,sum(decode(vtype,3,num,0))      arractroom
       ,sum(decode(vtype,4,num,0))      arractadult
       ,sum(decode(vtype,44,num,0))     arractchild
       ,sum(decode(vtype,5,num,0))      arrpreroom
       ,sum(decode(vtype,6,num,0))      arrpreadult
       ,sum(decode(vtype,66,num,0))     arrprechild
       ,sum(decode(vtype,7,num,0))      depexproom
       ,sum(decode(vtype,8,num,0))      depexpadult
       ,sum(decode(vtype,88,num,0))     depexpchild
       ,sum(decode(vtype,9,num,0))      depactroom
       ,sum(decode(vtype,10,num,0))     depactadult
       ,sum(decode(vtype,1010,num,0))   depactchild
       ,sum(decode(vtype,11,num,0))     dayroomroom
       ,sum(decode(vtype,12,num,0))     dayroomadult
       ,sum(decode(vtype,1212,num,0))   dayroomchild
       ,sum(decode(vtype,13,num,0))     walkinroom
       ,sum(decode(vtype,14,num,0))     walkinadult
       ,sum(decode(vtype,1414,num,0))   walkinchild
       ,sum(decode(vtype,15,num,0))     samedayroom
       ,sum(decode(vtype,16,num,0))     samedayadult
       ,sum(decode(vtype,1616,num,0))   samedaychild
       ,
        sum(decode(vtype, 1,numdu,0))   arrexproomDayUse
       ,sum(decode(vtype, 2,numdu,0))   arrexpadultDayUse
       ,sum(decode(vtype,22,numdu,0))   arrexpchildDayUse
       ,sum(decode(vtype, 3,numdu,0))   arractroomDayUse
       ,sum(decode(vtype, 4,numdu,0))   arractadultDayUse
       ,sum(decode(vtype,44,numdu,0))   arractchildDayUse
       ,sum(decode(vtype, 5,numdu,0))   arrpreroomDayUse
       ,sum(decode(vtype, 6,numdu,0))   arrpreadultDayUse
       ,sum(decode(vtype,66,numdu,0))   arrprechildDayUse
       ,sum(decode(vtype, 7,numdu,0))   depexproomDayUse
       ,sum(decode(vtype, 8,numdu,0))   depexpadultDayUse
       ,sum(decode(vtype,88,numdu,0))   depexpchildDayUse
       ,sum(decode(vtype, 9,numdu,0))   depactroomDayUse
       ,sum(decode(vtype,10,numdu,0))   depactadultDayUse
       ,sum(decode(vtype,1010,numdu,0)) depactchildDayUse
from (
--select wdat_date,   1 vtype,exproom num,exproomdayuse numdu    from v8_sys_hs_arr_exproom_bed 
--union
select wdat_date,   2 vtype,expadult num,expadultdayuse numdu    from v8_sys_hs_arr_expadult_bed 
union
select wdat_date,  22 vtype,expchild num,expchilddayuse numdu    from v8_sys_hs_arr_expchild_bed 
union
--select wdat_date,   3 vtype,actroom num ,actroomdayuse numdu    from v8_sys_hs_arr_actroom_bed 
--union                                   
select wdat_date,   4 vtype,actadult num,actadultdayuse numdu    from v8_sys_hs_arr_actadult_bed
union                                   
select wdat_date,  44 vtype,actchild num,actchilddayuse numdu    from v8_sys_hs_arr_actchild_bed
union                                   
--select wdat_date,   5 vtype,preroom num ,preroomdayuse numdu    from v8_sys_hs_arr_preroom_bed 
--union
select wdat_date,   6 vtype,preadult num  ,preadultDayUse numdu    from v8_sys_hs_arr_preadult_bed
union                                     
select wdat_date,  66 vtype,prechild num  ,prechildDayUse numdu    from v8_sys_hs_arr_prechild_bed
union                                     
--select wdat_date,   7 vtype,exproom num   ,exproomDayUse numdu    from v8_sys_hs_dep_exproom_bed
--union                                     
select wdat_date,   8 vtype,expadult num  ,expadultDayUse numdu    from v8_sys_hs_dep_expadult_bed
union                                     
select wdat_date,  88 vtype,expchild num  ,expchildDayUse numdu    from v8_sys_hs_dep_expchild_bed
union                                     
--select wdat_date,   9 vtype,actroom num   ,actroomDayUse numdu    from v8_sys_hs_dep_actroom
--union                                     
select wdat_date,  10 vtype,actadult num  ,actadultDayUse numdu    from v8_sys_hs_dep_actadult_bed
union                                     
select wdat_date,1010 vtype,actchild num  ,actchildDayUse numdu    from v8_sys_hs_dep_actchild_bed
union                                     
--select wdat_date,  11 vtype,noofrooms num ,0 numdu    from v8_sys_hs_dayroom_room_bed
--union                                     
select wdat_date,  12 vtype,noofadults num ,0 numdu  from v8_sys_hs_dayroom_adult_bed
union                                     
select wdat_date,1212 vtype,noofchild num  ,0 numdu  from v8_sys_hs_dayroom_child_bed
union                                     
--select wdat_date,  13 vtype,noofrooms num  ,0 numdu  from v8_sys_hs_walkin_room_bed
--union                                     
select wdat_date,  14 vtype,noofadults num ,0 numdu  from v8_sys_hs_walkin_adult_bed
union 
select wdat_date,1414 vtype,noofchild num  ,0 numdu  from v8_sys_hs_walkin_child_bed
union                                     
--select wdat_date,  15 vtype,noofrooms num  ,0 numdu  from v8_sys_hs_sameday_room_bed
--union                                     
select wdat_date,  16 vtype,noofadults num ,0 numdu  from v8_sys_hs_sameday_adult_bed
union                                     
select wdat_date,1616 vtype,noofchild num  ,0 numdu  from v8_sys_hs_sameday_child_bed
union 
select wdat_date,  0 vtype,0 num,0 numdu   from wdat
)
group by wdat_date;

create or replace view v8_sys_hs_arr_dep_combined_rb as
select
 wdat_date
,sum(arrexproom) arrexproom
,sum(arrexpadult) arrexpadult
,sum(arrexpchild) arrexpchild
,sum(arractroom) arractroom
,sum(arractadult) arractadult
,sum(arractchild) arractchild
,sum(arrpreroom) arrpreroom
,sum(arrpreadult) arrpreadult
,sum(arrprechild) arrprechild
,sum(depexproom) depexproom
,sum(depexpadult) depexpadult
,sum(depexpchild) depexpchild
,sum(depactroom) depactroom
,sum(depactadult) depactadult
,sum(depactchild) depactchild
,sum(dayroomroom) dayroomroom
,sum(dayroomadult) dayroomadult
,sum(dayroomchild) dayroomchild
,sum(walkinroom) walkinroom
,sum(walkinadult) walkinadult
,sum(walkinchild) walkinchild
,sum(samedayroom) samedayroom
,sum(samedayadult) samedayadult
,sum(samedaychild) samedaychild
,sum(arrexproomdayuse) arrexproomdayuse
,sum(arrexpadultdayuse) arrexpadultdayuse
,sum(arrexpchilddayuse) arrexpchilddayuse
,sum(arractroomdayuse) arractroomdayuse
,sum(arractadultdayuse) arractadultdayuse
,sum(arractchilddayuse) arractchilddayuse
,sum(arrpreroomdayuse) arrpreroomdayuse
,sum(arrpreadultdayuse) arrpreadultdayuse
,sum(arrprechilddayuse) arrprechilddayuse
,sum(depexproomdayuse) depexproomdayuse
,sum(depexpadultdayuse) depexpadultdayuse
,sum(depexpchilddayuse) depexpchilddayuse
,sum(depactroomdayuse) depactroomdayuse
,sum(depactadultdayuse) depactadultdayuse
,sum(depactchilddayuse) depactchilddayuse
from (
select 
 wdat_date
,arrexproom
,arrexpadult
,arrexpchild
,arractroom
,arractadult
,arractchild
,arrpreroom
,arrpreadult
,arrprechild
,depexproom
,depexpadult
,depexpchild
,depactroom
,depactadult
,depactchild
,dayroomroom
,dayroomadult
,dayroomchild
,walkinroom
,walkinadult
,walkinchild
,samedayroom
,samedayadult
,samedaychild
,arrexproomdayuse
,arrexpadultdayuse
,arrexpchilddayuse
,arractroomdayuse
,arractadultdayuse
,arractchilddayuse
,arrpreroomdayuse
,arrpreadultdayuse
,arrprechilddayuse
,depexproomdayuse
,depexpadultdayuse
,depexpchilddayuse
,depactroomdayuse
,depactadultdayuse
,depactchilddayuse
from v8_sys_hs_arr_dep_combined
union all
select 
 wdat_date
,arrexproom
,arrexpadult
,arrexpchild
,arractroom
,arractadult
,arractchild
,arrpreroom
,arrpreadult
,arrprechild
,depexproom
,depexpadult
,depexpchild
,depactroom
,depactadult
,depactchild
,dayroomroom
,dayroomadult
,dayroomchild
,walkinroom
,walkinadult
,walkinchild
,samedayroom
,samedayadult
,samedaychild
,arrexproomdayuse
,arrexpadultdayuse
,arrexpchilddayuse
,arractroomdayuse
,arractadultdayuse
,arractchilddayuse
,arrpreroomdayuse
,arrpreadultdayuse
,arrprechilddayuse
,depexproomdayuse
,depexpadultdayuse
,depexpchilddayuse
,depactroomdayuse
,depactadultdayuse
,depactchilddayuse
from v8_sys_hs_arr_dep_combined_bed
) group by wdat_date;

create or replace view v8_sys_hs_arr_dep_combined2 as
select wdat_date
       ,nvl((select exproom from v8_sys_hs_arr_exproom v where v.wdat_date=w.wdat_date),0) arrexproom
       ,nvl((select expadult from v8_sys_hs_arr_expadult v where v.wdat_date=w.wdat_date),0) arrexpadult
       ,nvl((select expchild from v8_sys_hs_arr_expchild v where v.wdat_date=w.wdat_date),0) arrexpchild
       ,nvl((select actroom from v8_sys_hs_arr_actroom v where v.wdat_date=w.wdat_date),0) arractroom
       ,nvl((select actadult from v8_sys_hs_arr_actadult v where v.wdat_date=w.wdat_date),0) arractadult
       ,nvl((select actchild from v8_sys_hs_arr_actchild v where v.wdat_date=w.wdat_date),0) arractchild
       ,nvl((select preroom from v8_sys_hs_arr_preroom v where v.wdat_date=w.wdat_date),0) arrpreroom
       ,nvl((select preadult from v8_sys_hs_arr_preadult v where v.wdat_date=w.wdat_date),0) arrpreadult
       ,nvl((select prechild from v8_sys_hs_arr_prechild v where v.wdat_date=w.wdat_date),0) arrprechild
       ,nvl((select exproom from v8_sys_hs_dep_exproom v where v.wdat_date=w.wdat_date),0) depexproom
       ,nvl((select expadult from v8_sys_hs_dep_expadult v where v.wdat_date=w.wdat_date),0) depexpadult
       ,nvl((select expchild from v8_sys_hs_dep_expchild v where v.wdat_date=w.wdat_date),0) depexpchild
       ,nvl((select actroom from v8_sys_hs_dep_actroom v where v.wdat_date=w.wdat_date),0) depactroom
       ,nvl((select actadult from v8_sys_hs_dep_actadult v where v.wdat_date=w.wdat_date),0) depactadult
       ,nvl((select actchild from v8_sys_hs_dep_actchild v where v.wdat_date=w.wdat_date),0) depactchild
       ,nvl((select noofrooms from v8_sys_hs_dayroom_room v where v.wdat_date=w.wdat_date),0) dayroomroom
       ,nvl((select noofadults from v8_sys_hs_dayroom_adult v where v.wdat_date=w.wdat_date),0) dayroomadult
       ,nvl((select noofchild from v8_sys_hs_dayroom_child v where v.wdat_date=w.wdat_date),0) dayroomchild
       ,nvl((select noofrooms from v8_sys_hs_walkin_room v where v.wdat_date=w.wdat_date),0) walkinroom
       ,nvl((select noofadults from v8_sys_hs_walkin_adult v where v.wdat_date=w.wdat_date),0) walkinadult
       ,nvl((select noofchild from v8_sys_hs_walkin_child v where v.wdat_date=w.wdat_date),0) walkinchild
       ,nvl((select noofrooms from v8_sys_hs_sameday_room v where v.wdat_date=w.wdat_date),0) samedayroom
       ,nvl((select noofadults from v8_sys_hs_sameday_adult v where v.wdat_date=w.wdat_date),0) samedayadult
       ,nvl((select noofchild from v8_sys_hs_sameday_child v where v.wdat_date=w.wdat_date),0) samedaychild
from wdat w;

CREATE OR REPLACE VIEW V8_SYS_HS_ARR_DEP_COMB_CAD
AS 
select /*+ RULE */ wdat_date
       ,ycad_id
       ,sum(decode(vtype,1,num,0)) arrexproom
       ,sum(decode(vtype,2,num,0)) arrexpadult
       ,sum(decode(vtype,22,num,0)) arrexpchild
       ,sum(decode(vtype,3,num,0)) arractroom
       ,sum(decode(vtype,4,num,0)) arractadult
       ,sum(decode(vtype,44,num,0)) arractchild
       ,sum(decode(vtype,5,num,0)) arrpreroom
       ,sum(decode(vtype,6,num,0)) arrpreadult
       ,sum(decode(vtype,66,num,0)) arrprechild
       ,sum(decode(vtype,7,num,0)) depexproom
       ,sum(decode(vtype,8,num,0)) depexpadult
       ,sum(decode(vtype,88,num,0)) depexpchild
       ,sum(decode(vtype,9,num,0)) depactroom
       ,sum(decode(vtype,10,num,0)) depactadult
       ,sum(decode(vtype,1010,num,0)) depactchild
       ,sum(decode(vtype,11,num,0)) dayroomroom
       ,sum(decode(vtype,12,num,0)) dayroomadult
       ,sum(decode(vtype,1212,num,0)) dayroomchild
       ,sum(decode(vtype,13,num,0)) walkinroom
       ,sum(decode(vtype,14,num,0)) walkinadult
       ,sum(decode(vtype,1414,num,0)) walkinchild
       ,sum(decode(vtype,15,num,0)) samedayroom
       ,sum(decode(vtype,16,num,0)) samedayadult
       ,sum(decode(vtype,1616,num,0)) samedaychild
       ,
        sum(decode(vtype, 1,numdu,0)) arrexproomDayUse
       ,sum(decode(vtype, 2,numdu,0)) arrexpadultDayUse
       ,sum(decode(vtype,22,numdu,0)) arrexpchildDayUse
       ,sum(decode(vtype, 3,numdu,0)) arractroomDayUse
       ,sum(decode(vtype, 4,numdu,0)) arractadultDayUse
       ,sum(decode(vtype,44,numdu,0)) arractchildDayUse
       ,sum(decode(vtype, 5,numdu,0)) arrpreroomDayUse
       ,sum(decode(vtype, 6,numdu,0)) arrpreadultDayUse
       ,sum(decode(vtype,66,numdu,0)) arrprechildDayUse
       ,sum(decode(vtype, 7,numdu,0)) depexproomDayUse
       ,sum(decode(vtype, 8,numdu,0)) depexpadultDayUse
       ,sum(decode(vtype,88,numdu,0)) depexpchildDayUse
       ,sum(decode(vtype, 9,numdu,0)) depactroomDayUse
       ,sum(decode(vtype,10,numdu,0)) depactadultDayUse
       ,sum(decode(vtype,1010,numdu,0)) depactchildDayUse
from (
select wdat_date,   1 vtype,exproom num  ,exproomDayUse numdu   ,ycad_id    from v8_sys_hs_arr_exproom_cad
union                                    
select wdat_date,   2 vtype,expadult num ,expadultDayUse numdu  ,ycad_id    from v8_sys_hs_arr_expadult_cad 
union                                    
select wdat_date,  22 vtype,expchild num ,expchildDayUse numdu  ,ycad_id    from v8_sys_hs_arr_expchild_cad 
union                                    
select wdat_date,   3 vtype,actroom num  ,actroomDayUse numdu   ,ycad_id    from v8_sys_hs_arr_actroom_cad 
union                                    
select wdat_date,   4 vtype,actadult num ,actadultDayUse numdu  ,ycad_id    from v8_sys_hs_arr_actadult_cad
union                                    
select wdat_date,  44 vtype,actchild num ,actchildDayUse numdu  ,ycad_id    from v8_sys_hs_arr_actchild_cad
union                                    
select wdat_date,   5 vtype,preroom num  ,preroomDayUse numdu   ,ycad_id    from v8_sys_hs_arr_preroom_cad 
union                                    
select wdat_date,   6 vtype,preadult num ,preadultDayUse numdu  ,ycad_id    from v8_sys_hs_arr_preadult_cad
union                                    
select wdat_date,  66 vtype,prechild num ,prechildDayUse numdu  ,ycad_id    from v8_sys_hs_arr_prechild_cad
union                                    
select wdat_date,   7 vtype,exproom num  ,exproomDayUse numdu   ,ycad_id    from v8_sys_hs_dep_exproom_cad
union                                    
select wdat_date,   8 vtype,expadult num ,expadultDayUse numdu  ,ycad_id    from v8_sys_hs_dep_expadult_cad
union                                    
select wdat_date,  88 vtype,expchild num ,expchildDayUse numdu  ,ycad_id    from v8_sys_hs_dep_expchild_cad
union                                    
select wdat_date,   9 vtype,actroom num  ,actroomDayUse numdu   ,ycad_id    from v8_sys_hs_dep_actroom_cad
union                                    
select wdat_date,  10 vtype,actadult num ,actadultDayUse numdu  ,ycad_id    from v8_sys_hs_dep_actadult_cad
union 
select wdat_date,1010 vtype,actchild num ,actchildDayUse numdu  ,ycad_id    from v8_sys_hs_dep_actchild_cad
union 
select wdat_date,  11 vtype,noofrooms num ,0 numdu ,ycad_id    from v8_sys_hs_dayroom_room_cad
union 
select wdat_date,  12 vtype,noofadults num,0 numdu ,ycad_id    from v8_sys_hs_dayroom_adult_cad
union 
select wdat_date,1212 vtype,noofchild num ,0 numdu ,ycad_id    from v8_sys_hs_dayroom_child_cad
union 
select wdat_date,  13 vtype,noofrooms num ,0 numdu ,ycad_id    from v8_sys_hs_walkin_room_cad
union 
select wdat_date,  14 vtype,noofadults num,0 numdu ,ycad_id    from v8_sys_hs_walkin_adult_cad
union 
select wdat_date,1414 vtype,noofchild num ,0 numdu ,ycad_id    from v8_sys_hs_walkin_child_cad
union 
select wdat_date,  15 vtype,noofrooms num ,0 numdu ,ycad_id    from v8_sys_hs_sameday_room_cad
union 
select wdat_date,  16 vtype,noofadults num,0 numdu ,ycad_id    from v8_sys_hs_sameday_adult_cad
union 
select wdat_date,1616 vtype,noofchild num ,0 numdu ,ycad_id    from v8_sys_hs_sameday_child_cad
union 
select wdat_date,   0 vtype,0 num,0 numdu,0 ycad_id from wdat
)
group by wdat_date,ycad_id;

create or replace view v8_sys_hs_arr_dep_comb_cad_bed
as 
select /*+ RULE */ wdat_date
       ,ycad_id
       ,sum(decode(vtype,1,num,0)) arrexproom
       ,sum(decode(vtype,2,num,0)) arrexpadult
       ,sum(decode(vtype,22,num,0)) arrexpchild
       ,sum(decode(vtype,3,num,0)) arractroom
       ,sum(decode(vtype,4,num,0)) arractadult
       ,sum(decode(vtype,44,num,0)) arractchild
       ,sum(decode(vtype,5,num,0)) arrpreroom
       ,sum(decode(vtype,6,num,0)) arrpreadult
       ,sum(decode(vtype,66,num,0)) arrprechild
       ,sum(decode(vtype,7,num,0)) depexproom
       ,sum(decode(vtype,8,num,0)) depexpadult
       ,sum(decode(vtype,88,num,0)) depexpchild
       ,sum(decode(vtype,9,num,0)) depactroom
       ,sum(decode(vtype,10,num,0)) depactadult
       ,sum(decode(vtype,1010,num,0)) depactchild
       ,sum(decode(vtype,11,num,0)) dayroomroom
       ,sum(decode(vtype,12,num,0)) dayroomadult
       ,sum(decode(vtype,1212,num,0)) dayroomchild
       ,sum(decode(vtype,13,num,0)) walkinroom
       ,sum(decode(vtype,14,num,0)) walkinadult
       ,sum(decode(vtype,1414,num,0)) walkinchild
       ,sum(decode(vtype,15,num,0)) samedayroom
       ,sum(decode(vtype,16,num,0)) samedayadult
       ,sum(decode(vtype,1616,num,0)) samedaychild
       ,
        sum(decode(vtype, 1,numdu,0)) arrexproomDayUse
       ,sum(decode(vtype, 2,numdu,0)) arrexpadultDayUse
       ,sum(decode(vtype,22,numdu,0)) arrexpchildDayUse
       ,sum(decode(vtype, 3,numdu,0)) arractroomDayUse
       ,sum(decode(vtype, 4,numdu,0)) arractadultDayUse
       ,sum(decode(vtype,44,numdu,0)) arractchildDayUse
       ,sum(decode(vtype, 5,numdu,0)) arrpreroomDayUse
       ,sum(decode(vtype, 6,numdu,0)) arrpreadultDayUse
       ,sum(decode(vtype,66,numdu,0)) arrprechildDayUse
       ,sum(decode(vtype, 7,numdu,0)) depexproomDayUse
       ,sum(decode(vtype, 8,numdu,0)) depexpadultDayUse
       ,sum(decode(vtype,88,numdu,0)) depexpchildDayUse
       ,sum(decode(vtype, 9,numdu,0)) depactroomDayUse
       ,sum(decode(vtype,10,numdu,0)) depactadultDayUse
       ,sum(decode(vtype,1010,numdu,0)) depactchildDayUse
from (
--select wdat_date,   1 vtype,exproom num  ,exproomDayUse numdu   ,ycad_id    from v8_sys_hs_arr_exproom_bed_cad
--union                                    
select wdat_date,   2 vtype,expadult num ,expadultDayUse numdu  ,ycad_id    from v8_sys_hs_arr_expadult_bed_cad 
union                                    
select wdat_date,  22 vtype,expchild num ,expchildDayUse numdu  ,ycad_id    from v8_sys_hs_arr_expchild_bed_cad 
union                                    
--select wdat_date,   3 vtype,actroom num  ,actroomDayUse numdu   ,ycad_id    from v8_sys_hs_arr_actroom_bed_cad 
--union                                    
select wdat_date,   4 vtype,actadult num ,actadultDayUse numdu  ,ycad_id    from v8_sys_hs_arr_actadult_bed_cad
union                                    
select wdat_date,  44 vtype,actchild num ,actchildDayUse numdu  ,ycad_id    from v8_sys_hs_arr_actchild_bed_cad
union                                    
--select wdat_date,   5 vtype,preroom num  ,preroomDayUse numdu   ,ycad_id    from v8_sys_hs_arr_preroom_bed_cad 
--union                                    
select wdat_date,   6 vtype,preadult num ,preadultDayUse numdu  ,ycad_id    from v8_sys_hs_arr_preadult_bed_cad
union                                    
select wdat_date,  66 vtype,prechild num ,prechildDayUse numdu  ,ycad_id    from v8_sys_hs_arr_prechild_bed_cad
union                                    
--select wdat_date,   7 vtype,exproom num  ,exproomDayUse numdu   ,ycad_id    from v8_sys_hs_dep_exproom_bed_cad
--union                                    
select wdat_date,   8 vtype,expadult num ,expadultDayUse numdu  ,ycad_id    from v8_sys_hs_dep_expadult_bed_cad
union                                    
select wdat_date,  88 vtype,expchild num ,expchildDayUse numdu  ,ycad_id    from v8_sys_hs_dep_expchild_bed_cad
union                                    
--select wdat_date,   9 vtype,actroom num  ,actroomDayUse numdu   ,ycad_id    from v8_sys_hs_dep_actroom_bed_cad
--union                                    
select wdat_date,  10 vtype,actadult num ,actadultDayUse numdu  ,ycad_id    from v8_sys_hs_dep_actadult_bed_cad
union 
select wdat_date,1010 vtype,actchild num ,actchildDayUse numdu  ,ycad_id    from v8_sys_hs_dep_actchild_bed_cad
union 
--select wdat_date,  11 vtype,noofrooms num ,0 numdu ,ycad_id    from v8_sys_hs_dayroom_room_bed_cad
--union 
select wdat_date,  12 vtype,noofadults num,0 numdu ,ycad_id    from v8_sys_hs_dayroom_adult_bedcad
union 
select wdat_date,1212 vtype,noofchild num ,0 numdu ,ycad_id    from v8_sys_hs_dayroom_child_bedcad
union 
--select wdat_date,  13 vtype,noofrooms num ,0 numdu ,ycad_id    from v8_sys_hs_walkin_room_bed_cad
--union 
select wdat_date,  14 vtype,noofadults num,0 numdu ,ycad_id    from v8_sys_hs_walkin_adult_bed_cad
union 
select wdat_date,1414 vtype,noofchild num ,0 numdu ,ycad_id    from v8_sys_hs_walkin_child_cad_bed
union 
--select wdat_date,  15 vtype,noofrooms num ,0 numdu ,ycad_id    from v8_sys_hs_sameday_room_bed_cad
--union 
select wdat_date,  16 vtype,noofadults num,0 numdu ,ycad_id    from v8_sys_hs_sameday_adult_bedcad
union 
select wdat_date,1616 vtype,noofchild num ,0 numdu ,ycad_id    from v8_sys_hs_sameday_child_bedcad
union 
select wdat_date,   0 vtype,0 num,0 numdu,0 ycad_id from wdat
)
group by wdat_date,ycad_id;

create or replace view v8_sys_hs_arr_dep_comb_cad_rb as
select
 wdat_date
,ycad_id 
,sum(arrexproom) arrexproom
,sum(arrexpadult) arrexpadult
,sum(arrexpchild) arrexpchild
,sum(arractroom) arractroom
,sum(arractadult) arractadult
,sum(arractchild) arractchild
,sum(arrpreroom) arrpreroom
,sum(arrpreadult) arrpreadult
,sum(arrprechild) arrprechild
,sum(depexproom) depexproom
,sum(depexpadult) depexpadult
,sum(depexpchild) depexpchild
,sum(depactroom) depactroom
,sum(depactadult) depactadult
,sum(depactchild) depactchild
,sum(dayroomroom) dayroomroom
,sum(dayroomadult) dayroomadult
,sum(dayroomchild) dayroomchild
,sum(walkinroom) walkinroom
,sum(walkinadult) walkinadult
,sum(walkinchild) walkinchild
,sum(samedayroom) samedayroom
,sum(samedayadult) samedayadult
,sum(samedaychild) samedaychild
,sum(arrexproomdayuse) arrexproomdayuse
,sum(arrexpadultdayuse) arrexpadultdayuse
,sum(arrexpchilddayuse) arrexpchilddayuse
,sum(arractroomdayuse) arractroomdayuse
,sum(arractadultdayuse) arractadultdayuse
,sum(arractchilddayuse) arractchilddayuse
,sum(arrpreroomdayuse) arrpreroomdayuse
,sum(arrpreadultdayuse) arrpreadultdayuse
,sum(arrprechilddayuse) arrprechilddayuse
,sum(depexproomdayuse) depexproomdayuse
,sum(depexpadultdayuse) depexpadultdayuse
,sum(depexpchilddayuse) depexpchilddayuse
,sum(depactroomdayuse) depactroomdayuse
,sum(depactadultdayuse) depactadultdayuse
,sum(depactchilddayuse) depactchilddayuse
from (
select 
 wdat_date
,ycad_id
,arrexproom
,arrexpadult
,arrexpchild
,arractroom
,arractadult
,arractchild
,arrpreroom
,arrpreadult
,arrprechild
,depexproom
,depexpadult
,depexpchild
,depactroom
,depactadult
,depactchild
,dayroomroom
,dayroomadult
,dayroomchild
,walkinroom
,walkinadult
,walkinchild
,samedayroom
,samedayadult
,samedaychild
,arrexproomdayuse
,arrexpadultdayuse
,arrexpchilddayuse
,arractroomdayuse
,arractadultdayuse
,arractchilddayuse
,arrpreroomdayuse
,arrpreadultdayuse
,arrprechilddayuse
,depexproomdayuse
,depexpadultdayuse
,depexpchilddayuse
,depactroomdayuse
,depactadultdayuse
,depactchilddayuse
from v8_sys_hs_arr_dep_comb_cad
union all
select 
 wdat_date
,ycad_id 
,arrexproom
,arrexpadult
,arrexpchild
,arractroom
,arractadult
,arractchild
,arrpreroom
,arrpreadult
,arrprechild
,depexproom
,depexpadult
,depexpchild
,depactroom
,depactadult
,depactchild
,dayroomroom
,dayroomadult
,dayroomchild
,walkinroom
,walkinadult
,walkinchild
,samedayroom
,samedayadult
,samedaychild
,arrexproomdayuse
,arrexpadultdayuse
,arrexpchilddayuse
,arractroomdayuse
,arractadultdayuse
,arractchilddayuse
,arrpreroomdayuse
,arrpreadultdayuse
,arrprechilddayuse
,depexproomdayuse
,depexpadultdayuse
,depexpchilddayuse
,depactroomdayuse
,depactadultdayuse
,depactchilddayuse
from v8_sys_hs_arr_dep_comb_cad_bed
) group by wdat_date,ycad_id;

create or replace view v8_sys_hs_arr_dep_comb_small as
select wdat_date
       ,sum(decode(vtype,1,num,0)) arrexproom
       ,sum(decode(vtype,2,num,0)) arrexpadult
       ,sum(decode(vtype,3,num,0)) depexproom
       ,sum(decode(vtype,4,num,0)) depexpadult
       ,sum(decode(vtype,1,numdu,0)) arrexproomdayuse
       ,sum(decode(vtype,2,numdu,0)) arrexpadultdayuse
       ,sum(decode(vtype,3,numdu,0)) depexproomdayuse
       ,sum(decode(vtype,4,numdu,0)) depexpadultdayuse
from (
select wdat_date,1 vtype,allexproom num,allexproomdayuse numdu    from v8_sys_hs_arr_allroom 
union
select wdat_date,2 vtype,allexpadult num,allexpadultdayuse numdu    from v8_sys_hs_arr_alladult 
union
select wdat_date,3 vtype,alldeproom num,alldeproomdayuse numdu    from v8_sys_hs_dep_allroom
union 
select wdat_date,4 vtype,alldepadult num,alldepadultdayuse numdu    from v8_sys_hs_dep_alladult
union 
select wdat_date,0 vtype,0,0 num from wdat
)
group by wdat_date;

create or replace view v8_sys_hs_arr_dep_comb_sma_cad as
select wdat_date
       ,ycad_id
       ,sum(decode(vtype,1,num,0)) arrexproom
       ,sum(decode(vtype,2,num,0)) arrexpadult
       ,sum(decode(vtype,3,num,0)) depexproom
       ,sum(decode(vtype,4,num,0)) depexpadult
       ,sum(decode(vtype,1,numdu,0)) arrexproomdayuse
       ,sum(decode(vtype,2,numdu,0)) arrexpadultdayuse
       ,sum(decode(vtype,3,numdu,0)) depexproomdayuse
       ,sum(decode(vtype,4,numdu,0)) depexpadultdayuse
from (
select wdat_date,1 vtype,ycad_id,allexproom num,allexproomdayuse numdu from v8_sys_hs_arr_allroom_cad 
union
select wdat_date,2 vtype,ycad_id,allexpadult num,allexpadultdayuse numdu from v8_sys_hs_arr_alladult_cad 
union
select wdat_date,3 vtype,ycad_id,alldeproom num,alldeproomdayuse numdu from v8_sys_hs_dep_allroom_cad
union 
select wdat_date,4 vtype,ycad_id,alldepadult num,alldepadultdayuse numdu from v8_sys_hs_dep_alladult_cad
union 
select wdat_date,0 vtype,ycad_id,0 num,0 numdu from wdat,ycad
)
group by wdat_date,ycad_id;

create or replace view v8_sys_hs_arr_dep_comb_sma_prt as
select wdat_date
       ,ycat_id
       ,sum(decode(vtype,1,num,0)) arrexproom
       ,sum(decode(vtype,2,num,0)) arrexpadult
       ,sum(decode(vtype,3,num,0)) depexproom
       ,sum(decode(vtype,4,num,0)) depexpadult
       ,sum(decode(vtype,1,numdu,0)) arrexproomdayuse
       ,sum(decode(vtype,2,numdu,0)) arrexpadultdayuse
       ,sum(decode(vtype,3,numdu,0)) depexproomdayuse
       ,sum(decode(vtype,4,numdu,0)) depexpadultdayuse
from (
select wdat_date,1 vtype,ydet_ycat_id ycat_id,allexproom num,allexproomdayuse numdu from v8_sys_hs_arr_allroom_prt 
union
select wdat_date,2 vtype,ydet_ycat_id ycat_id,allexpadult num,allexpadultdayuse numdu from v8_sys_hs_arr_alladult_prt 
union
select wdat_date,3 vtype,ydet_ycat_id ycat_id,alldeproom num,alldeproomdayuse numdu from v8_sys_hs_dep_allroom_prt
union 
select wdat_date,4 vtype,ydet_ycat_id ycat_id,alldepadult num,alldepadultdayuse numdu from v8_sys_hs_dep_alladult_prt
union 
select wdat_date,0 vtype,ycat_id,0,0 num from wdat,ycat
)
group by wdat_date,ycat_id;

CREATE OR REPLACE FORCE VIEW V8_SYS_AVGRATE
AS 
select
   ydet_date wdat_Date
   ,(sum(rate)/sum(ROOMS)) as AVGRATE
   ,(sum(ratenet)/sum(ROOMS)) as AVGRATEnet
from
  (
   select 
       ydet_date
	  ,sum( getYDetDiscountedAmount(ydet_rateamount,ydet_ratemanual,ydet_ratediff,ydet_ratediscountperc,ydet_nodiscountpackage,ydet_finalAmount)
           *YDET_NOOFROOMS
           ) rate
	  ,sum(ydet_netamount*YDET_NOOFROOMS) ratenet 
      ,avg(YDET_NOOFROOMS) ROOMS
   from ydet,yres,ycat
   where nvl(ydet_sharenum,0)<>0
   and ydet_yres_id=yres_id
   and yres_resstatus<>3
   and ycat_id=ydet_ycat_id
   and ycat_notinavl=0
   group by ydet_date,ydet_sharenum
   union all
   select 
       ydet_date
	  , getYDetDiscountedAmount(ydet_rateamount,ydet_ratemanual,ydet_ratediff,ydet_ratediscountperc,ydet_nodiscountpackage,ydet_finalAmount) 
       *YDET_NOOFROOMS
       rate
	  ,ydet_netamount*ydet_noofrooms ratenet
      , (YDET_NOOFROOMS) ROOMS
   from ydet,yres,ycat
   where nvl(ydet_sharenum,0)= 0
   and ydet_yres_id=yres_id
   and yres_resstatus<>3
   and ycat_id=ydet_ycat_id
   and ycat_notinavl=0
   ) a
group by ydet_date;

create or replace force view v8_sys_avgrate_person
as 
select
    ydet_date wdat_Date
   ,sum(decode(yres_bed,0,rate,0))/greatest(1,sum(decode(yres_bed,0,persons,0))) avgRate
   ,sum(decode(yres_bed,0,rateNet,0))/greatest(1,sum(decode(yres_bed,0,persons,0))) avgRateNet
   ,sum(decode(yres_bed,1,rate,0))/greatest(1,sum(decode(yres_bed,1,persons,0))) avgRateBed
   ,sum(decode(yres_bed,1,rateNet,0))/greatest(1,sum(decode(yres_bed,1,persons,0))) avgRateNetBed
   ,sum(rate)/sum(persons) avgRateBoth
   ,sum(rateNet)/sum(persons) avgRateNetBoth
from
  (select 
       ydet_date
	  , getYDetDiscountedAmount(ydet_rateamount,ydet_ratemanual,ydet_ratediff,ydet_ratediscountperc,ydet_nodiscountpackage,ydet_finalAmount) 
       *YDET_NOOFROOMS
       rate
	  ,ydet_netamount*ydet_noofrooms ratenet
      ,ydet_noofrooms rooms
      ,ydet_noofrooms*ydet_noofperson persons
      ,yres_bed 
   from ydet,yres,ycat
   where ydet_yres_id=yres_id
   and yres_resstatus<>3
   and ycat_id=ydet_any_ycat_id
   and ycat_notinavl=0
  ) 
group by ydet_date;

CREATE OR REPLACE FORCE VIEW V8_SYS_AVGRATE_XCMA
AS 
select
    ydet_date wdat_Date
   ,ydet_xcma_id xcma_id
   ,(sum(rate)/sum(ROOMS)) as AVGRATE
   ,(sum(ratenet)/sum(ROOMS)) as AVGRATEnet
from
  (
   select 
       ydet_date
      ,ydet_xcma_id 
	  ,sum( getYDetDiscountedAmount(ydet_rateamount,ydet_ratemanual,ydet_ratediff,ydet_ratediscountperc,ydet_nodiscountpackage,ydet_finalAmount)
           *YDET_NOOFROOMS
          ) rate
	  ,sum(ydet_netamount*YDET_NOOFROOMS) ratenet 
      ,avg(YDET_NOOFROOMS) ROOMS
   from ydet,yres,ycat
   where nvl(ydet_sharenum,0)<>0
   and ydet_yres_id=yres_id
   and yres_resstatus<>3
   and ycat_id=ydet_ycat_id
   and ycat_notinavl=0
   group by ydet_date,ydet_sharenum,ydet_xcma_id
   union all
   select 
       ydet_date
      ,ydet_xcma_id 
	  , getYDetDiscountedAmount(ydet_rateamount,ydet_ratemanual,ydet_ratediff,ydet_ratediscountperc,ydet_nodiscountpackage,ydet_finalAmount) 
       *YDET_NOOFROOMS
       rate
	  ,ydet_netamount*ydet_noofrooms ratenet
      , (YDET_NOOFROOMS) ROOMS
   from ydet,yres,ycat
   where nvl(ydet_sharenum,0)= 0
   and ydet_yres_id=yres_id
   and yres_resstatus<>3
   and ycat_id=ydet_ycat_id
   and ycat_notinavl=0
   ) a
group by ydet_date,ydet_xcma_id;

CREATE OR REPLACE FORCE VIEW V8_SYS_AVGRATE_PRTRC --per room type and rate code
AS 
select
    ydet_date wdat_Date
   ,ydet_ycat_id ycat_id
   ,ydet_yrch_id yrch_id
   ,(sum(rate)/sum(ROOMS)) as AVGRATE
   ,(sum(ratenet)/sum(ROOMS)) as AVGRATEnet
from
  (
   select 
       ydet_date
      ,ydet_ycat_id
      ,ydet_yrch_id
	  ,sum( getYDetDiscountedAmount(ydet_rateamount,ydet_ratemanual,ydet_ratediff,ydet_ratediscountperc,ydet_nodiscountpackage,ydet_finalAmount) 
           *YDET_NOOFROOMS
          ) rate
	  ,sum(ydet_netamount*YDET_NOOFROOMS) ratenet 
      ,avg(YDET_NOOFROOMS) ROOMS
   from ydet,yres,ycat
   where nvl(ydet_sharenum,0)<>0
   and ydet_yres_id=yres_id
   and yres_resstatus<>3
   and ycat_id=ydet_ycat_id
   and ycat_notinavl=0
   group by ydet_date,ydet_sharenum,ydet_ycat_id,ydet_yrch_id
   union all
   select 
       ydet_date
      ,ydet_ycat_id
      ,ydet_yrch_id 
	  , getYDetDiscountedAmount(ydet_rateamount,ydet_ratemanual,ydet_ratediff,ydet_ratediscountperc,ydet_nodiscountpackage,ydet_finalAmount)
       *YDET_NOOFROOMS rate
	  ,ydet_netamount*ydet_noofrooms ratenet
      , (YDET_NOOFROOMS) ROOMS
   from ydet,yres,ycat
   where nvl(ydet_sharenum,0)= 0
   and ydet_yres_id=yres_id
   and yres_resstatus<>3
   and ycat_id=ydet_ycat_id
   and ycat_notinavl=0
   ) a
group by ydet_date,ydet_ycat_id,ydet_yrch_id;

CREATE OR REPLACE FORCE VIEW V8_SYS_AVGRATE_source
AS 
select
    ydet_date day
   ,yral_yras_id source_id
   ,(sum(rate)/sum(ROOMS)) as AVGRATE
   ,(sum(ratenet)/sum(ROOMS)) as AVGRATEnet
from
  (
   select 
       ydet_date
      ,yral_yras_id 
	  ,sum( getYDetDiscountedAmount(ydet_rateamount,ydet_ratemanual,ydet_ratediff,ydet_ratediscountperc,ydet_nodiscountpackage,ydet_finalAmount) 
           *YDET_NOOFROOMS
          ) rate
	  ,sum(ydet_netamount*YDET_NOOFROOMS) ratenet 
      ,avg(YDET_NOOFROOMS) ROOMS
   from ydet,yres,ycat,yral
   where nvl(ydet_sharenum,0)<>0
   and ydet_yres_id=yres_id
   and yres_resstatus<>3
   and ycat_id=ydet_ycat_id
   and ycat_notinavl=0
   and yral_ydet_id=ydet_id
   and yral_yrac_id=1
   group by ydet_date,ydet_sharenum,yral_yras_id
   union all
   select 
       ydet_date
      ,yral_yras_id 
	  , getYDetDiscountedAmount(ydet_rateamount,ydet_ratemanual,ydet_ratediff,ydet_ratediscountperc,ydet_nodiscountpackage,ydet_finalAmount) 
       *YDET_NOOFROOMS rate
	  ,ydet_netamount*ydet_noofrooms ratenet
      , (YDET_NOOFROOMS) ROOMS
   from ydet,yres,ycat,yral
   where nvl(ydet_sharenum,0)= 0
   and ydet_yres_id=yres_id
   and yres_resstatus<>3
   and ycat_id=ydet_ycat_id
   and ycat_notinavl=0
   and yral_ydet_id=ydet_id
   and yral_yrac_id=1
   ) a
group by ydet_date,yral_yras_id;

CREATE OR REPLACE FORCE VIEW V8_SYS_AVGRATE_cad
AS 
select
   ydet_date wdat_Date
   ,ycad_id
   ,sum(rate) sumrate
   ,sum(ratenet) sumratenet
   ,sum(ROOMS) sumroom
   ,(sum(rate)/sum(ROOMS)) as AVGRATE  --for backward compatibility with existing views
   ,(sum(ratenet)/sum(ROOMS)) as AVGRATEnet --for backward compatibility with existing views
from
  (
   select 
       ydet_date
      ,YCAT_SUM_ROOMTYPE_YCAD_ID ycad_id 
	  ,sum( getYDetDiscountedAmount(ydet_rateamount,ydet_ratemanual,ydet_ratediff,ydet_ratediscountperc,ydet_nodiscountpackage,ydet_finalAmount) 
           *YDET_NOOFROOMS
          ) rate
	  ,sum(ydet_netamount*YDET_NOOFROOMS) ratenet 
      ,avg(YDET_NOOFROOMS) ROOMS
   from ydet,yres,ycat
   where nvl(ydet_sharenum,0)<>0
   and ydet_yres_id=yres_id
   and yres_resstatus<>3
   and ycat_id=ydet_ycat_id
   group by ydet_date,ydet_sharenum,YCAT_SUM_ROOMTYPE_YCAD_ID
   union all
   select 
       ydet_date
      ,YCAT_SUM_ROOMTYPE_YCAD_ID ycad_id 
	  , getYDetDiscountedAmount(ydet_rateamount,ydet_ratemanual,ydet_ratediff,ydet_ratediscountperc,ydet_nodiscountpackage,ydet_finalAmount) 
       *YDET_NOOFROOMS 
       rate
	  ,ydet_netamount*ydet_noofrooms ratenet
      , (YDET_NOOFROOMS) ROOMS
   from ydet,yres,ycat
   where nvl(ydet_sharenum,0)= 0
   and ydet_yres_id=yres_id
   and yres_resstatus<>3
   and ycat_id=ydet_ycat_id
   ) a
group by ydet_date,ycad_id;

create or replace force view v8_sys_avgrate_cad_person
as 
select
    ydet_date wdat_Date
   ,ycad_id
   ,sum(decode(yres_bed,0,rate,0))/greatest(1,sum(decode(yres_bed,0,persons,0))) avgRate
   ,sum(decode(yres_bed,0,rateNet,0))/greatest(1,sum(decode(yres_bed,0,persons,0))) avgRateNet
   ,sum(decode(yres_bed,1,rate,0))/greatest(1,sum(decode(yres_bed,1,persons,0))) avgRateBed
   ,sum(decode(yres_bed,1,rateNet,0))/greatest(1,sum(decode(yres_bed,1,persons,0))) avgRateNetBed
   ,sum(rate)/sum(persons) avgRateBoth
   ,sum(rateNet)/sum(persons) avgRateNetBoth
from
  (select 
       ydet_date
      ,ycat_sum_roomtype_ycad_id ycad_id 
	  , getYDetDiscountedAmount(ydet_rateamount,ydet_ratemanual,ydet_ratediff,ydet_ratediscountperc,ydet_nodiscountpackage,ydet_finalAmount) 
       *YDET_NOOFROOMS
       rate
	  ,ydet_netamount*ydet_noofrooms ratenet
      ,ydet_noofrooms rooms
      ,ydet_noofrooms*ydet_noofperson persons
      ,yres_bed 
   from ydet,yres,ycat
   where ydet_yres_id=yres_id
   and yres_resstatus<>3
   and ycat_id=ydet_any_ycat_id
   and ycat_notinavl=0
  ) 
group by ydet_date,ycad_id;

CREATE OR REPLACE FORCE VIEW V8_SYS_AVGrevenue
AS 
select
   ydet_date wdat_Date
   ,(sum(gross)/sum(ROOMS)) as gross
   ,(sum(net)/sum(ROOMS)) as net
from
  (
   select 
       ydet_date
	  ,sum(ypos_unitprice*ypos_quantity) gross
      ,sum(decode(ypos_dt,1,ypos_unitprice*ypos_quantity)) net 
      ,avg(YDET_NOOFROOMS) ROOMS
   from ydet,yres,ypos,ycat
   where nvl(ydet_sharenum,0)<>0
   and ydet_yres_id=yres_id
   and yres_resstatus<>3
   and ypos_yres_id=yres_id
   and ypos_date=ydet_date
   and ypos_type=0
   and ycat_id=ydet_ycat_id
   and ycat_notinavl=0
   group by ydet_date,ydet_sharenum
   union all
   select
       ydet_date 
	  ,sum(ypos_unitprice*ypos_quantity) gross
      ,sum(decode(ypos_dt,1,ypos_unitprice*ypos_quantity)) net 
      ,YDET_NOOFROOMS ROOMS
   from ydet,yres,ypos,ycat
   where nvl(ydet_sharenum,0)= 0
   and ydet_yres_id=yres_id
   and yres_resstatus<>3
   and ypos_yres_id=yres_id
   and ypos_date=ydet_date
   and ypos_type=0
   and ycat_id=ydet_ycat_id
   and ycat_notinavl=0
   group by ydet_id,ydet_Date,ydet_noofrooms
   ) a
group by ydet_date;

create or replace force view v8_sys_avgrevenue_person
as 
select
    ydet_date wdat_Date
   ,sum(decode(yres_bed,0,gross,0))/greatest(1,sum(decode(yres_bed,0,persons,0))) gross
   ,sum(decode(yres_bed,0,net,0))/greatest(1,sum(decode(yres_bed,0,persons,0))) net
   ,sum(decode(yres_bed,1,gross,0))/greatest(1,sum(decode(yres_bed,1,persons,0))) grossBed
   ,sum(decode(yres_bed,1,net,0))/greatest(1,sum(decode(yres_bed,1,persons,0))) netBed
   ,sum(gross)/sum(persons) grossBoth
   ,sum(net)/sum(persons) netBoth
from
  (
  select
       ydet_date
      ,yres_id 
	  ,sum(ypos_unitprice*ypos_quantity) gross
      ,sum(decode(ypos_dt,1,ypos_unitprice*ypos_quantity)) net 
      ,avg(ydet_noofrooms) rooms
      ,avg(ydet_noofrooms*ydet_noofperson) persons
      ,min(yres_bed) yres_bed 
   from ydet,yres,ypos,ycat
   where ydet_yres_id=yres_id
   and yres_resstatus<>3
   and ypos_yres_id=yres_id
   and ypos_date=ydet_date
   and ypos_type=0
   and ycat_id=ydet_any_ycat_id
   and ycat_notinavl=0
   group by ydet_date,yres_id
   )
group by ydet_date;

CREATE OR REPLACE FORCE VIEW V8_SYS_AVGrevenue_cad
AS 
select
   ydet_date wdat_Date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(gross) grosssum
   ,sum(net) netsum
   ,sum(ROOMS) roomsum 
from
  (
   select 
       ydet_date
      ,ycat_sum_roomtype_ycad_id 
	  ,sum(ypos_unitprice*ypos_quantity) gross
      ,sum(decode(ypos_dt,1,ypos_unitprice*ypos_quantity)) net 
      ,avg(YDET_NOOFROOMS) ROOMS
   from ydet,yres,ypos,ycat
   where nvl(ydet_sharenum,0)<>0
   and ydet_yres_id=yres_id
   and yres_resstatus<>3
   and ypos_yres_id=yres_id
   and ypos_date=ydet_date
   and ypos_type=0
   and ycat_id=ydet_ycat_id
   and ycat_notinavl=0
   group by ydet_date,ydet_sharenum,ycat_sum_roomtype_ycad_id
   union all
   select
       ydet_date
      ,ycat_sum_roomtype_ycad_id  
	  ,sum(ypos_unitprice*ypos_quantity) gross
      ,sum(decode(ypos_dt,1,ypos_unitprice*ypos_quantity)) net 
      ,YDET_NOOFROOMS ROOMS
   from ydet,yres,ypos,ycat
   where nvl(ydet_sharenum,0)= 0
   and ydet_yres_id=yres_id
   and yres_resstatus<>3
   and ypos_yres_id=yres_id
   and ypos_date=ydet_date
   and ypos_type=0
   and ycat_id=ydet_ycat_id
   and ycat_notinavl=0
   group by ydet_id,ydet_Date,ydet_noofrooms,ycat_sum_roomtype_ycad_id
   ) a
group by ydet_date,ycat_sum_roomtype_ycad_id;

create or replace force view v8_sys_avgrevenue_cad_person
as 
select
    ydet_date wdat_Date
   ,ycad_id 
   ,sum(decode(yres_bed,0,gross,0))/greatest(1,sum(decode(yres_bed,0,persons,0))) gross
   ,sum(decode(yres_bed,0,net,0))/greatest(1,sum(decode(yres_bed,0,persons,0))) net
   ,sum(decode(yres_bed,1,gross,0))/greatest(1,sum(decode(yres_bed,1,persons,0))) grossBed
   ,sum(decode(yres_bed,1,net,0))/greatest(1,sum(decode(yres_bed,1,persons,0))) netBed
   ,sum(gross)/sum(persons) grossBoth
   ,sum(net)/sum(persons) netBoth
from
  (
  select
       ydet_date
      ,yres_id 
      ,ycat_sum_roomtype_ycad_id ycad_id
	  ,sum(ypos_unitprice*ypos_quantity) gross
      ,sum(decode(ypos_dt,1,ypos_unitprice*ypos_quantity)) net 
      ,avg(ydet_noofrooms) rooms
      ,avg(ydet_noofrooms*ydet_noofperson) persons
      ,min(yres_bed) yres_bed 
   from ydet,yres,ypos,ycat
   where ydet_yres_id=yres_id
   and yres_resstatus<>3
   and ypos_yres_id=yres_id
   and ypos_date=ydet_date
   and ypos_type=0
   and ycat_id=ydet_any_ycat_id
   and ycat_notinavl=0
   group by ydet_date,yres_id,ycat_sum_roomtype_ycad_id
   )
group by ydet_date,ycad_id;

CREATE OR REPLACE FORCE VIEW V8_SYS_AVGrevenue_source
AS 
select
   ydet_date day
   ,yral_yras_id source_id
   ,sum(gross) grosssum
   ,sum(net) netsum
   ,sum(ROOMS) roomsum 
from
  (
   select 
       ydet_date
      ,yral_yras_id 
	  ,sum(ypos_unitprice*ypos_quantity) gross
      ,sum(decode(ypos_dt,1,ypos_unitprice*ypos_quantity)) net 
      ,avg(YDET_NOOFROOMS) ROOMS
   from ydet,yres,ypos,yral
   where nvl(ydet_sharenum,0)<>0
   and ydet_yres_id=yres_id
   and yres_resstatus<>3
   and ypos_yres_id=yres_id
   and ypos_date=ydet_date
   and ypos_type=0
   and yral_ydet_id=ydet_id
   and yral_yrac_id=1
   group by ydet_date,ydet_sharenum,yral_yras_id
   union all
   select
       ydet_date
      ,yral_yras_id  
	  ,sum(ypos_unitprice*ypos_quantity) gross
      ,sum(decode(ypos_dt,1,ypos_unitprice*ypos_quantity)) net 
      ,YDET_NOOFROOMS ROOMS
   from ydet,yres,ypos,yral
   where nvl(ydet_sharenum,0)= 0
   and ydet_yres_id=yres_id
   and yres_resstatus<>3
   and ypos_yres_id=yres_id
   and ypos_date=ydet_date
   and ypos_type=0
   and yral_ydet_id=ydet_id
   and yral_yrac_id=1
   group by ydet_id,ydet_Date,ydet_noofrooms,yral_yras_id
   ) a
group by ydet_date,yral_yras_id;

create or replace view v8_sys_noofallotments as
select
   wdat_date
   ,sum(decode(sign(ybld_cutoffdate-d.WGBS_DATEVALUE-1),-1,0,nvl(ybld_actualnoofrooms,0))) noofrooms
from wdat,ybld,yblh,ybst,fideliodate d
where
    wdat_date=ybld_date
and ybld_yblh_id=yblh_id
and yblh_ybst_id=ybst_id
and ybst_resstatus<>3
group by wdat_date;

create or replace view v8_sys_noofrescategories as
select  
    wdat_date
   ,sum(pickupnoofrooms) pickupnoofrooms
   ,sum(groupnoofrooms) groupnoofrooms
   ,sum(groupmasternoofrooms) groupmasternoofrooms
   ,sum(blockngrouprooms) blockngrouprooms
   ,sum(blockngroupmasterrooms) blockngroupmasterrooms
   ,sum(totalnoofrooms) totalnoofrooms
   ,sum(pickupnoofadults) pickupnoofadults
   ,sum(groupnoofadults) groupnoofadults
   ,sum(groupmasternoofadults) groupmasternoofadults
   ,sum(blockngroupnoofadults) blockngroupnoofadults
   ,sum(blockngroupmasternoofadults) blockngroupmasternoofadults
   ,sum(totalnoofadults) totalnoofadults  
   ,sum(pickupnoofchild) pickupnoofchild
   ,sum(groupnoofchild) groupnoofchild
   ,sum(groupmasternoofchild) groupmasternoofchild
   ,sum(blockngroupnoofchild) blockngroupnoofchild
   ,sum(blockngroupmasternoofchild) blockngroupmasternoofchild
   ,sum(totalnoofchild) totalnoofchild  
 from (
select
   wdat_date
   ,decode(nvl(ydet_sharenum,0),0
       ,sum(decode(nvl(ydet_ybld_id,0),0,0,decode(nvl(yres_ygrp_id,0),0,decode(nvl(ydet_ygrp_id,0),0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)))))
       ,    least(1,max(nvl(ydet_ybld_id,0)))   --block
        *(1-least(1,max(nvl(yres_ygrp_id,0))))  --no group master
        *(1-least(1,max(nvl(ydet_ygrp_id,0))))  --no group member
       *avg(decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0))
      ) pickupnoofrooms
   ,decode(nvl(ydet_sharenum,0),0
       ,sum(decode(nvl(ydet_ygrp_id,0),0,0,decode(nvl(ydet_ybld_id,0),0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)))) 
       , (1-least(1,max(nvl(ydet_ybld_id,0))))  --no block
        *(1-least(1,max(nvl(yres_ygrp_id,0))))  --no group master
        *   least(1,max(nvl(ydet_ygrp_id,0)))   --group member
       *avg(decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0))
      ) groupnoofrooms
   ,decode(nvl(ydet_sharenum,0),0
       ,sum(decode(nvl(yres_ygrp_id,0),0,0,decode(nvl(ydet_ygrp_id,0),0,decode(nvl(ydet_ybld_id,0),0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0))))) 
       , (1-least(1,max(nvl(ydet_ybld_id,0))))  --no block
        *   least(1,max(nvl(yres_ygrp_id,0)))   --group master
        *(1-least(1,max(nvl(ydet_ygrp_id,0))))  --no group member
       *avg(decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0))
      ) groupmasternoofrooms
   ,decode(nvl(ydet_sharenum,0),0
       ,sum(decode(nvl(ydet_ybld_id,0),0,0,decode(nvl(yres_ygrp_id,0),0,decode(nvl(ydet_ygrp_id,0),0,0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)))))
       ,    least(1,max(nvl(ydet_ybld_id,0)))   --block
        *(1-least(1,max(nvl(yres_ygrp_id,0))))  --group master
        *   least(1,max(nvl(ydet_ygrp_id,0)))   --no group member
       *avg(decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0))
      ) blockngrouprooms
   ,decode(nvl(ydet_sharenum,0),0
       ,sum(decode(nvl(ydet_ybld_id,0),0,0,decode(nvl(yres_ygrp_id,0),0,0,decode(nvl(ydet_ygrp_id,0),0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)))))
       ,    least(1,max(nvl(ydet_ybld_id,0)))   --block
        *   least(1,max(nvl(yres_ygrp_id,0)))   --group master
        *(1-least(1,max(nvl(ydet_ygrp_id,0))))  --no group member
       *avg(decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0))
      ) blockngroupmasterrooms
   ,decode(nvl(ydet_sharenum,0),0
       ,sum(decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0))
       ,avg(decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0))
      )  totalnoofrooms
   ,sum(decode(nvl(ydet_ybld_id,0),0,0,decode(nvl(yres_ygrp_id,0),0,decode(nvl(ydet_ygrp_id,0),0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*ydet_adultno)))) pickupnoofadults
   ,sum(decode(nvl(ydet_ygrp_id,0),0,0,decode(nvl(ydet_ybld_id,0),0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*ydet_adultno))) groupnoofadults
   ,sum(decode(nvl(yres_ygrp_id,0),0,0,decode(nvl(ydet_ygrp_id,0),0,decode(nvl(ydet_ybld_id,0),0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*ydet_adultno)))) groupmasternoofadults
   ,sum(decode(nvl(ydet_ygrp_id,0),0,0,decode(nvl(ydet_ybld_id,0),0,0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*ydet_adultno))) blockngroupnoofadults
   ,sum(decode(nvl(yres_ygrp_id,0),0,0,decode(nvl(ydet_ygrp_id,0),0,decode(nvl(ydet_ybld_id,0),0,0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*ydet_adultno)))) blockngroupmasternoofadults
   ,sum(decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*ydet_adultno)  totalnoofadults
   ,sum(decode(nvl(ydet_ybld_id,0),0,0,decode(nvl(yres_ygrp_id,0),0,decode(nvl(ydet_ygrp_id,0),0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*(select sum(yrgl_number) from yrgl where yrgl_ydet_id=ydet_id))))) pickupnoofchild
   ,sum(decode(nvl(ydet_ygrp_id,0),0,0,decode(nvl(ydet_ybld_id,0),0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*(select sum(yrgl_number) from yrgl where yrgl_ydet_id=ydet_id)))) groupnoofchild
   ,sum(decode(nvl(yres_ygrp_id,0),0,0,decode(nvl(ydet_ygrp_id,0),0,decode(nvl(ydet_ybld_id,0),0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*(select sum(yrgl_number) from yrgl where yrgl_ydet_id=ydet_id))))) groupmasternoofchild
   ,sum(decode(nvl(ydet_ygrp_id,0),0,0,decode(nvl(ydet_ybld_id,0),0,0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*(select sum(yrgl_number) from yrgl where yrgl_ydet_id=ydet_id)))) blockngroupnoofchild
   ,sum(decode(nvl(yres_ygrp_id,0),0,0,decode(nvl(ydet_ygrp_id,0),0,decode(nvl(ydet_ybld_id,0),0,0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*(select sum(yrgl_number) from yrgl where yrgl_ydet_id=ydet_id))))) blockngroupmasternoofchild
   ,sum(decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*(select sum(yrgl_number) from yrgl where yrgl_ydet_id=ydet_id))  totalnoofchild
from wdat,yres,ydet,ycat
where
    wdat_date=ydet_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_exparrtime<>yres_expdeptime
and ycat_id(+)=ydet_ycat_id
and yres_bed=0
group by wdat_date,ydet_sharenum
) group by wdat_date;

create or replace view v8_sys_noofrescategories_bed as
select
   wdat_date
   ,sum(case when     ydet_ybld_id>0
                  and nvl(yres_ygrp_id,0)=0
                  and nvl(ydet_ygrp_id,0)=0
                  and nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 1 else 0 end 
             else 0 end
       ) pickupNoofAdults
   ,sum(case when     nvl(ydet_ybld_id,0)=0
                  and ydet_ygrp_id>0
                  and nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 1 else 0 end
             else 0 end
       ) groupNoofAdults
   ,sum(case when     nvl(ydet_ybld_id,0)=0
                  and yres_ygrp_id>0
                  and nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 1 else 0 end
             else 0 end
       ) groupMasterNoofAdults 
   ,sum(case when     ydet_ybld_id>0
                  and nvl(yres_ygrp_id,0)=0
                  and ydet_ygrp_id>0
                  and nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 1 else 0 end
             else 0 end
       ) blocknGroupNoofAdults
   ,sum(case when     ydet_ybld_id>0
                  and yres_ygrp_id>0
                  and nvl(ydet_ygrp_id,0)=0
                  and nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 1 else 0 end
             else 0 end
       ) blocknGroupMasterNoofAdults        
   ,sum(case when nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 1 else 0 end
             else 0 end
       )  totalNoofAdults
   ,sum(case when     ydet_ybld_id>0
                  and nvl(yres_ygrp_id,0)=0
                  and nvl(ydet_ygrp_id,0)=0
                  and nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 0 else 1 end
             else 0 end
       ) pickupNoofChild
   ,sum(case when     nvl(ydet_ybld_id,0)=0
                  and ydet_ygrp_id>0
                  and nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 0 else 1 end
             else 0 end
       ) groupNoofChild
   ,sum(case when     nvl(ydet_ybld_id,0)=0
                  and yres_ygrp_id>0
                  and nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 0 else 1 end
             else 0 end
       ) groupMasterNoofChild 
   ,sum(case when     ydet_ybld_id>0
                  and nvl(yres_ygrp_id,0)=0
                  and ydet_ygrp_id>0
                  and nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 0 else 1 end
             else 0 end
       ) blocknGroupNoofChild
   ,sum(case when     ydet_ybld_id>0
                  and yres_ygrp_id>0
                  and nvl(ydet_ygrp_id,0)=0
                  and nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 0 else 1 end
             else 0 end
       ) blocknGroupMasterNoofChild        
   ,sum(case when nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 0 else 1 end
             else 0 end
       )  totalNoofChild
from wdat,yres,ydet,ycat,yrpe
where
    wdat_date=ydet_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_exparrtime<>yres_expdeptime
and ycat_id=ydet_bed_ycat_id
and yres_bed=1
and yrpe_yres_id=yres_id
and yrpe_noshow=0
and (yrpe_ActDepTime is null or trunc(yrpe_ActDepTime)>ydet_date)
group by wdat_date;

create or replace view v8_sys_noofrescategories_rb as
select  
    wdat_date
   ,sum(pickupnoofrooms) pickupnoofrooms
   ,sum(groupnoofrooms) groupnoofrooms
   ,sum(groupmasternoofrooms) groupmasternoofrooms
   ,sum(blockngrouprooms) blockngrouprooms
   ,sum(blockngroupmasterrooms) blockngroupmasterrooms
   ,sum(totalnoofrooms) totalnoofrooms
   ,sum(pickupnoofadults) pickupnoofadults
   ,sum(groupnoofadults) groupnoofadults
   ,sum(groupmasternoofadults) groupmasternoofadults
   ,sum(blockngroupnoofadults) blockngroupnoofadults
   ,sum(blockngroupmasternoofadults) blockngroupmasternoofadults
   ,sum(totalnoofadults) totalnoofadults  
   ,sum(pickupnoofchild) pickupnoofchild
   ,sum(groupnoofchild) groupnoofchild
   ,sum(groupmasternoofchild) groupmasternoofchild
   ,sum(blockngroupnoofchild) blockngroupnoofchild
   ,sum(blockngroupmasternoofchild) blockngroupmasternoofchild
   ,sum(totalnoofchild) totalnoofchild  
 from (
select
  wdat_date
 ,pickupnoofrooms
 ,groupnoofrooms
 ,groupmasternoofrooms
 ,blockngrouprooms
 ,blockngroupmasterrooms
 ,totalnoofrooms
 ,pickupnoofadults
 ,groupnoofadults
 ,groupmasternoofadults
 ,blockngroupnoofadults
 ,blockngroupmasternoofadults
 ,totalnoofadults
 ,pickupnoofchild
 ,groupnoofchild
 ,groupmasternoofchild
 ,blockngroupnoofchild
 ,blockngroupmasternoofchild
 ,totalnoofchild
from v8_sys_noofrescategories
union all
select
  wdat_date
 ,0 pickupnoofrooms
 ,0 groupnoofrooms
 ,0 groupmasternoofrooms
 ,0 blockngrouprooms
 ,0 blockngroupmasterrooms
 ,0 totalnoofrooms
 ,pickupnoofadults
 ,groupnoofadults
 ,groupmasternoofadults
 ,blockngroupnoofadults
 ,blockngroupmasternoofadults
 ,totalnoofadults
 ,pickupnoofchild
 ,groupnoofchild
 ,groupmasternoofchild
 ,blockngroupnoofchild
 ,blockngroupmasternoofchild
 ,totalnoofchild
from v8_sys_noofrescategories_bed
) group by wdat_date;

--i dont want to touch original view to make one base view. dont want to risk speed issues.

create or replace view v8_sys_noofrescategories_cad as
select
    ycat_sum_roomtype_ycad_id ycad_id  
   ,wdat_date
   ,sum(pickupnoofrooms) pickupnoofrooms
   ,sum(groupnoofrooms) groupnoofrooms
   ,sum(groupmasternoofrooms) groupmasternoofrooms
   ,sum(blockngrouprooms) blockngrouprooms
   ,sum(blockngroupmasterrooms) blockngroupmasterrooms
   ,sum(totalnoofrooms) totalnoofrooms
   ,sum(pickupnoofadults) pickupnoofadults
   ,sum(groupnoofadults) groupnoofadults
   ,sum(groupmasternoofadults) groupmasternoofadults
   ,sum(blockngroupnoofadults) blockngroupnoofadults
   ,sum(blockngroupmasternoofadults) blockngroupmasternoofadults
   ,sum(totalnoofadults) totalnoofadults  
   ,sum(pickupnoofchild) pickupnoofchild
   ,sum(groupnoofchild) groupnoofchild
   ,sum(groupmasternoofchild) groupmasternoofchild
   ,sum(blockngroupnoofchild) blockngroupnoofchild
   ,sum(blockngroupmasternoofchild) blockngroupmasternoofchild
   ,sum(totalnoofchild) totalnoofchild  
 from (
select
   ycat_sum_roomtype_ycad_id
   ,wdat_date
   ,decode(nvl(ydet_sharenum,0),0
       ,sum(decode(nvl(ydet_ybld_id,0),0,0,decode(nvl(yres_ygrp_id,0),0,decode(nvl(ydet_ygrp_id,0),0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)))))
       ,    least(1,max(nvl(ydet_ybld_id,0)))   --block
        *(1-least(1,max(nvl(yres_ygrp_id,0))))  --no group master
        *(1-least(1,max(nvl(ydet_ygrp_id,0))))  --no group member
       *avg(decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0))
      ) pickupnoofrooms
   ,decode(nvl(ydet_sharenum,0),0
       ,sum(decode(nvl(ydet_ygrp_id,0),0,0,decode(nvl(ydet_ybld_id,0),0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)))) 
       , (1-least(1,max(nvl(ydet_ybld_id,0))))  --no block
        *(1-least(1,max(nvl(yres_ygrp_id,0))))  --no group master
        *   least(1,max(nvl(ydet_ygrp_id,0)))   --group member
       *avg(decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0))
      ) groupnoofrooms
   ,decode(nvl(ydet_sharenum,0),0
       ,sum(decode(nvl(yres_ygrp_id,0),0,0,decode(nvl(ydet_ygrp_id,0),0,decode(nvl(ydet_ybld_id,0),0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0))))) 
       , (1-least(1,max(nvl(ydet_ybld_id,0))))  --no block
        *   least(1,max(nvl(yres_ygrp_id,0)))   --group master
        *(1-least(1,max(nvl(ydet_ygrp_id,0))))  --no group member
       *avg(decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0))
      ) groupmasternoofrooms
   ,decode(nvl(ydet_sharenum,0),0
       ,sum(decode(nvl(ydet_ybld_id,0),0,0,decode(nvl(yres_ygrp_id,0),0,decode(nvl(ydet_ygrp_id,0),0,0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)))))
       ,    least(1,max(nvl(ydet_ybld_id,0)))   --block
        *(1-least(1,max(nvl(yres_ygrp_id,0))))  --group master
        *   least(1,max(nvl(ydet_ygrp_id,0)))   --no group member
       *avg(decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0))
      ) blockngrouprooms
   ,decode(nvl(ydet_sharenum,0),0
       ,sum(decode(nvl(ydet_ybld_id,0),0,0,decode(nvl(yres_ygrp_id,0),0,0,decode(nvl(ydet_ygrp_id,0),0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)))))
       ,    least(1,max(nvl(ydet_ybld_id,0)))   --block
        *   least(1,max(nvl(yres_ygrp_id,0)))   --group master
        *(1-least(1,max(nvl(ydet_ygrp_id,0))))  --no group member
       *avg(decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0))
      ) blockngroupmasterrooms
   ,decode(nvl(ydet_sharenum,0),0
       ,sum(decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0))
       ,avg(decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0))
      )  totalnoofrooms
   ,sum(decode(nvl(ydet_ybld_id,0),0,0,decode(nvl(yres_ygrp_id,0),0,decode(nvl(ydet_ygrp_id,0),0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*ydet_adultno)))) pickupnoofadults
   ,sum(decode(nvl(ydet_ygrp_id,0),0,0,decode(nvl(ydet_ybld_id,0),0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*ydet_adultno))) groupnoofadults
   ,sum(decode(nvl(yres_ygrp_id,0),0,0,decode(nvl(ydet_ygrp_id,0),0,decode(nvl(ydet_ybld_id,0),0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*ydet_adultno)))) groupmasternoofadults
   ,sum(decode(nvl(ydet_ygrp_id,0),0,0,decode(nvl(ydet_ybld_id,0),0,0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*ydet_adultno))) blockngroupnoofadults
   ,sum(decode(nvl(yres_ygrp_id,0),0,0,decode(nvl(ydet_ygrp_id,0),0,decode(nvl(ydet_ybld_id,0),0,0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*ydet_adultno)))) blockngroupmasternoofadults
   ,sum(decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*ydet_adultno)  totalnoofadults
   ,sum(decode(nvl(ydet_ybld_id,0),0,0,decode(nvl(yres_ygrp_id,0),0,decode(nvl(ydet_ygrp_id,0),0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*(select sum(yrgl_number) from yrgl where yrgl_ydet_id=ydet_id))))) pickupnoofchild
   ,sum(decode(nvl(ydet_ygrp_id,0),0,0,decode(nvl(ydet_ybld_id,0),0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*(select sum(yrgl_number) from yrgl where yrgl_ydet_id=ydet_id)))) groupnoofchild
   ,sum(decode(nvl(yres_ygrp_id,0),0,0,decode(nvl(ydet_ygrp_id,0),0,decode(nvl(ydet_ybld_id,0),0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*(select sum(yrgl_number) from yrgl where yrgl_ydet_id=ydet_id))))) groupmasternoofchild
   ,sum(decode(nvl(ydet_ygrp_id,0),0,0,decode(nvl(ydet_ybld_id,0),0,0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*(select sum(yrgl_number) from yrgl where yrgl_ydet_id=ydet_id)))) blockngroupnoofchild
   ,sum(decode(nvl(yres_ygrp_id,0),0,0,decode(nvl(ydet_ygrp_id,0),0,decode(nvl(ydet_ybld_id,0),0,0,decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*(select sum(yrgl_number) from yrgl where yrgl_ydet_id=ydet_id))))) blockngroupmasternoofchild
   ,sum(decode(nvl(ycat_notinavl,0),0,ydet_noofrooms,0)*(select sum(yrgl_number) from yrgl where yrgl_ydet_id=ydet_id))  totalnoofchild
from wdat,yres,ydet,ycat
where
    wdat_date=ydet_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_exparrtime<>yres_expdeptime
and ycat_id(+)=ydet_ycat_id
and yres_bed=0
group by wdat_date,ycat_sum_roomtype_ycad_id,ydet_sharenum
) group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_noofResCategor_cad_bed as
select
    wdat_date
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,sum(case when     ydet_ybld_id>0
                  and nvl(yres_ygrp_id,0)=0
                  and nvl(ydet_ygrp_id,0)=0
                  and nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 1 else 0 end
             else 0 end
       ) pickupNoofAdults
   ,sum(case when     nvl(ydet_ybld_id,0)=0
                  and ydet_ygrp_id>0
                  and nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 1 else 0 end
             else 0 end
       ) groupNoofAdults
   ,sum(case when     nvl(ydet_ybld_id,0)=0
                  and yres_ygrp_id>0
                  and nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 1 else 0 end
             else 0 end
       ) groupMasterNoofAdults 
   ,sum(case when     ydet_ybld_id>0
                  and nvl(yres_ygrp_id,0)=0
                  and ydet_ygrp_id>0
                  and nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 1 else 0 end
             else 0 end
       ) blocknGroupNoofAdults
   ,sum(case when     ydet_ybld_id>0
                  and yres_ygrp_id>0
                  and nvl(ydet_ygrp_id,0)=0
                  and nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 1 else 0 end
             else 0 end
       ) blocknGroupMasterNoofAdults        
   ,sum(case when nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 1 else 0 end
             else 0 end
       )  totalNoofAdults
   ,sum(case when     ydet_ybld_id>0
                  and nvl(yres_ygrp_id,0)=0
                  and nvl(ydet_ygrp_id,0)=0
                  and nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 0 else 1 end
             else 0 end
       ) pickupNoofChild
   ,sum(case when     nvl(ydet_ybld_id,0)=0
                  and ydet_ygrp_id>0
                  and nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 0 else 1 end
             else 0 end
       ) groupNoofChild
   ,sum(case when     nvl(ydet_ybld_id,0)=0
                  and yres_ygrp_id>0
                  and nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 0 else 1 end
             else 0 end
       ) groupMasterNoofChild 
   ,sum(case when     ydet_ybld_id>0
                  and nvl(yres_ygrp_id,0)=0
                  and ydet_ygrp_id>0
                  and nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 0 else 1 end
             else 0 end
       ) blocknGroupNoofChild
   ,sum(case when     ydet_ybld_id>0
                  and yres_ygrp_id>0
                  and nvl(ydet_ygrp_id,0)=0
                  and nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 0 else 1 end
             else 0 end
       ) blocknGroupMasterNoofChild        
   ,sum(case when nvl(ycat_notinavl,0)=0
             then ydet_noofrooms*case when yrpe_xcac_id=0 then 0 else 1 end
             else 0 end
       )  totalNoofChild
from wdat,yres,ydet,ycat,yrpe
where
    wdat_date=ydet_date
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_exparrtime<>yres_expdeptime
and ycat_id=ydet_bed_ycat_id
and yres_bed=1
and yrpe_yres_id=yres_id
and yrpe_noshow=0
and (yrpe_ActDepTime is null or trunc(yrpe_ActDepTime)>ydet_date)
group by wdat_date,ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_noofResCategor_cad_rb as
select  
    wdat_date
   ,ycad_id 
   ,sum(pickupnoofrooms) pickupnoofrooms
   ,sum(groupnoofrooms) groupnoofrooms
   ,sum(groupmasternoofrooms) groupmasternoofrooms
   ,sum(blockngrouprooms) blockngrouprooms
   ,sum(blockngroupmasterrooms) blockngroupmasterrooms
   ,sum(totalnoofrooms) totalnoofrooms
   ,sum(pickupnoofadults) pickupnoofadults
   ,sum(groupnoofadults) groupnoofadults
   ,sum(groupmasternoofadults) groupmasternoofadults
   ,sum(blockngroupnoofadults) blockngroupnoofadults
   ,sum(blockngroupmasternoofadults) blockngroupmasternoofadults
   ,sum(totalnoofadults) totalnoofadults  
   ,sum(pickupnoofchild) pickupnoofchild
   ,sum(groupnoofchild) groupnoofchild
   ,sum(groupmasternoofchild) groupmasternoofchild
   ,sum(blockngroupnoofchild) blockngroupnoofchild
   ,sum(blockngroupmasternoofchild) blockngroupmasternoofchild
   ,sum(totalnoofchild) totalnoofchild  
 from (
select
  wdat_date
 ,ycad_id 
 ,pickupnoofrooms
 ,groupnoofrooms
 ,groupmasternoofrooms
 ,blockngrouprooms
 ,blockngroupmasterrooms
 ,totalnoofrooms
 ,pickupnoofadults
 ,groupnoofadults
 ,groupmasternoofadults
 ,blockngroupnoofadults
 ,blockngroupmasternoofadults
 ,totalnoofadults
 ,pickupnoofchild
 ,groupnoofchild
 ,groupmasternoofchild
 ,blockngroupnoofchild
 ,blockngroupmasternoofchild
 ,totalnoofchild
from v8_sys_noofrescategories_cad
union all
select
  wdat_date
 ,ycad_id 
 ,0 pickupnoofrooms
 ,0 groupnoofrooms
 ,0 groupmasternoofrooms
 ,0 blockngrouprooms
 ,0 blockngroupmasterrooms
 ,0 totalnoofrooms
 ,pickupnoofadults
 ,groupnoofadults
 ,groupmasternoofadults
 ,blockngroupnoofadults
 ,blockngroupmasternoofadults
 ,totalnoofadults
 ,pickupnoofchild
 ,groupnoofchild
 ,groupmasternoofchild
 ,blockngroupnoofchild
 ,blockngroupmasternoofchild
 ,totalnoofchild
from v8_sys_noofResCategor_cad_bed
) group by wdat_date,ycad_id;

create or replace view v8_sys_current_occ as
select sum(rooms) rooms, sum(adults) adults,sum(children) children from (
select 
    decode(nvl(ydet_sharenum,0),0,sum(ydet_noofrooms),avg(ydet_noofrooms)) rooms
   ,sum(ydet_adultno) adults
   ,sum((select sum(yrgl_number) from yrgl where yrgl_ydet_id=ydet_id)) children
from ydet yd,yres y,ycat
where
    (   ydet_date=(select WGBS_DATEVALUE from wgbs) 
     or (yres_expdeptime=(select WGBS_DATEVALUE from wgbs) and ydet_id=yres_current_ydet_id)
    )  
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is not null
and yres_actdeptime is null
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by ydet_sharenum
);

create or replace view v8_sys_current_occ_bed as
select 
    sum(case when yrpe_xcac_id=0 then 1 end) adults
   ,sum(case when yrpe_xcac_id>0 then 1 end) children
from ydet,yres,ycat,yrpe
where
    (   ydet_date=(select WGBS_DATEVALUE from wgbs) 
     or (yres_expdeptime=(select WGBS_DATEVALUE from wgbs) and ydet_id=yres_current_ydet_id)
    )  
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_yres_id=yres_id
and yrpe_actarrtime is not null
and yrpe_actdeptime is null
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
;

create or replace view v8_sys_current_occ_RoomBed as
select sum(rooms) rooms,sum(adults) adults,sum(children) children
from (
select rooms,adults,children from v8_sys_current_occ
union all 
select null rooms,adults,children from v8_sys_current_occ_bed
);

create or replace view v8_sys_current_occ_cad as
select ycat_sum_roomtype_ycad_id ycad_id,sum(rooms) rooms, sum(adults) adults,sum(children) children from (
select 
   ycat_sum_roomtype_ycad_id
   ,decode(nvl(ydet_sharenum,0),0,sum(ydet_noofrooms),avg(ydet_noofrooms)) rooms
   ,sum(ydet_adultno) adults
   ,sum((select sum(yrgl_number) from yrgl where yrgl_ydet_id=ydet_id)) children
from ydet yd,yres y,ycat
where
    (   ydet_date=(select WGBS_DATEVALUE from wgbs) 
     or (yres_expdeptime=(select WGBS_DATEVALUE from wgbs) and ydet_id=yres_current_ydet_id)
    )  
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is not null
and yres_actdeptime is null
and ycat_id=ydet_ycat_id
and ycat_notinavl=0
group by ydet_sharenum,ycat_sum_roomtype_ycad_id
) group by ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_current_occ_cad_bed as
select
    ycat_sum_roomtype_ycad_id ycad_id 
   ,sum(case when yrpe_xcac_id=0 then 1 end) adults
   ,sum(case when yrpe_xcac_id>0 then 1 end) children
from ydet,yres,ycat,yrpe
where
    (   ydet_date=(select WGBS_DATEVALUE from wgbs) 
     or (yres_expdeptime=(select WGBS_DATEVALUE from wgbs) and ydet_id=yres_current_ydet_id)
    )  
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yrpe_yres_id=yres_id
and yrpe_actarrtime is not null
and yrpe_actdeptime is null
and ycat_id=ydet_bed_ycat_id
and ycat_notinavl=0
group by ycat_sum_roomtype_ycad_id
;

create or replace view v8_sys_current_occ_cad_RoomBed as
select ycad_id,sum(rooms) rooms,sum(adults) adults,sum(children) children
from (
select ycad_id,rooms,adults,children from v8_sys_current_occ_cad
union all 
select ycad_id,null rooms,adults,children from v8_sys_current_occ_cad_bed
)
group by ycad_id;

create or replace view v8_sys_earlydep as
select sum(rooms) rooms, sum(adults) adults,sum(children) children from (
select 
    decode(nvl(ydet_sharenum,0),0,sum(ydet_noofrooms),avg(ydet_noofrooms)) rooms
   ,sum(ydet_adultno) adults
   ,sum((select sum(yrgl_number) from yrgl where yrgl_ydet_id=ydet_id)) children
from ydet,yres
where
    ydet_id=yres_current_ydet_id
and yres_resstatus<>3
and trunc(yres_orgdeptime)>trunc(yres_actdeptime)
and trunc(yres_actdeptime)=V8_sys_fideliodate
group by ydet_sharenum
);

create or replace view v8_sys_earlydep_bed as
select 
    sum(case when yrpe_xcac_id=0 then 1 end) adults
   ,sum(case when yrpe_xcac_id>0 then 1 end) children
from yres,yrpe
where
    yres_resstatus<>3
and yrpe_yres_id=yres_Id
and trunc(yres_orgdeptime)>trunc(yrpe_actdeptime)
and trunc(yrpe_actdeptime)=V8_sys_fideliodate
;

create or replace view v8_sys_earlydep_RoomBed as
select sum(rooms) rooms,sum(adults) adults,sum(children) children
from (
select rooms,adults,children from v8_sys_earlydep
union all 
select null rooms,adults,children from v8_sys_earlydep_bed
);

create or replace view v8_sys_earlydep_cad as
select ycat_sum_roomtype_ycad_id ycad_id,sum(rooms) rooms, sum(adults) adults,sum(children) children from (
select 
    ycat_sum_roomtype_ycad_id   
   ,decode(nvl(ydet_sharenum,0),0,sum(ydet_noofrooms),avg(ydet_noofrooms)) rooms
   ,sum(ydet_adultno) adults
   ,sum((select sum(yrgl_number) from yrgl where yrgl_ydet_id=ydet_id)) children
from ydet,yres,ycat
where
    ydet_id=yres_current_ydet_id
and yres_resstatus<>3
and trunc(yres_orgdeptime)>trunc(yres_actdeptime)
and trunc(yres_actdeptime)=V8_sys_fideliodate
and ycat_id=ydet_ycat_id
group by ydet_sharenum,ycat_sum_roomtype_ycad_id
) group by ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_earlydep_cad_bed as
select 
    ycat_sum_roomtype_ycad_id ycad_id   
   ,sum(case when yrpe_xcac_id=0 then 1 end) adults
   ,sum(case when yrpe_xcac_id>0 then 1 end) children
from ydet,yres,ycat,yrpe
where
    ydet_id=yres_current_ydet_id
and yres_resstatus<>3
and yrpe_yres_id=yres_id
and trunc(yres_orgdeptime)>trunc(yrpe_actdeptime)
and trunc(yrpe_actdeptime)=V8_sys_fideliodate
and ycat_id=ydet_bed_ycat_id
group by ycat_sum_roomtype_ycad_id;

create or replace view v8_sys_earlydep_cad_RoomBed as
select ycad_id,sum(rooms) rooms,sum(adults) adults,sum(children) children
from (
select ycad_id,rooms,adults,children from v8_sys_earlydep_cad
union all 
select ycad_id,null rooms,adults,children from v8_sys_earlydep_cad_bed
) 
group by ycad_id;

CREATE OR REPLACE VIEW V8_SYS_REVENUE_ON_THE_BOOKS AS
select max(a) Booked, 
	   max(b) OnTheBooks, 
	   max(c) BookedRREV,
	   max(d) BookedFBREV,
	   max(e) BookedMISCREV	   
from 
(
select 
round(sum(zpos.ZPOS_GROSSUNITPRICE*zpos.ZPOS_QUANTITY)) a,
0 b,
0 c,
0 d,
0 e
from zpos
where zpos.ZPOS_POSTDATE =V8_sys_fideliodate
and zpos.ZPOS_CDT<>5 
union
select 
0 a,
round(sum(ypos.YPOS_QUANTITY * ypos.YPOS_UNITPRICE)) b ,
0 c,
0 d,
0 e
from ypos
where ypos.YPOS_DATE = V8_sys_fideliodate
and not exists (select 1 from yres where yres.yres_lastfixposttime=ypos_date and yres.YRES_ID=YPOS_YRES_ID)
union
select 
0 a,
0 b,
round(sum(zpos.ZPOS_GROSSUNITPRICE*zpos.ZPOS_QUANTITY)) c,
0 d,
0 e
from zpos, zdco
where zpos.ZPOS_POSTDATE =V8_sys_fideliodate
and zpos.ZPOS_CDT<>5
and zpos.ZPOS_ZDCO_ID = zdco.ZDCO_ID
and zdco.ZDCO_STATS_TYPE = 1
union
select 
0 a,
0 b,
0 c,
round(sum(zpos.ZPOS_GROSSUNITPRICE*zpos.ZPOS_QUANTITY)) d,
0 e
from zpos, zdco
where zpos.ZPOS_POSTDATE =V8_sys_fideliodate
and zpos.ZPOS_CDT<>5
and zpos.ZPOS_ZDCO_ID = zdco.ZDCO_ID
and zdco.ZDCO_STATS_TYPE = 2
union
select 
0 a,
0 b,
0 c,
0 d,
round(sum(zpos.ZPOS_GROSSUNITPRICE*zpos.ZPOS_QUANTITY)) e
from zpos, zdco
where zpos.ZPOS_POSTDATE =V8_sys_fideliodate
and zpos.ZPOS_CDT<>5
and zpos.ZPOS_ZDCO_ID = zdco.ZDCO_ID
and zdco.ZDCO_STATS_TYPE not in (1)
);



CREATE OR REPLACE FORCE VIEW V8_SYS_REVPAR
AS 
select
   ypos_date wdat_Date
   ,round((sum(ratenet)/(select (YRMS_WDAT_COUNT.NUMBEROFROOMS - YRMS_WDAT_COUNT.OOO) from YRMS_WDAT_COUNT where YRMS_WDAT_COUNT.WDAT_DATE = ypos_date)),2) as RevPar
from
  (   
    select sum(ratenet) RateNet, Ypos_date from
   (SELECT SUM (DECODE (zdco_nonhtlrev, 1, 0, YPOS_unitprice * ypos_quantity)
              ) RateNet,
              ypos_date 
     FROM Ypos, zdco, yres
    WHERE Ypos_zdco_id = zdco_id
      AND yres.YRES_ID = ypos.YPOS_YRES_ID
      AND yres.YRES_RESSTATUS <> 3
      AND Ypos_date >= V8_sys_fideliodate
      AND zdco.ZDCO_CDT = 1
      AND Zdco_stats_type = 1
      group by ypos_date
union
   SELECT SUM (YPOS_unitprice * ypos_quantity) RateNet,
              ypos_date      from v8_rep_rev_forecast_huge
      where 
        v8_rep_rev_forecast_huge.YPOS_DT = 1 and
        v8_rep_rev_forecast_huge.ZDCO_STATS_TYPE = 1 and
        v8_rep_rev_forecast_huge.TYPENUM in  (4,5)
        AND v8_rep_rev_forecast_huge.YPOS_DATE >= V8_sys_fideliodate
      group by ypos_date)     
      group by ypos_date)
      group by ypos_date
union
SELECT
sman.SMAN_DATE WDAT_DATE,
CASE WHEN
  (SMAN_ROOMSINHOTEL_D - SMAN_ROOMS_OOO_D) = 0
  THEN
  0
  ELSE
ROUND(SMAN_ROOMREVNET_D/(SMAN_ROOMSINHOTEL_D - SMAN_ROOMS_OOO_D),2)  
END RevPar
FROM
sman;




CREATE OR REPLACE FORCE VIEW V8_SYS_MONTH_AVG_REVPAR
as
select 
  TO_CHAR(a.WDAT_DATE ,'mm') Month,
  TO_CHAR(a.WDAT_DATE,'yyyy_mm') MonthYear,  
  round(avg(a.REVPAR ),2) RevPaR 
from 
V8_SYS_REVPAR a 
group by TO_CHAR(a.WDAT_DATE,'mm'),  TO_CHAR(a.WDAT_DATE,'yyyy_mm')
order by TO_CHAR(a.WDAT_DATE,'yyyy_mm');



--NEW VIEW V8_SYS_HS_ARR_ALLADL_XCMA_CAD for Hotelsegment calculation for VIEW V8_REP_XCMA_HISTFOR_CAD

CREATE OR REPLACE VIEW V8_SYS_HS_ARR_ALLADL_XCMA_CAD
AS 
SELECT wdat_date,xcma_id,ycad_id,SUM(allexpadult) allexpadult,SUM(defexpadult) defexpadult,SUM(tentexpadult) tentexpadult  FROM (
SELECT
   wdat_date
   ,ydet_xcma_id xcma_id
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,SUM(ydet_adultno*ydet_noofrooms) allexpadult
   ,DECODE(yres_resstatus,1,SUM(ydet_adultno*ydet_noofrooms)) defexpadult
   ,DECODE(yres_resstatus,0,SUM(ydet_adultno*ydet_noofrooms)) tentexpadult
FROM WDAT,YDET,YRES,YCAT
WHERE
    ydet_date=wdat_date
AND yres_exparrtime=wdat_date
AND ydet_yres_id=yres_id
AND yres_resstatus<>3
AND ycat_id=ydet_ycat_id
AND ycat_notinavl=0
GROUP BY wdat_date,yres_resstatus,ydet_xcma_id,ycat_sum_roomtype_ycad_id
) GROUP BY wdat_date,xcma_id,ycad_id;

CREATE OR REPLACE VIEW V8_SYS_HS_ARR_ALLADL_XCMA_PRT
AS 
SELECT wdat_date,xcma_id,ycat_id,SUM(allexpadult) allexpadult,SUM(defexpadult) defexpadult,SUM(tentexpadult) tentexpadult  FROM (
SELECT
   wdat_date
   ,ydet_xcma_id xcma_id
   ,ycat_id
   ,SUM(ydet_adultno*ydet_noofrooms) allexpadult
   ,DECODE(yres_resstatus,1,SUM(ydet_adultno*ydet_noofrooms)) defexpadult
   ,DECODE(yres_resstatus,0,SUM(ydet_adultno*ydet_noofrooms)) tentexpadult
FROM WDAT,YDET,YRES,YCAT
WHERE
    ydet_date=wdat_date
AND yres_exparrtime=wdat_date
AND ydet_yres_id=yres_id
AND yres_resstatus<>3
AND ycat_id=ydet_ycat_id
AND ycat_notinavl=0
GROUP BY wdat_date,yres_resstatus,ydet_xcma_id,ycat_id
) GROUP BY wdat_date,xcma_id,ycat_id;

--NEW VIEW V8_SYS_HS_ARR_ALLROOM_XCMA_CAD for Hotelsegment calculation for VIEW V8_REP_XCMA_HISTFOR_CAD

CREATE OR REPLACE VIEW V8_SYS_HS_ARR_ALLROOM_XCMA_CAD
AS 
SELECT wdat_date,xcma_id,ycad_id,SUM(allexproom) allexproom,SUM(defexproom) defexproom,SUM(tentexproom) tentexproom  FROM (
SELECT
   wdat_date
   ,ydet_xcma_id xcma_id
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,DECODE(NVL(ydet_sharenum,0),0,SUM(ydet_noofrooms),AVG(ydet_noofrooms)) allexproom
   ,DECODE(NVL(ydet_sharenum,0),0,SUM(DECODE(yres_resstatus,1,ydet_noofrooms,0)),DECODE(MAX(yres_resstatus),1,AVG(ydet_noofrooms),0)) defexproom
   ,DECODE(NVL(ydet_sharenum,0),0,SUM(DECODE(yres_resstatus,0,ydet_noofrooms,0)),DECODE(MAX(yres_resstatus),0,AVG(ydet_noofrooms),0)) tentexproom
FROM WDAT,YDET yd,YRES y,YCAT
WHERE
    ydet_date=wdat_date
AND yres_exparrtime=wdat_date
AND ydet_yres_id=yres_id
AND yres_resstatus<>3
AND ycat_id=ydet_ycat_id
AND ycat_notinavl=0
AND (NVL(ydet_sharenum,0)=0
     OR
  NOT EXISTS (SELECT d.ydet_sharenum
                        FROM YDET d,YRES r
         WHERE d.ydet_yres_id=r.yres_id
        AND d.ydet_id<>yd.ydet_id
        AND (y.yres_exparrtime<>r.yres_exparrtime)
        AND d.ydet_sharenum=yd.ydet_sharenum
         )
 )
GROUP BY wdat_date,ydet_sharenum,ydet_xcma_id,ycat_sum_roomtype_ycad_id
) GROUP BY wdat_date,xcma_id,ycad_id;

CREATE OR REPLACE VIEW V8_SYS_HS_ARR_ALLROOM_XCMA_PRT
AS 
SELECT wdat_date,xcma_id,ycat_id,SUM(allexproom) allexproom,SUM(defexproom) defexproom,SUM(tentexproom) tentexproom  FROM (
SELECT
   wdat_date
   ,ydet_xcma_id xcma_id
   ,ycat_id
   ,DECODE(NVL(ydet_sharenum,0),0,SUM(ydet_noofrooms),AVG(ydet_noofrooms)) allexproom
   ,DECODE(NVL(ydet_sharenum,0),0,SUM(DECODE(yres_resstatus,1,ydet_noofrooms,0)),DECODE(MAX(yres_resstatus),1,AVG(ydet_noofrooms),0)) defexproom
   ,DECODE(NVL(ydet_sharenum,0),0,SUM(DECODE(yres_resstatus,0,ydet_noofrooms,0)),DECODE(MAX(yres_resstatus),0,AVG(ydet_noofrooms),0)) tentexproom
FROM WDAT,YDET yd,YRES y,YCAT
WHERE
    ydet_date=wdat_date
AND yres_exparrtime=wdat_date
AND ydet_yres_id=yres_id
AND yres_resstatus<>3
AND ycat_id=ydet_ycat_id
AND ycat_notinavl=0
AND (NVL(ydet_sharenum,0)=0
     OR
  NOT EXISTS (SELECT d.ydet_sharenum
                        FROM YDET d,YRES r
         WHERE d.ydet_yres_id=r.yres_id
        AND d.ydet_id<>yd.ydet_id
        AND (y.yres_exparrtime<>r.yres_exparrtime)
        AND d.ydet_sharenum=yd.ydet_sharenum
         )
 )
GROUP BY wdat_date,ydet_sharenum,ydet_xcma_id,ycat_id
) GROUP BY wdat_date,xcma_id,ycat_id;

--NEW VIEW V8_SYS_HS_DEP_ALLROOM_XCMA_CAD for Hotelsegment calculation for VIEW V8_REP_XCMA_HISTFOR_CAD

CREATE OR REPLACE VIEW V8_SYS_HS_DEP_ALLROOM_XCMA_CAD
AS 
SELECT wdat_date,xcma_id,ycad_id,SUM(alldeproom) alldeproom,SUM(defdeproom) defdeproom ,SUM(tentdeproom) tentdeproom  FROM (
SELECT
   wdat_date
   ,ydet_xcma_id xcma_id
   ,ycat_sum_roomtype_ycad_id ycad_id
   ,ydet_sharenum
   ,DECODE(NVL(ydet_sharenum,0),0,SUM(ydet_noofrooms),AVG(ydet_noofrooms)) alldeproom
   ,DECODE(NVL(ydet_sharenum,0),0,SUM(DECODE(yres_resstatus,1,ydet_noofrooms,0)),DECODE(MAX(yres_resstatus),1,AVG(ydet_noofrooms),0)) defdeproom
   ,DECODE(NVL(ydet_sharenum,0),0,SUM(DECODE(yres_resstatus,0,ydet_noofrooms,0)),DECODE(MAX(yres_resstatus),0,AVG(ydet_noofrooms),0)) tentdeproom
FROM WDAT,YDET yd,YRES y,YCAT
WHERE
ydet_id=yres_last_ydet_id
AND yres_expdeptime=wdat_date
AND yres_resstatus<>3
AND ycat_id=ydet_ycat_id
AND ycat_notinavl=0
AND (NVL(ydet_sharenum,0)= 0
     OR
  NOT EXISTS (SELECT d.ydet_sharenum
                        FROM YDET d,YRES r
         WHERE d.ydet_yres_id=r.yres_id
        AND d.ydet_id<>yd.ydet_id
        AND y.yres_expdeptime<>r.yres_expdeptime
        AND d.ydet_sharenum=yd.ydet_sharenum
         )
 )
GROUP BY wdat_date,ydet_sharenum,ydet_xcma_id,ycat_sum_roomtype_ycad_id)
GROUP BY wdat_date,xcma_id,ycad_id;

CREATE OR REPLACE VIEW V8_SYS_HS_DEP_ALLROOM_XCMA_PRT
AS 
SELECT wdat_date,xcma_id,ycat_id,SUM(alldeproom) alldeproom,SUM(defdeproom) defdeproom ,SUM(tentdeproom) tentdeproom  FROM (
SELECT
   wdat_date
   ,ydet_xcma_id xcma_id
   ,ycat_id
   ,ydet_sharenum
   ,DECODE(NVL(ydet_sharenum,0),0,SUM(ydet_noofrooms),AVG(ydet_noofrooms)) alldeproom
   ,DECODE(NVL(ydet_sharenum,0),0,SUM(DECODE(yres_resstatus,1,ydet_noofrooms,0)),DECODE(MAX(yres_resstatus),1,AVG(ydet_noofrooms),0)) defdeproom
   ,DECODE(NVL(ydet_sharenum,0),0,SUM(DECODE(yres_resstatus,0,ydet_noofrooms,0)),DECODE(MAX(yres_resstatus),0,AVG(ydet_noofrooms),0)) tentdeproom
FROM WDAT,YDET yd,YRES y,YCAT
WHERE
ydet_id=yres_last_ydet_id
AND yres_expdeptime=wdat_date
AND yres_resstatus<>3
AND ycat_id=ydet_ycat_id
AND ycat_notinavl=0
AND (NVL(ydet_sharenum,0)= 0
     OR
  NOT EXISTS (SELECT d.ydet_sharenum
                        FROM YDET d,YRES r
         WHERE d.ydet_yres_id=r.yres_id
        AND d.ydet_id<>yd.ydet_id
        AND y.yres_expdeptime<>r.yres_expdeptime
        AND d.ydet_sharenum=yd.ydet_sharenum
         )
 )
GROUP BY wdat_date,ydet_sharenum,ydet_xcma_id,ycat_id)
GROUP BY wdat_date,xcma_id,ycat_id;

-- calculation rooms occupied with one pax

create or replace view v8_sys_current_occ_pax_base as 
select 
   ydet_sharenum
  ,ydet_noofrooms rooms
  ,ydet_adultno adults
  ,nvl((select sum(yrgl_number) from yrgl where yrgl_ydet_id=ydet_id),0) children
from ydet,yres,ycat
where(   ydet_date=(select WGBS_DATEVALUE from wgbs) 
       or (yres_expdeptime=(select WGBS_DATEVALUE from wgbs) and ydet_id=yres_current_ydet_id)
     )  
and ydet_yres_id=yres_id
and yres_resstatus<>3
and yres_actarrtime is not null
and yres_actdeptime is null
and ycat_id=ydet_ycat_id
and ycat_notinavl=0;
 
create or replace view V8_SYS_CURRENT_OCC_PAX as
select
    0 ydet_sharenum 
   ,sum(rooms) rooms
   ,adults
   ,children
   ,adults+children pax
from v8_sys_current_occ_pax_base
where ydet_sharenum is null
group by adults,children
union all
select
    ydet_sharenum 
   ,avg(rooms) rooms
   ,sum(adults) adults
   ,sum(children) children
   ,sum(adults+children) pax
from v8_sys_current_occ_pax_base
where ydet_sharenum is not null
group by ydet_sharenum;

CREATE OR REPLACE VIEW V8_SYS_CURRENT_OCC_PAX2
AS 
select
   sum(case when pax=0 then rooms else 0 end) e0Rooms
  ,sum(case when pax=1 then rooms else 0 end) e1Rooms
  ,sum(case when pax=1 then adults else 0 end) e1Adults
  ,sum(case when pax=1 then children else 0 end) e1Children
  ,sum(case when pax>1 then rooms else 0 end) m1Rooms
  ,sum(case when pax>1 then rooms*adults else 0 end) m1Adults
  ,sum(case when pax>1 then rooms*children else 0 end) m1Children
from V8_SYS_CURRENT_OCC_PAX; 

create or replace view v8_sys_RoomResInfo as
select
  yrms_id
  ,yrms_shortdesc
  ,yrms_longdesc
  ,yrms_yhks_id
  ,yhks_clean
  ,count(1) resCount
  ,min(yres_exparrtime) arrival
  ,max(yres_expdeptime) departure
  ,sum(ydet_adultno) adults
  ,sum(nvl((select sum(yrgl_number) from yrgl where yrgl_ydet_id=ydet_id),0)) childCount
  ,max(decode(yres_expdeptime,wgbs_datevalue,yres_expDepHour,null)) expDepHourCurrentGuest
  ,(select min(yres_exparrhour) 
    from yres,ydet 
    where yres_exparrtime=(select * from fideliodate)
    and ydet_yres_id=yres_id
    and yres_actarrtime is null
    and yres_actdeptime is null
    and yres_resstatus<>3
    and ydet_yrms_id=yrms_id
   ) expArrHourNextGuestToday
  ,min(yres_id) minYresId 
  ,max((select 1 from xpda, xpal, xcms  where  XPAL.XPAL_XPDA_ID = XPDA.XPDA_ID AND  XPAL.XPAL_XCMS_ID = XCMS_ID AND XPDA.XPDA_XPAC_ID = 11 and rownum = 1 and yres_xcms_id=xcms_id)) Vip 
  ,max(((select 1 from yque where yres_id=yque_yres_id))) inQueue
  ,(select min(yres_id) 
    from yres,ydet 
    where yres_exparrtime=(select * from fideliodate)
    and ydet_yres_id=yres_id
    and yres_actarrtime is null
    and yres_actdeptime is null
    and yres_resstatus<>3
    and ydet_yrms_id=yrms_id
   ) nextMinYresId
from yrms,yres,ydet,fideliodate,yhks
where yrms_suite=0
and ydet_yrms_id=yrms_id
and yres_id=ydet_yres_id
and yres_actarrtime is not null
and yres_actdeptime is null 
and yres_vacant=0
and (   (ydet_date=wgbs_datevalue)
     or (ydet_date=wgbs_datevalue-1 and yres_expdeptime=wgbs_Datevalue)
    ) 
and yhks_id(+)=yrms_yhks_id
group by 
   yrms_id
  ,yrms_shortdesc
  ,yrms_longdesc
  ,yrms_yhks_id
  ,yhks_clean
union all
select 
  yrms_id
  ,yrms_shortdesc
  ,yrms_longdesc
  ,yrms_yhks_id
  ,yhks_clean
  ,count(yres_id)
  ,min(yres_exparrtime)  arrival
  ,max(yres_expdeptime) departure
  ,sum(ydet_adultno) adults
  ,sum(nvl((select sum(yrgl_number) from yrgl where yrgl_ydet_id=ydet_id),0)) childCount
  ,null expDepHourCurrentGuest
  ,min(yres_exparrhour) expArrHourNextGuestToday
  ,null  
  ,max((select 1 from xpda, xpal, xcms  where  XPAL.XPAL_XPDA_ID = XPDA.XPDA_ID AND  XPAL.XPAL_XCMS_ID = XCMS_ID AND XPDA.XPDA_XPAC_ID = 11 and rownum = 1 and yres_xcms_id=xcms_id)) Vip 
  ,max(((select 1 from yque where yres_id=yque_yres_id))) inQueue
  ,min(Yres_Id)
from yrms,yres,ydet,fideliodate,yhks
where yrms_suite=0
and ydet_yrms_id=yrms_id
and yres_id=ydet_yres_id
and ydet_date=wgbs_datevalue
and yres_exparrtime=wgbs_datevalue 
and yres_actarrtime is null
and yres_resstatus<>3
and yhks_id(+)=yrms_yhks_id
and not exists (
     select 1 
     from ydet y,yres r
     where y.ydet_yrms_id=yrms_id
     and r.yres_id=y.ydet_yres_id
     and yres_actarrtime is not null
     and yres_actdeptime is null 
     and yres_vacant=0
     and (   (y.ydet_date=wgbs_datevalue and r.yres_actarrtime is not null)
          or (y.ydet_date=wgbs_datevalue-1 and r.yres_expdeptime=wgbs_Datevalue and r.yres_actdeptime is null and r.yres_vacant=0)
         ) 
    )
group by 
   yrms_id
  ,yrms_shortdesc
  ,yrms_longdesc
  ,yrms_yhks_id
  ,yhks_clean;
