--LAST UPDATE SCRIPT CONTAINING ONLY VIEWS THAT HAVE CHANGED SINCE PREVIOUS RELEASE

create or replace function V8_FNC_GETFIELDVALUE
(
 /*
Function retuns the name of the booked menu item, FS, resource, package.
eg select getfieldvalue('YEBR','YEBR_NAME', 2714) from dual will return the resource name of the booked resource with id 2714.
The function is used in getccmzposcommenttranslated   
*/ 
tablename varchar2,
tablefield varchar2,
tableid number
)
return varchar2 is
  TYPE dynamicCursor IS REF CURSOR;
  curr  dynamicCursor;
  res varchar2(2000);
begin
  OPEN curr FOR 'select '||tablefield||' from '||tablename||' where '||tablename||'_id='||nvl(tableid, 0);
  FETCH curr into res;
  return res;
end; 
/

CREATE OR REPLACE function getccmzposcommenttranslated
(
/*
Function retuns the translated values of CCM resources in the invoices.
It will return the translation if zpos_comment = the name of the resource.
If zpos_comment is manually overwrittten it will return zpos_comment.
The function is used in V8_ED_FOLIO_REV_INFO which is the base view for internal editor folios.
*/ 
zposid number
)
 return varchar2 is
  cursor cursorzposcomment is 
    select zpos_comment,
      decode(zpos_comment, 
        V8_FNC_GETFIELDVALUE(decode(yblp_resource_type, 2, 'YFSC', 3, 'YEBR', 5,'YEMI', 9, 'YPKD', 8,'YEMN', 10, 'YEMN', 'YEBR'),  
                           decode(yblp_resource_type, 2, 'YFSC_LONGDESC', 3, 'YEBR_NAME', 5, 'YEMI_NAME', 9, 'YPKD_NAME', 8, 'YEMN_MENUNAME', 10, 'YEMN_MENUNAME', 'YEBR_NAME'), 
                           decode(yblp_resource_type, 2, (select yebs_yfsc_id from yebs where yebs_id=yblp_resource_id), 3, yblp_resource_id, 5, yblp_resource_id, 9, yblp_resource_id, 8, (select ymrd_yemn_id from ymrd where ymrd_id=yblp_resource_id) , 10, yblp_resource_id, 0)), 
        V8_FNC_WTRL_TRANSLATE(
        decode(yblp_resource_type, 2, 'YEBS', 3, 'YEBR', 5,'YEMI',9,'YPKD', 8, 'YEMN', 10, 'YEMN', 'YEBR'), 
        decode(yblp_resource_type, 2, 'YFSC_LONGDESC', 3, 'YEBR_NAME', 5, 'YEMI_NAME',9,'YPKD_NAME', 8, 'YEMN_MENUNAME', 10, 'YEMN_MENUNAME', 'YEBR_NAME'),
        decode(yblp_resource_type, 8, (select ymrd_yemn_id from ymrd where ymrd_id=yblp_resource_id), yblp_resource_id), 
          NVL
          (
               (SELECT xcms.xcms_wlan_id
                FROM xcms
                WHERE xcms.xcms_id = zwin.zwin_invoice_to_xcms_id),
            (SELECT wlan.wlan_id
            FROM wlan
            WHERE wlan.wlan_default = 1)
          ),0
      ), zpos_comment) tr_zpos_comment 
    from zpos, zwin, yblp
    where zpos_id=zposid and zpos_id=yblp_zpos_id and zpos.zpos_zwin_id = zwin.zwin_id(+);
  
   reczposcomment cursorzposcomment%rowtype;
   
BEGIN
  open cursorzposcomment;
  fetch cursorzposcomment into reczposcomment;
  return reczposcomment.tr_zpos_comment;
END;
/

CREATE OR REPLACE VIEW V8_REP_YHRL_INFOS
AS 
SELECT
  (select wgbs.wgbs_datevalue from wgbs) FIDELIODATE,
  nvl(YHRL.YHRL_YRMS_ID,0) YRMS_ID,   
  (select yrms.YRMS_SHORTDESC from yrms where YRMS.YRMS_ID=YHRL.YHRL_YRMS_ID) YRMS_SHORTDESC,
  (select yrms.YRMS_LONGDESC from yrms where YRMS.YRMS_ID=YHRL.YHRL_YRMS_ID) YRMS_LONGDESC,
  (select yrms.YRMS_YCAT_ID from yrms where YRMS.YRMS_ID=YHRL.YHRL_YRMS_ID) YRMS_YCAT_ID,
  nvl(YHRL.YHRL_YPUB_ID,0) YPUB_ID,
  nvl(YHRL.YHRL_YFSC_ID,0) YFSC_ID,
  decode(nvl(YHRL.YHRL_YRMS_ID,0),0,decode(nvl(YHRL.YHRL_YPUB_ID,0),0,(select YFSC.YFSC_LONGDESC from yfsc where YFSC.YFSC_ID=YHRL.YHRL_YFSC_ID),(select YPUB.YPUB_LONGDESC from ypub where YPUB.YPUB_ID=YHRL.YHRL_YPUB_ID)),(select YCAT.YCAT_SHORTDESC from ycat where YCAT.YCAT_ID=(select yrms.YRMS_YCAT_ID from yrms where YRMS.YRMS_ID=YHRL.YHRL_YRMS_ID)) ) PLACES_FS,
  yhrl.YHRL_AUTOMAID_NR YHRL_AUTOMAID_NR,
  yhrl.YHRL_CREDIT YHRL_CREDIT,
  yhrl.YHRL_AUTOMAID_MAXCREDITS YHRL_AUTOMAID_MAXCREDITS,
  yhrl.YHRL_YHAT_ID YHRL_YHAT_ID,
  yhrl.YHRL_DATE YHRL_DATE,
  (select yhac.YHAC_COMMENT from yhac where yhac.YHAC_ID=yhrl.YHRL_YHAC_ID) ATTENDANT_NOTES,
  (select yhat.YHAT_NAME from yhat where yhat.YHAT_ID=yhrl.YHRL_YHAT_ID) YHAT_NAME,
  (select yhat.YHAT_NAME from yhat, yhhc, yrms,yhrl maid_yhrl where yhhc.YHHC_ID = yhat.YHAT_YHHC_ID and maid_yhrl.YHRL_YHAT_ID = yhat.YHAT_ID and yhhc.YHHC_ROLE = 0 and maid_yhrl.YHRL_DATE = yhrl.YHRL_DATE and maid_Yhrl.YHRL_YRMS_ID =yrms.yrms_id and rownum=1) YHAT_MAID_NAME,
  (select yhhc.YHHC_SHORTDESC from yhat, YHHC where yhhc.YHHC_ID = yhat.YHAT_YHHC_ID and  yhat.YHAT_ID=yhrl.YHRL_YHAT_ID) YHHC_SHORTDESC,
  (select yhhc.YHHC_LONGDESC from yhat, YHHC where yhhc.YHHC_ID = yhat.YHAT_YHHC_ID and  yhat.YHAT_ID=yhrl.YHRL_YHAT_ID) YHHC_LONGDESC,
  (select yhhc.YHHC_ROLE from yhat, YHHC where yhhc.YHHC_ID = yhat.YHAT_YHHC_ID and  yhat.YHAT_ID=yhrl.YHRL_YHAT_ID) YHHC_ROLE,    
  (select decode(yhhc.YHHC_ROLE,0,'Maid',1,'Housekeeper','UNK') from yhat, YHHC where yhhc.YHHC_ID = yhat.YHAT_YHHC_ID and  yhat.YHAT_ID=yhrl.YHRL_YHAT_ID) YHHC_TYPE,  
  (select YCAT.YCAT_SHORTDESC from ycat where YCAT.YCAT_ID=(select yrms.YRMS_YCAT_ID from yrms where YRMS.YRMS_ID=YHRL.YHRL_YRMS_ID)) YCAT_SHORTDESC,
  (select YCAT.YCAT_LONGDESC from ycat where YCAT.YCAT_ID=(select yrms.YRMS_YCAT_ID from yrms where YRMS.YRMS_ID=YHRL.YHRL_YRMS_ID)) YCAT_LONGDESC,
  (select YRMS.YRMS_YHKS_ID from yrms where YRMS.YRMS_ID=YHRL.YHRL_YRMS_ID) YRMS_YHKS_ID,
  (select yhks.YHKS_SHORTDESC from yhks where YHKS.YHKS_ID=(select YRMS.YRMS_YHKS_ID from yrms where YRMS.YRMS_ID=YHRL.YHRL_YRMS_ID)) YHKS_SHORTDESC,
  (select yhks.YHKS_LONGDESC from yhks where YHKS.YHKS_ID=(select YRMS.YRMS_YHKS_ID from yrms where YRMS.YRMS_ID=YHRL.YHRL_YRMS_ID)) YHKS_LONGDESC,
  (select yrst.YRST_ID from yrst where ((select wgbs.wgbs_datevalue from wgbs) between yrst.YRST_FROMTIME and yrst.YRST_UNTILTIME-1) and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1) YRST_ID,
  (select yrst.YRST_YRSC_ID from yrst where ((select wgbs.wgbs_datevalue from wgbs) between yrst.YRST_FROMTIME and yrst.YRST_UNTILTIME-1) and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1) YRST_YRSC_ID,
  (select yrsc.YRSC_SHORTDESC from yrsc,yrst where yrsc.YRSC_ID=yrst.YRST_YRSC_ID and ((select wgbs.wgbs_datevalue from wgbs) between yrst.YRST_FROMTIME and yrst.YRST_UNTILTIME-1) and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1) YRSC_SHORTDESC,
  (select yrst.YRST_FROMTIME from yrst where ((select wgbs.wgbs_datevalue from wgbs) between yrst.YRST_FROMTIME and yrst.YRST_UNTILTIME-1) and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1) YRST_FROMTIME,
  (select yrst.YRST_UNTILTIME from yrst where ((select wgbs.wgbs_datevalue from wgbs) between yrst.YRST_FROMTIME and yrst.YRST_UNTILTIME-1) and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1) YRST_UNTILTIME,
  (select yrst.YRST_ID from yrst where (select wgbs.wgbs_datevalue from wgbs)=yrst.YRST_FROMTIME and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID) NEXT_YRST_ID,
  (select yrst.YRST_YRSC_ID from yrst where (select wgbs.wgbs_datevalue from wgbs)=yrst.YRST_FROMTIME and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID) NEXT_YRST_YRSC_ID,
  (select yrsc.YRSC_SHORTDESC from yrsc,yrst where yrsc.YRSC_ID=yrst.YRST_YRSC_ID and (select wgbs.wgbs_datevalue from wgbs)=yrst.YRST_FROMTIME and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID) NEXT_YRSC_SHORTDESC,
  (select yrst.YRST_FROMTIME from yrst where (select wgbs.wgbs_datevalue from wgbs)=yrst.YRST_FROMTIME and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID) NEXT_YRST_FROMTIME,
  (select yrst.YRST_UNTILTIME from yrst where (select wgbs.wgbs_datevalue from wgbs)=yrst.YRST_FROMTIME and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID) NEXT_YRST_UNTILTIME,
  decode((select yrst.YRST_ID from yrst where (select wgbs.wgbs_datevalue from wgbs)=yrst.YRST_FROMTIME and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID),NULL,(select yrst.YRST_ID from yrst where ((select wgbs.wgbs_datevalue from wgbs) between yrst.YRST_FROMTIME and yrst.YRST_UNTILTIME-1) and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1),((select yrst.YRST_ID from yrst where (select wgbs.wgbs_datevalue from wgbs)=yrst.YRST_FROMTIME and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID))) BOTH_YRST_ID,
  decode((select yrst.YRST_ID from yrst where (select wgbs.wgbs_datevalue from wgbs)=yrst.YRST_FROMTIME and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID),NULL,(select yrst.YRST_ID from yrst where ((select wgbs.wgbs_datevalue from wgbs) between yrst.YRST_FROMTIME and yrst.YRST_UNTILTIME-1) and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1),((select yrst.YRST_ID from yrst where (select wgbs.wgbs_datevalue from wgbs)=yrst.YRST_FROMTIME and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID)),NULL,0,1) IS_OOO,
  decode((select yrst.YRST_ID from yrst where (select wgbs.wgbs_datevalue from wgbs)=yrst.YRST_FROMTIME and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID),NULL,(select yrsc.YRSC_SHORTDESC from yrsc,yrst where yrsc.YRSC_ID=yrst.YRST_YRSC_ID and ((select wgbs.wgbs_datevalue from wgbs) between yrst.YRST_FROMTIME and yrst.YRST_UNTILTIME-1) and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1),(select yrsc.YRSC_SHORTDESC from yrsc,yrst where yrsc.YRSC_ID=yrst.YRST_YRSC_ID and (select wgbs.wgbs_datevalue from wgbs)=yrst.YRST_FROMTIME and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID)) BOTH_YRSC_SHORTDESC,
  decode((select yrst.YRST_ID from yrst where (select wgbs.wgbs_datevalue from wgbs)=yrst.YRST_FROMTIME and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID),NULL,(select yrst.YRST_FROMTIME from yrst where ((select wgbs.wgbs_datevalue from wgbs) between yrst.YRST_FROMTIME and yrst.YRST_UNTILTIME-1) and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1),((select yrst.YRST_FROMTIME from yrst where (select wgbs.wgbs_datevalue from wgbs)=yrst.YRST_FROMTIME and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID))) BOTH_YRST_FROMTIME,
  decode((select yrst.YRST_ID from yrst where (select wgbs.wgbs_datevalue from wgbs)=yrst.YRST_FROMTIME and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID),NULL,(select yrst.YRST_UNTILTIME from yrst where ((select wgbs.wgbs_datevalue from wgbs) between yrst.YRST_FROMTIME and yrst.YRST_UNTILTIME-1) and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1),((select yrst.YRST_UNTILTIME from yrst where (select wgbs.wgbs_datevalue from wgbs)=yrst.YRST_FROMTIME and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID))) BOTH_YRST_UNTILTIME,
  decode((select yrst.YRST_ID from yrst where (select wgbs.wgbs_datevalue from wgbs)=yrst.YRST_FROMTIME and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID),NULL,(decode((select yrst.YRST_ID from yrst where ((select wgbs.wgbs_datevalue from wgbs) between yrst.YRST_FROMTIME and yrst.YRST_UNTILTIME-1) and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1),NULL,(select yhks.YHKS_SHORTDESC from yhks where yhks.YHKS_ID=(select YRMS.YRMS_YHKS_ID from yrms where YRMS.YRMS_ID=YHRL.YHRL_YRMS_ID)),(select yrsc.YRSC_SHORTDESC from yrsc,yrst where yrsc.YRSC_ID=yrst.YRST_YRSC_ID and ((select wgbs.wgbs_datevalue from wgbs) between yrst.YRST_FROMTIME and yrst.YRST_UNTILTIME-1) and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1))),(select yrsc.YRSC_SHORTDESC from yrsc,yrst where yrsc.YRSC_ID=yrst.YRST_YRSC_ID and (select wgbs.wgbs_datevalue from wgbs)=yrst.YRST_FROMTIME and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID)) ROOM_STATUS,
  (select yres.YRES_ID from yres where (select wgbs.wgbs_datevalue from wgbs) between (YRES.YRES_EXPARRTIME) and (YRES.YRES_EXPDEPTIME) and yres.YRES_YRMS_ID=yhrl.YHRL_YRMS_ID and yres.YRES_RESSTATUS<>3 and trunc(yres.YRES_ACTARRTIME)<>(select wgbs.wgbs_datevalue from wgbs) and rownum=1) STAY_OVER_YRES_ID,
  decode((select yres.YRES_ID from yres where (select wgbs.wgbs_datevalue from wgbs) between (YRES.YRES_EXPARRTIME) and (YRES.YRES_EXPDEPTIME) and yres.YRES_YRMS_ID=yhrl.YHRL_YRMS_ID and yres.YRES_RESSTATUS<>3 and trunc(yres.YRES_ACTARRTIME)<>(select wgbs.wgbs_datevalue from wgbs) and rownum=1),NULL,0,1) STAY_OVER,
  (select yres.YRES_ID from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE+1=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and yres.YRES_RESSTATUS<>3 and rownum=1) DEP_YRES_ID,
  decode((select yres.YRES_ID from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE+1=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and yres.YRES_RESSTATUS<>3 and rownum=1),NULL,0,1) DEPARTURE,
  (select yres.YRES_ACTDEPTIMENULL from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE+1=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1) DEP_YRES_ACTDEPTIMENULL,
  (select yres.YRES_ID from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1 and (YRES.YRES_EXPARRTIME)=(select wgbs.wgbs_datevalue from wgbs)) ARR_YRES_ID,
  decode((select yres.YRES_ID from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1 and (YRES.YRES_EXPARRTIME)=(select wgbs.wgbs_datevalue from wgbs)),null,0,1) ARRIVAL,
  (select yres.YRES_ACTARRTIMENULL from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1 and (YRES.YRES_EXPARRTIME)=(select wgbs.wgbs_datevalue from wgbs)) ARR_YRES_ACTARRTIMENULL,
  decode((select yres.YRES_ID from yres where (select wgbs.wgbs_datevalue from wgbs) between (YRES.YRES_EXPARRTIME) and (YRES.YRES_EXPDEPTIME) and yres.YRES_YRMS_ID=yhrl.YHRL_YRMS_ID and yres.YRES_RESSTATUS<>3 and trunc(yres.YRES_ACTARRTIME)<>(select wgbs.wgbs_datevalue from wgbs) and rownum=1),NULL,(select yres.YRES_ID from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE+1=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1),(select yres.YRES_ID from yres where (select wgbs.wgbs_datevalue from wgbs) between (YRES.YRES_EXPARRTIME) and (YRES.YRES_EXPDEPTIME) and yres.YRES_YRMS_ID=yhrl.YHRL_YRMS_ID and yres.YRES_RESSTATUS<>3 and trunc(yres.YRES_ACTARRTIME)<>(select wgbs.wgbs_datevalue from wgbs) and rownum=1)) STAY_DEP_YRES_ID,
  decode((select ydet.YDET_ADULTNO from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE+1=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1),NULL,(select ydet.YDET_ADULTNO from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1),(select ydet.YDET_ADULTNO from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE+1=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1)) STAY_DEP_YDET_ADULTNO,
  decode((select yres.YRES_XCMS_ID from yres where (select wgbs.wgbs_datevalue from wgbs) between (YRES.YRES_EXPARRTIME) and (YRES.YRES_EXPDEPTIME) and yres.YRES_YRMS_ID=yhrl.YHRL_YRMS_ID and yres.YRES_RESSTATUS<>3 and trunc(yres.YRES_ACTARRTIME)<>(select wgbs.wgbs_datevalue from wgbs) and rownum=1),NULL,(select yres.YRES_XCMS_ID from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE+1=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1),(select yres.YRES_XCMS_ID from yres where (select wgbs.wgbs_datevalue from wgbs) between (YRES.YRES_EXPARRTIME) and (YRES.YRES_EXPDEPTIME) and yres.YRES_YRMS_ID=yhrl.YHRL_YRMS_ID and yres.YRES_RESSTATUS<>3 and trunc(yres.YRES_ACTARRTIME)<>(select wgbs.wgbs_datevalue from wgbs) and rownum=1)) STAY_DEP_XCMS_ID,
  (select v8_rep_name.NAME2 from v8_rep_name where v8_rep_name.XCMS_ID=(decode((select yres.YRES_XCMS_ID from yres where (select wgbs.wgbs_datevalue from wgbs) between (YRES.YRES_EXPARRTIME) and (YRES.YRES_EXPDEPTIME) and yres.YRES_YRMS_ID=yhrl.YHRL_YRMS_ID and yres.YRES_RESSTATUS<>3 and trunc(yres.YRES_ACTARRTIME)<>(select wgbs.wgbs_datevalue from wgbs) and rownum=1),NULL,(select yres.YRES_XCMS_ID from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE+1=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1),(select yres.YRES_XCMS_ID from yres where (select wgbs.wgbs_datevalue from wgbs) between (YRES.YRES_EXPARRTIME) and (YRES.YRES_EXPDEPTIME) and yres.YRES_YRMS_ID=yhrl.YHRL_YRMS_ID and yres.YRES_RESSTATUS<>3 and trunc(yres.YRES_ACTARRTIME)<>(select wgbs.wgbs_datevalue from wgbs) and rownum=1)))) STAY_DEP_NAME,
  decode((select yres.YRES_ID from yres where (select wgbs.wgbs_datevalue from wgbs) between (YRES.YRES_EXPARRTIME) and (YRES.YRES_EXPDEPTIME) and yres.YRES_YRMS_ID=yhrl.YHRL_YRMS_ID and yres.YRES_RESSTATUS<>3 and trunc(yres.YRES_ACTARRTIME)<>(select wgbs.wgbs_datevalue from wgbs) and rownum=1),NULL,(select yres.YRES_EXPARRTIME from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE+1=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1),(select yres.YRES_EXPARRTIME from yres where (select wgbs.wgbs_datevalue from wgbs) between (YRES.YRES_EXPARRTIME) and (YRES.YRES_EXPDEPTIME) and yres.YRES_YRMS_ID=yhrl.YHRL_YRMS_ID and yres.YRES_RESSTATUS<>3 and trunc(yres.YRES_ACTARRTIME)<>(select wgbs.wgbs_datevalue from wgbs) and rownum=1)) STAY_DEP_YRES_EXPARRTIME,
  decode((select yres.YRES_ID from yres where (select wgbs.wgbs_datevalue from wgbs) between (YRES.YRES_EXPARRTIME) and (YRES.YRES_EXPDEPTIME) and yres.YRES_YRMS_ID=yhrl.YHRL_YRMS_ID and yres.YRES_RESSTATUS<>3 and trunc(yres.YRES_ACTARRTIME)<>(select wgbs.wgbs_datevalue from wgbs) and rownum=1),NULL,(select yres.YRES_EXPDEPTIME from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE+1=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1),(select yres.YRES_EXPDEPTIME from yres where (select wgbs.wgbs_datevalue from wgbs) between (YRES.YRES_EXPARRTIME) and (YRES.YRES_EXPDEPTIME) and yres.YRES_YRMS_ID=yhrl.YHRL_YRMS_ID and yres.YRES_RESSTATUS<>3 and trunc(yres.YRES_ACTARRTIME)<>(select wgbs.wgbs_datevalue from wgbs) and rownum=1)) STAY_DEP_YRES_EXPDEPTIME,
  decode((select yres.YRES_ID from yres where (select wgbs.wgbs_datevalue from wgbs) between (YRES.YRES_EXPARRTIME) and (YRES.YRES_EXPDEPTIME) and yres.YRES_YRMS_ID=yhrl.YHRL_YRMS_ID and yres.YRES_RESSTATUS<>3 and trunc(yres.YRES_ACTARRTIME)<>(select wgbs.wgbs_datevalue from wgbs) and rownum=1),NULL,(select yres.YRES_FREETEXT1 from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE+1=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1),(select yres.YRES_FREETEXT1 from yres where (select wgbs.wgbs_datevalue from wgbs) between (YRES.YRES_EXPARRTIME) and (YRES.YRES_EXPDEPTIME) and yres.YRES_YRMS_ID=yhrl.YHRL_YRMS_ID and yres.YRES_RESSTATUS<>3 and trunc(yres.YRES_ACTARRTIME)<>(select wgbs.wgbs_datevalue from wgbs) and rownum=1)) STAY_DEP_YRES_FREETEXT1,
  decode((select yres.YRES_ID from yres where (select wgbs.wgbs_datevalue from wgbs) between (YRES.YRES_EXPARRTIME) and (YRES.YRES_EXPDEPTIME) and yres.YRES_YRMS_ID=yhrl.YHRL_YRMS_ID and yres.YRES_RESSTATUS<>3 and trunc(yres.YRES_ACTARRTIME)<>(select wgbs.wgbs_datevalue from wgbs) and rownum=1),NULL,(select yres.YRES_FREETEXT2 from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE+1=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1),(select yres.YRES_FREETEXT2 from yres where (select wgbs.wgbs_datevalue from wgbs) between (YRES.YRES_EXPARRTIME) and (YRES.YRES_EXPDEPTIME) and yres.YRES_YRMS_ID=yhrl.YHRL_YRMS_ID and yres.YRES_RESSTATUS<>3 and trunc(yres.YRES_ACTARRTIME)<>(select wgbs.wgbs_datevalue from wgbs) and rownum=1)) STAY_DEP_YRES_FREETEXT2,
  (select xpda.XPDA_SHORTDESC from xpda,xpal where xpda.XPDA_ID=xpal.XPAL_XPDA_ID and xpal.XPAL_XPAC_ID=11 and xpal.XPAL_XCMS_ID=(decode((select yres.YRES_XCMS_ID from yres where (select wgbs.wgbs_datevalue from wgbs) between (YRES.YRES_EXPARRTIME) and (YRES.YRES_EXPDEPTIME) and yres.YRES_YRMS_ID=yhrl.YHRL_YRMS_ID and yres.YRES_RESSTATUS<>3 and trunc(yres.YRES_ACTARRTIME)<>(select wgbs.wgbs_datevalue from wgbs) and rownum=1),NULL,(select yres.YRES_XCMS_ID from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE+1=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1),(select yres.YRES_XCMS_ID from yres where (select wgbs.wgbs_datevalue from wgbs) between (YRES.YRES_EXPARRTIME) and (YRES.YRES_EXPDEPTIME) and yres.YRES_YRMS_ID=yhrl.YHRL_YRMS_ID and yres.YRES_RESSTATUS<>3 and trunc(yres.YRES_ACTARRTIME)<>(select wgbs.wgbs_datevalue from wgbs) and rownum=1)))) STAY_DEP_VIP,
  (select yres.YRES_XCMS_ID from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1 and (YRES.YRES_EXPARRTIME)=(select wgbs.wgbs_datevalue from wgbs)) ARR_XCMS_ID,
  (select v8_rep_name.NAME2 from v8_rep_name where v8_rep_name.XCMS_ID=(select yres.YRES_XCMS_ID from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1 and (YRES.YRES_EXPARRTIME)=(select wgbs.wgbs_datevalue from wgbs))) ARR_NAME,
  (select yres.YRES_EXPARRTIME from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1 and (YRES.YRES_EXPARRTIME)=(select wgbs.wgbs_datevalue from wgbs)) ARR_YRES_EXPARRTIME,
  (select yres.YRES_EXPDEPTIME from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1 and (YRES.YRES_EXPARRTIME)=(select wgbs.wgbs_datevalue from wgbs)) ARR_YRES_EXPDEPTIME,
  (select yres.YRES_FREETEXT1 from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1 and (YRES.YRES_EXPARRTIME)=(select wgbs.wgbs_datevalue from wgbs)) ARR_YRES_FREETEXT1,
  (select yres.YRES_FREETEXT2 from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1 and (YRES.YRES_EXPARRTIME)=(select wgbs.wgbs_datevalue from wgbs)) ARR_YRES_FREETEXT2,
  (select ydet.YDET_ADULTNO from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1 and (YRES.YRES_EXPARRTIME)=(select wgbs.wgbs_datevalue from wgbs)) ARR_YDET_ADULTNO,
  (select xpda.XPDA_SHORTDESC from xpda,xpal where xpda.XPDA_ID=xpal.XPAL_XPDA_ID and xpal.XPAL_XPAC_ID=11 and xpal.XPAL_XCMS_ID=(select yres.YRES_XCMS_ID from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1 and (YRES.YRES_EXPARRTIME)=(select wgbs.wgbs_datevalue from wgbs))) ARR_VIP,
  (case when (decode((select yres.YRES_ID from yres where (select wgbs.wgbs_datevalue from wgbs) between (YRES.YRES_EXPARRTIME) and (YRES.YRES_EXPDEPTIME) and yres.YRES_YRMS_ID=yhrl.YHRL_YRMS_ID and yres.YRES_RESSTATUS<>3 and trunc(yres.YRES_ACTARRTIME)<>(select wgbs.wgbs_datevalue from wgbs) and rownum=1),NULL,0,1)=1 or decode((select yres.YRES_ID from yres,ydet where yres.YRES_CURRENT_YDET_ID=ydet.YDET_ID and ydet.YDET_DATE=(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1 and (YRES.YRES_EXPARRTIME)=(select wgbs.wgbs_datevalue from wgbs)),null,0,1)=1 or decode(decode((select yrst.YRST_ID from yrst where (select wgbs.wgbs_datevalue from wgbs)=yrst.YRST_FROMTIME and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID),NULL,(select yrst.YRST_ID from yrst where ((select wgbs.wgbs_datevalue from wgbs) between yrst.YRST_FROMTIME and yrst.YRST_UNTILTIME-1) and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID and rownum=1),((select yrst.YRST_ID from yrst where (select wgbs.wgbs_datevalue from wgbs)=yrst.YRST_FROMTIME and yrst.YRST_YRMS_ID=yhrl.YHRL_YRMS_ID))),NULL,0,1)=1) then 0 else 1 end) FREEROOM,
  (select min(ydet.YDET_DATE) from ydet,yres where ydet.YDET_YRES_ID=yres.YRES_ID and yres.YRES_RESSTATUS<>3 and yres.YRES_EXPARRTIME>(select wgbs.wgbs_datevalue from wgbs) and ydet.YDET_YRMS_ID=yhrl.YHRL_YRMS_ID) NEXT_ARRIVAL  
From
  yhrl
where
    yhrl.YHRL_DATE=(select wgbs.wgbs_datevalue from wgbs); 

CREATE OR REPLACE VIEW V8_STAT_SHIS_SOURCE_MONTH_CAD
AS 
SELECT 
  WDAT_DATE WDAT_DATE, 
  SOURCE_ID SOURCE_ID, 
  YCAD_ID YCAD_ID,
  sum(NOOFROOMS) NOOFROOMS, 
  sum(ADULTNO) ADULTNO, 
  sum(KIDS) KIDS, 
  sum(ARRIVALS) ARRIVALS, 
  sum(DEPARTURES) DEPARTURES, 
  sum(ARR_ADULTNO) ARR_ADULTNO, 
  sum(DEP_ADULTNO) DEP_ADULTNO, 
  sum(ARR_KIDS) ARR_KIDS, 
  sum(DEP_KIDS) DEP_KIDS, 
  sum(NOSHOW_ROOM) NOSHOW_ROOM, 
  sum(CANCEL_ROOM) CANCEL_ROOM, 
  sum(NETROOMREV) NETROOMREV, 
  sum(NETFBREV) NETFBREV, 
  sum(NETMINIBARREV) NETMINIBARREV, 
  sum(NETTELEREV) NETTELEREV, 
  sum(NETMISCREV) NETMISCREV, 
  sum(NETREV) NETREV, 
  sum(GROSSROOMREV) GROSSROOMREV, 
  sum(GROSSFBREV) GROSSFBREV, 
  sum(GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(GROSSTELEREV) GROSSTELEREV, 
  sum(GROSSMISCREV) GROSSMISCREV, 
  sum(GROSSREV) GROSSREV
FROM 
( 
SELECT 
  wdat.WDAT_DATE WDAT_DATE, 
  yras.YRAS_ID SOURCE_ID, 
  YCAT.YCAT_SUM_ROOMTYPE_YCAD_ID YCAD_ID,
  sum(decode(S.SHIS_YRES_RESSTATUS,3,0,decode(S.SHIS_DEP_ID,1,0,S.SHIS_YDET_NOOFROOMS))) NOOFROOMS,
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN S.SHIS_YDET_ADULTNO WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END )) ADULTNO, 
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN NVL(S.SHIS_TOTALYRGL_NUMBER,0) WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER,0) END )) KIDS, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_NOOFROOMS,0)) ARRIVALS, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_NOOFROOMS,0)) DEPARTURES, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_ADULTNO,0)) ARR_ADULTNO, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_ADULTNO,0)) DEP_ADULTNO, 
  sum(decode(S.SHIS_ARR_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) ARR_KIDS, 
  sum(decode(S.SHIS_DEP_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) DEP_KIDS, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,4,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) NOSHOW_ROOM,
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,3,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) CANCEL_ROOM,
  sum(S.SHIS_NETROOMREV) NETROOMREV, 
  sum(S.SHIS_NETFBREV) NETFBREV, 
  sum(S.SHIS_NETMINIBARREV) NETMINIBARREV, 
  sum(S.SHIS_NETTELEREV) NETTELEREV, 
  sum(S.SHIS_NETMISCREV) NETMISCREV, 
  sum(S.SHIS_NETREV) NETREV, 
  sum(S.SHIS_GROSSROOMREV) GROSSROOMREV, 
  sum(S.SHIS_GROSSFBREV) GROSSFBREV, 
  sum(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(S.SHIS_GROSSTELEREV) GROSSTELEREV, 
  sum(S.SHIS_GROSSMISCREV) GROSSMISCREV, 
 sum(S.SHIS_GROSSREV) GROSSREV 
FROM 
  wdat, 
  shis S, 
  yras,
  ycat
WHERE 
add_months(last_day(wdat.WDAT_DATE),-1)+1<=S.SHIS_WDAT_DATE and 
S.SHIS_WDAT_DATE<=wdat.WDAT_DATE and 
yras.YRAS_ID(+)=S.SHIS_SOURCE_ID and
nvl(nvl(S.SHIS_YDET_YCAT_ID,(select ZFAG.ZFAG_YCAD_ID from zfag,zfac where ZFAG.ZFAG_ID=ZFAC.ZFAC_ZFAG_ID and ZFAC.ZFAC_ID=S.SHIS_ZFAC_ID)),(SELECT YCAD.YCAD_ID FROM YCAD WHERE YCAD.YCAD_DEFAULT=1))=YCAT.YCAT_ID  and
yras.YRAS_YRAC_ID=1
group by wdat.WDAT_DATE,yras.YRAS_ID, YCAT_SUM_ROOMTYPE_YCAD_ID) 
group by WDAT_DATE,SOURCE_ID,YCAD_ID;

CREATE OR REPLACE VIEW V8_STAT_SHIS_SOURCE_YEAR_CAD
AS 
SELECT 
  WDAT_DATE WDAT_DATE, 
  SOURCE_ID SOURCE_ID, 
  YCAD_ID YCAD_ID,
  sum(NOOFROOMS) NOOFROOMS, 
  sum(ADULTNO) ADULTNO, 
  sum(KIDS) KIDS, 
  sum(ARRIVALS) ARRIVALS, 
  sum(DEPARTURES) DEPARTURES, 
  sum(ARR_ADULTNO) ARR_ADULTNO, 
  sum(DEP_ADULTNO) DEP_ADULTNO, 
  sum(ARR_KIDS) ARR_KIDS, 
  sum(DEP_KIDS) DEP_KIDS, 
  sum(NOSHOW_ROOM) NOSHOW_ROOM, 
  sum(CANCEL_ROOM) CANCEL_ROOM, 
  sum(NETROOMREV) NETROOMREV, 
  sum(NETFBREV) NETFBREV, 
  sum(NETMINIBARREV) NETMINIBARREV, 
  sum(NETTELEREV) NETTELEREV, 
  sum(NETMISCREV) NETMISCREV, 
  sum(NETREV) NETREV, 
  sum(GROSSROOMREV) GROSSROOMREV, 
  sum(GROSSFBREV) GROSSFBREV, 
  sum(GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(GROSSTELEREV) GROSSTELEREV, 
  sum(GROSSMISCREV) GROSSMISCREV, 
  sum(GROSSREV) GROSSREV
FROM 
( 
SELECT 
  wdat.WDAT_DATE WDAT_DATE, 
  yras.YRAS_ID SOURCE_ID, 
  YCAT.YCAT_SUM_ROOMTYPE_YCAD_ID YCAD_ID,
  sum(decode(S.SHIS_YRES_RESSTATUS,3,0,decode(S.SHIS_DEP_ID,1,0,S.SHIS_YDET_NOOFROOMS))) NOOFROOMS,
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN S.SHIS_YDET_ADULTNO WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END )) ADULTNO, 
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN NVL(S.SHIS_TOTALYRGL_NUMBER,0) WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER,0) END )) KIDS, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_NOOFROOMS,0)) ARRIVALS, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_NOOFROOMS,0)) DEPARTURES, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_ADULTNO,0)) ARR_ADULTNO, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_ADULTNO,0)) DEP_ADULTNO, 
  sum(decode(S.SHIS_ARR_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) ARR_KIDS, 
  sum(decode(S.SHIS_DEP_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) DEP_KIDS, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,4,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) NOSHOW_ROOM,
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,3,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) CANCEL_ROOM,
  sum(S.SHIS_NETROOMREV) NETROOMREV, 
  sum(S.SHIS_NETFBREV) NETFBREV, 
  sum(S.SHIS_NETMINIBARREV) NETMINIBARREV, 
  sum(S.SHIS_NETTELEREV) NETTELEREV, 
  sum(S.SHIS_NETMISCREV) NETMISCREV, 
  sum(S.SHIS_NETREV) NETREV, 
  sum(S.SHIS_GROSSROOMREV) GROSSROOMREV, 
  sum(S.SHIS_GROSSFBREV) GROSSFBREV, 
  sum(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(S.SHIS_GROSSTELEREV) GROSSTELEREV, 
  sum(S.SHIS_GROSSMISCREV) GROSSMISCREV, 
 sum(S.SHIS_GROSSREV) GROSSREV 
FROM 
  wdat, 
  shis S, 
  yras,
  ycat
WHERE 
(select v8_rep_dates.FI_FIRST_OF_YEAR from v8_rep_dates where v8_rep_dates.FIDELIODATE=wdat.WDAT_DATE)<=S.SHIS_WDAT_DATE and 
S.SHIS_WDAT_DATE<=wdat.WDAT_DATE and 
yras.YRAS_ID(+)=S.SHIS_SOURCE_ID and
nvl(nvl(S.SHIS_YDET_YCAT_ID,(select ZFAG.ZFAG_YCAD_ID from zfag,zfac where ZFAG.ZFAG_ID=ZFAC.ZFAC_ZFAG_ID and ZFAC.ZFAC_ID=S.SHIS_ZFAC_ID)),(SELECT YCAD.YCAD_ID FROM YCAD WHERE YCAD.YCAD_DEFAULT=1))=YCAT.YCAT_ID  and
yras.YRAS_YRAC_ID=1
group by wdat.WDAT_DATE,yras.YRAS_ID, YCAT_SUM_ROOMTYPE_YCAD_ID) 
group by WDAT_DATE,SOURCE_ID,YCAD_ID;

CREATE OR REPLACE VIEW V8_STAT_SHIS_SOURCE_COMBI_CAD
AS 
SELECT 
  WDAT_DATE WDAT_DATE,
  SOURCE_ID SOURCE_ID,
  YCAD_ID YCAD_ID,
  sum(NOOFROOMS_D) NOOFROOMS_D,
  sum(ADULTNO_D) ADULTNO_D,
  sum(KIDS_D) KIDS_D,
  sum(ARRIVALS_D) ARRIVALS_D,
  sum(DEPARTURES_D) DEPARTURES_D,
  sum(ARR_ADULTNO_D) ARR_ADULTNO_D,
  sum(DEP_ADULTNO_D) DEP_ADULTNO_D,
  sum(ARR_KIDS_D) ARR_KIDS_D,
  sum(DEP_KIDS_D) DEP_KIDS_D,
  sum(NOSHOW_ROOM_D) NOSHOW_ROOM_D,
  sum(CANCEL_ROOM_D) CANCEL_ROOM_D, 
  sum(NETROOMREV_D) NETROOMREV_D, 
  sum(NETFBREV_D) NETFBREV_D, 
  sum(NETMINIBARREV_D) NETMINIBARREV_D, 
  sum(NETTELEREV_D) NETTELEREV_D, 
  sum(NETMISCREV_D) NETMISCREV_D, 
  sum(NETREV_D) NETREV_D,
  sum(GROSSROOMREV_D) GROSSROOMREV_D,
  sum(GROSSFBREV_D) GROSSFBREV_D,
  sum(GROSSMINIBARREV_D) GROSSMINIBARREV_D,
  sum(GROSSTELEREV_D) GROSSTELEREV_D,
  sum(GROSSMISCREV_D) GROSSMISCREV_D,
  sum(GROSSREV_D) GROSSREV_D,
  sum(NOOFROOMS_M) NOOFROOMS_M,
  sum(ADULTNO_M) ADULTNO_M,
  sum(KIDS_M) KIDS_M,
  sum(ARRIVALS_M) ARRIVALS_M,
  sum(DEPARTURES_M) DEPARTURES_M,
  sum(ARR_ADULTNO_M) ARR_ADULTNO_M,
  sum(DEP_ADULTNO_M) DEP_ADULTNO_M,
  sum(ARR_KIDS_M) ARR_KIDS_M,
  sum(DEP_KIDS_M) DEP_KIDS_M,
  sum(NOSHOW_ROOM_M) NOSHOW_ROOM_M,
  sum(CANCEL_ROOM_M) CANCEL_ROOM_M, 
  sum(NETROOMREV_M) NETROOMREV_M, 
  sum(NETFBREV_M) NETFBREV_M, 
  sum(NETMINIBARREV_M) NETMINIBARREV_M, 
  sum(NETTELEREV_M) NETTELEREV_M, 
  sum(NETMISCREV_M) NETMISCREV_M, 
  sum(NETREV_M) NETREV_M,
  sum(GROSSROOMREV_M) GROSSROOMREV_M,
  sum(GROSSFBREV_M) GROSSFBREV_M,
  sum(GROSSMINIBARREV_M) GROSSMINIBARREV_M,
  sum(GROSSTELEREV_M) GROSSTELEREV_M,
  sum(GROSSMISCREV_M) GROSSMISCREV_M,
  sum(GROSSREV_M) GROSSREV_M,
  sum(NOOFROOMS_Y) NOOFROOMS_Y,
  sum(ADULTNO_Y) ADULTNO_Y,
  sum(KIDS_Y) KIDS_Y,
  sum(ARRIVALS_Y) ARRIVALS_Y,
  sum(DEPARTURES_Y) DEPARTURES_Y,
  sum(ARR_ADULTNO_Y) ARR_ADULTNO_Y,
  sum(DEP_ADULTNO_Y) DEP_ADULTNO_Y,
  sum(ARR_KIDS_Y) ARR_KIDS_Y,
  sum(DEP_KIDS_Y) DEP_KIDS_Y,
  sum(NOSHOW_ROOM_Y) NOSHOW_ROOM_Y,
  sum(CANCEL_ROOM_Y) CANCEL_ROOM_Y, 
  sum(NETROOMREV_Y) NETROOMREV_Y, 
  sum(NETFBREV_Y) NETFBREV_Y, 
  sum(NETMINIBARREV_Y) NETMINIBARREV_Y, 
  sum(NETTELEREV_Y) NETTELEREV_Y, 
  sum(NETMISCREV_Y) NETMISCREV_Y, 
  sum(NETREV_Y) NETREV_Y,
  sum(GROSSROOMREV_Y) GROSSROOMREV_Y,
  sum(GROSSFBREV_Y) GROSSFBREV_Y,
  sum(GROSSMINIBARREV_Y) GROSSMINIBARREV_Y,
  sum(GROSSTELEREV_Y) GROSSTELEREV_Y,
  sum(GROSSMISCREV_Y) GROSSMISCREV_Y,
  sum(GROSSREV_Y) GROSSREV_Y
FROM (
SELECT WDAT_DATE, SOURCE_ID, YCAD_ID,
NOOFROOMS NOOFROOMS_D, ADULTNO ADULTNO_D, KIDS KIDS_D, ARRIVALS ARRIVALS_D, DEPARTURES DEPARTURES_D, ARR_ADULTNO ARR_ADULTNO_D, 
DEP_ADULTNO DEP_ADULTNO_D, ARR_KIDS ARR_KIDS_D, DEP_KIDS DEP_KIDS_D, NOSHOW_ROOM NOSHOW_ROOM_D, CANCEL_ROOM CANCEL_ROOM_D,
NETROOMREV NETROOMREV_D, NETFBREV NETFBREV_D, NETMINIBARREV NETMINIBARREV_D, NETTELEREV NETTELEREV_D, NETMISCREV NETMISCREV_D, 
NETREV NETREV_D, GROSSROOMREV GROSSROOMREV_D, GROSSFBREV GROSSFBREV_D, GROSSMINIBARREV GROSSMINIBARREV_D, 
GROSSTELEREV GROSSTELEREV_D, GROSSMISCREV GROSSMISCREV_D, GROSSREV GROSSREV_D,
0 NOOFROOMS_M, 0 ADULTNO_M, 0 KIDS_M, 0 ARRIVALS_M, 0 DEPARTURES_M, 0 ARR_ADULTNO_M, 
0 DEP_ADULTNO_M, 0 ARR_KIDS_M, 0 DEP_KIDS_M, 0 NOSHOW_ROOM_M, 0 CANCEL_ROOM_M,
0 NETROOMREV_M, 0 NETFBREV_M, 0 NETMINIBARREV_M, 0 NETTELEREV_M, 0 NETMISCREV_M, 
0 NETREV_M, 0 GROSSROOMREV_M, 0 GROSSFBREV_M, 0 GROSSMINIBARREV_M, 
0 GROSSTELEREV_M, 0 GROSSMISCREV_M, 0 GROSSREV_M,
0 NOOFROOMS_Y, 0 ADULTNO_Y, 0 KIDS_Y, 0 ARRIVALS_Y, 0 DEPARTURES_Y, 0 ARR_ADULTNO_Y, 
0 DEP_ADULTNO_Y, 0 ARR_KIDS_Y, 0 DEP_KIDS_Y, 0 NOSHOW_ROOM_Y, 0 CANCEL_ROOM_Y,
0 NETROOMREV_Y, 0 NETFBREV_Y, 0 NETMINIBARREV_Y, 0 NETTELEREV_Y, 0 NETMISCREV_Y, 
0 NETREV_Y, 0 GROSSROOMREV_Y, 0 GROSSFBREV_Y, 0 GROSSMINIBARREV_Y, 
0 GROSSTELEREV_Y, 0 GROSSMISCREV_Y, 0 GROSSREV_Y
FROM V8_STAT_SHIS_SOURCE_DAY_CAD
UNION
SELECT WDAT_DATE, SOURCE_ID, YCAD_ID,
0 NOOFROOMS_D, 0 ADULTNO_D, 0 KIDS_D, 0 ARRIVALS_D, 0 DEPARTURES_D, 0 ARR_ADULTNO_D, 
0 DEP_ADULTNO_D, 0 ARR_KIDS_D, 0 DEP_KIDS_D, 0 NOSHOW_ROOM_D, 0 CANCEL_ROOM_D,
0 NETROOMREV_D, 0 NETFBREV_D, 0 NETMINIBARREV_D, 0 NETTELEREV_D, 0 NETMISCREV_D, 
0 NETREV_D, 0 GROSSROOMREV_D, 0 GROSSFBREV_D, 0 GROSSMINIBARREV_D, 
0 GROSSTELEREV_D, 0 GROSSMISCREV_D, 0 GROSSREV_D,
NOOFROOMS NOOFROOMS_M, ADULTNO ADULTNO_M, KIDS KIDS_M, ARRIVALS ARRIVALS_M, DEPARTURES DEPARTURES_M, ARR_ADULTNO ARR_ADULTNO_M, 
DEP_ADULTNO DEP_ADULTNO_M, ARR_KIDS ARR_KIDS_M, DEP_KIDS DEP_KIDS_M, NOSHOW_ROOM NOSHOW_ROOM_M, CANCEL_ROOM CANCEL_ROOM_M,
NETROOMREV NETROOMREV_M, NETFBREV NETFBREV_M, NETMINIBARREV NETMINIBARREV_M, NETTELEREV NETTELEREV_M, NETMISCREV NETMISCREV_M, 
NETREV NETREV_M, GROSSROOMREV GROSSROOMREV_M, GROSSFBREV GROSSFBREV_M, GROSSMINIBARREV GROSSMINIBARREV_M, 
GROSSTELEREV GROSSTELEREV_M, GROSSMISCREV GROSSMISCREV_M, GROSSREV GROSSREV_M,
0 NOOFROOMS_Y, 0 ADULTNO_Y, 0 KIDS_Y, 0 ARRIVALS_Y, 0 DEPARTURES_Y, 0 ARR_ADULTNO_Y, 
0 DEP_ADULTNO_Y, 0 ARR_KIDS_Y, 0 DEP_KIDS_Y, 0 NOSHOW_ROOM_Y, 0 CANCEL_ROOM_Y,
0 NETROOMREV_Y, 0 NETFBREV_Y, 0 NETMINIBARREV_Y, 0 NETTELEREV_Y, 0 NETMISCREV_Y, 
0 NETREV_Y, 0 GROSSROOMREV_Y, 0 GROSSFBREV_Y, 0 GROSSMINIBARREV_Y, 
0 GROSSTELEREV_Y, 0 GROSSMISCREV_Y, 0 GROSSREV_Y
FROM V8_STAT_SHIS_SOURCE_MONTH_CAD
UNION
SELECT WDAT_DATE, SOURCE_ID, YCAD_ID,
0 NOOFROOMS_D, 0 ADULTNO_D, 0 KIDS_D, 0 ARRIVALS_D, 0 DEPARTURES_D, 0 ARR_ADULTNO_D, 
0 DEP_ADULTNO_D, 0 ARR_KIDS_D, 0 DEP_KIDS_D, 0 NOSHOW_ROOM_D, 0 CANCEL_ROOM_D,
0 NETROOMREV_D, 0 NETFBREV_D, 0 NETMINIBARREV_D, 0 NETTELEREV_D, 0 NETMISCREV_D, 
0 NETREV_D, 0 GROSSROOMREV_D, 0 GROSSFBREV_D, 0 GROSSMINIBARREV_D, 
0 GROSSTELEREV_D, 0 GROSSMISCREV_D, 0 GROSSREV_D,
0 NOOFROOMS_M, 0 ADULTNO_M, 0 KIDS_M, 0 ARRIVALS_M, 0 DEPARTURES_M, 0 ARR_ADULTNO_M, 
0 DEP_ADULTNO_M, 0 ARR_KIDS_M, 0 DEP_KIDS_M, 0 NOSHOW_ROOM_M, 0 CANCEL_ROOM_M,
0 NETROOMREV_M, 0 NETFBREV_M, 0 NETMINIBARREV_M, 0 NETTELEREV_M, 0 NETMISCREV_M, 
0 NETREV_M, 0 GROSSROOMREV_M, 0 GROSSFBREV_M, 0 GROSSMINIBARREV_M, 
0 GROSSTELEREV_M, 0 GROSSMISCREV_M, 0 GROSSREV_M,
NOOFROOMS NOOFROOMS_Y, ADULTNO ADULTNO_Y, KIDS KIDS_Y, ARRIVALS ARRIVALS_Y, DEPARTURES DEPARTURES_Y, ARR_ADULTNO ARR_ADULTNO_Y, 
DEP_ADULTNO DEP_ADULTNO_Y, ARR_KIDS ARR_KIDS_Y, DEP_KIDS DEP_KIDS_Y, NOSHOW_ROOM NOSHOW_ROOM_Y, CANCEL_ROOM CANCEL_ROOM_Y,
NETROOMREV NETROOMREV_Y, NETFBREV NETFBREV_Y, NETMINIBARREV NETMINIBARREV_Y, NETTELEREV NETTELEREV_Y, NETMISCREV NETMISCREV_Y, 
NETREV NETREV_Y, GROSSROOMREV GROSSROOMREV_Y, GROSSFBREV GROSSFBREV_Y, GROSSMINIBARREV GROSSMINIBARREV_Y, 
GROSSTELEREV GROSSTELEREV_Y, GROSSMISCREV GROSSMISCREV_Y, GROSSREV GROSSREV_Y
FROM V8_STAT_SHIS_SOURCE_YEAR_CAD)
GROUP BY WDAT_DATE, SOURCE_ID, YCAD_ID;

CREATE OR REPLACE VIEW V8_STAT_SHIS_CHANNEL_DAY_CAD
AS 
SELECT 
  WDAT_DATE WDAT_DATE, 
  CHANNEL_ID CHANNEL_ID, 
  YCAD_ID YCAD_ID,
  sum(NOOFROOMS) NOOFROOMS, 
  sum(ADULTNO) ADULTNO, 
  sum(KIDS) KIDS, 
  sum(ARRIVALS) ARRIVALS, 
  sum(DEPARTURES) DEPARTURES, 
  sum(ARR_ADULTNO)ARR_ADULTNO, 
  sum(DEP_ADULTNO) DEP_ADULTNO, 
  sum(ARR_KIDS)ARR_KIDS, 
  sum(DEP_KIDS)DEP_KIDS, 
  sum(NOSHOW_ROOM)NOSHOW_ROOM, 
  sum(CANCEL_ROOM)CANCEL_ROOM, 
  sum(NETROOMREV)NETROOMREV, 
  sum(NETFBREV)NETFBREV, 
  sum(NETMINIBARREV)NETMINIBARREV, 
  sum(NETTELEREV)NETTELEREV, 
  sum(NETMISCREV)NETMISCREV, 
  sum(NETREV)NETREV, 
  sum(GROSSROOMREV)GROSSROOMREV, 
  sum(GROSSFBREV)GROSSFBREV, 
  sum(GROSSMINIBARREV)GROSSMINIBARREV, 
  sum(GROSSTELEREV)GROSSTELEREV, 
  sum(GROSSMISCREV)GROSSMISCREV, 
  sum(GROSSREV)GROSSREV 
FROM 
( 
SELECT 
  wdat.WDAT_DATE WDAT_DATE, 
  yras.YRAS_ID CHANNEL_ID, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,0,decode(S.SHIS_DEP_ID,1,0,S.SHIS_YDET_NOOFROOMS))) NOOFROOMS,
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN S.SHIS_YDET_ADULTNO WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END )) ADULTNO, 
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN NVL(S.SHIS_TOTALYRGL_NUMBER,0) WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER,0) END )) KIDS, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_NOOFROOMS,0)) ARRIVALS, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_NOOFROOMS,0)) DEPARTURES, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_ADULTNO,0)) ARR_ADULTNO, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_ADULTNO,0)) DEP_ADULTNO, 
  sum(decode(S.SHIS_ARR_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) ARR_KIDS, 
  sum(decode(S.SHIS_DEP_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) DEP_KIDS, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,4,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) NOSHOW_ROOM,
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,3,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) CANCEL_ROOM,
  sum(S.SHIS_NETROOMREV) NETROOMREV, 
  sum(S.SHIS_NETFBREV) NETFBREV, 
  sum(S.SHIS_NETMINIBARREV) NETMINIBARREV, 
  sum(S.SHIS_NETTELEREV) NETTELEREV, 
  sum(S.SHIS_NETMISCREV) NETMISCREV, 
  sum(S.SHIS_NETREV) NETREV, 
  sum(S.SHIS_GROSSROOMREV) GROSSROOMREV, 
  sum(S.SHIS_GROSSFBREV) GROSSFBREV, 
  sum(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(S.SHIS_GROSSTELEREV) GROSSTELEREV, 
  sum(S.SHIS_GROSSMISCREV) GROSSMISCREV, 
 sum(S.SHIS_GROSSREV) GROSSREV ,
 YCAT.YCAT_SUM_ROOMTYPE_YCAD_ID YCAD_ID
FROM 
  wdat, 
  shis S, 
  yras ,
  ycat
WHERE 
wdat.WDAT_DATE=S.SHIS_WDAT_DATE and 
yras.YRAS_ID(+)=S.SHIS_CHANNEL_ID and
nvl(nvl(S.SHIS_YDET_YCAT_ID,(select ZFAG.ZFAG_YCAD_ID from zfag,zfac where ZFAG.ZFAG_ID=ZFAC.ZFAC_ZFAG_ID and ZFAC.ZFAC_ID=S.SHIS_ZFAC_ID)),(SELECT YCAD.YCAD_ID FROM YCAD WHERE YCAD.YCAD_DEFAULT=1))=YCAT.YCAT_ID  
and yras.YRAS_YRAC_ID=2
group by wdat.WDAT_DATE,yras.YRAS_ID, YCAT_SUM_ROOMTYPE_YCAD_ID) 
group by WDAT_DATE,CHANNEL_ID,YCAD_ID;

CREATE OR REPLACE VIEW V8_STAT_SHIS_CHANNEL_MONTH_CAD
AS 
SELECT 
  WDAT_DATE WDAT_DATE, 
  CHANNEL_ID CHANNEL_ID, 
  YCAD_ID YCAD_ID,
  sum(NOOFROOMS) NOOFROOMS, 
  sum(ADULTNO) ADULTNO, 
  sum(KIDS) KIDS, 
  sum(ARRIVALS) ARRIVALS, 
  sum(DEPARTURES) DEPARTURES, 
  sum(ARR_ADULTNO) ARR_ADULTNO, 
  sum(DEP_ADULTNO) DEP_ADULTNO, 
  sum(ARR_KIDS) ARR_KIDS, 
  sum(DEP_KIDS) DEP_KIDS, 
  sum(NOSHOW_ROOM) NOSHOW_ROOM, 
  sum(CANCEL_ROOM) CANCEL_ROOM, 
  sum(NETROOMREV) NETROOMREV, 
  sum(NETFBREV) NETFBREV, 
  sum(NETMINIBARREV) NETMINIBARREV, 
  sum(NETTELEREV) NETTELEREV, 
  sum(NETMISCREV) NETMISCREV, 
  sum(NETREV) NETREV, 
  sum(GROSSROOMREV) GROSSROOMREV, 
  sum(GROSSFBREV) GROSSFBREV, 
  sum(GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(GROSSTELEREV) GROSSTELEREV, 
  sum(GROSSMISCREV) GROSSMISCREV, 
  sum(GROSSREV) GROSSREV
FROM 
( 
SELECT 
  wdat.WDAT_DATE WDAT_DATE, 
  yras.YRAS_ID CHANNEL_ID, 
  YCAT.YCAT_SUM_ROOMTYPE_YCAD_ID YCAD_ID,
  sum(decode(S.SHIS_YRES_RESSTATUS,3,0,decode(S.SHIS_DEP_ID,1,0,S.SHIS_YDET_NOOFROOMS))) NOOFROOMS,
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN S.SHIS_YDET_ADULTNO WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END )) ADULTNO, 
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN NVL(S.SHIS_TOTALYRGL_NUMBER,0) WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER,0) END )) KIDS, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_NOOFROOMS,0)) ARRIVALS, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_NOOFROOMS,0)) DEPARTURES, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_ADULTNO,0)) ARR_ADULTNO, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_ADULTNO,0)) DEP_ADULTNO, 
  sum(decode(S.SHIS_ARR_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) ARR_KIDS, 
  sum(decode(S.SHIS_DEP_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) DEP_KIDS, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,4,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) NOSHOW_ROOM,
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,3,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) CANCEL_ROOM,
  sum(S.SHIS_NETROOMREV) NETROOMREV, 
  sum(S.SHIS_NETFBREV) NETFBREV, 
  sum(S.SHIS_NETMINIBARREV) NETMINIBARREV, 
  sum(S.SHIS_NETTELEREV) NETTELEREV, 
  sum(S.SHIS_NETMISCREV) NETMISCREV, 
  sum(S.SHIS_NETREV) NETREV, 
  sum(S.SHIS_GROSSROOMREV) GROSSROOMREV, 
  sum(S.SHIS_GROSSFBREV) GROSSFBREV, 
  sum(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(S.SHIS_GROSSTELEREV) GROSSTELEREV, 
  sum(S.SHIS_GROSSMISCREV) GROSSMISCREV, 
 sum(S.SHIS_GROSSREV) GROSSREV 
FROM 
  wdat, 
  shis S, 
  yras,
  ycat
WHERE 
add_months(last_day(wdat.WDAT_DATE),-1)+1<=S.SHIS_WDAT_DATE and 
S.SHIS_WDAT_DATE<=wdat.WDAT_DATE and 
yras.YRAS_ID(+)=S.SHIS_CHANNEL_ID and
nvl(nvl(S.SHIS_YDET_YCAT_ID,(select ZFAG.ZFAG_YCAD_ID from zfag,zfac where ZFAG.ZFAG_ID=ZFAC.ZFAC_ZFAG_ID and ZFAC.ZFAC_ID=S.SHIS_ZFAC_ID)),(SELECT YCAD.YCAD_ID FROM YCAD WHERE YCAD.YCAD_DEFAULT=1))=YCAT.YCAT_ID  and
yras.YRAS_YRAC_ID=2
group by wdat.WDAT_DATE,yras.YRAS_ID, YCAT_SUM_ROOMTYPE_YCAD_ID) 
group by WDAT_DATE,CHANNEL_ID,YCAD_ID;

CREATE OR REPLACE VIEW V8_STAT_SHIS_CHANNEL_YEAR_CAD
AS 
SELECT 
  WDAT_DATE WDAT_DATE, 
  CHANNEL_ID CHANNEL_ID, 
  YCAD_ID YCAD_ID,
  sum(NOOFROOMS) NOOFROOMS, 
  sum(ADULTNO) ADULTNO, 
  sum(KIDS) KIDS, 
  sum(ARRIVALS) ARRIVALS, 
  sum(DEPARTURES) DEPARTURES, 
  sum(ARR_ADULTNO) ARR_ADULTNO, 
  sum(DEP_ADULTNO) DEP_ADULTNO, 
  sum(ARR_KIDS) ARR_KIDS, 
  sum(DEP_KIDS) DEP_KIDS, 
  sum(NOSHOW_ROOM) NOSHOW_ROOM, 
  sum(CANCEL_ROOM) CANCEL_ROOM, 
  sum(NETROOMREV) NETROOMREV, 
  sum(NETFBREV) NETFBREV, 
  sum(NETMINIBARREV) NETMINIBARREV, 
  sum(NETTELEREV) NETTELEREV, 
  sum(NETMISCREV) NETMISCREV, 
  sum(NETREV) NETREV, 
  sum(GROSSROOMREV) GROSSROOMREV, 
  sum(GROSSFBREV) GROSSFBREV, 
  sum(GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(GROSSTELEREV) GROSSTELEREV, 
  sum(GROSSMISCREV) GROSSMISCREV, 
  sum(GROSSREV) GROSSREV
FROM 
( 
SELECT 
  wdat.WDAT_DATE WDAT_DATE, 
  yras.YRAS_ID CHANNEL_ID, 
  YCAT.YCAT_SUM_ROOMTYPE_YCAD_ID YCAD_ID,
  sum(decode(S.SHIS_YRES_RESSTATUS,3,0,decode(S.SHIS_DEP_ID,1,0,S.SHIS_YDET_NOOFROOMS))) NOOFROOMS,
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN S.SHIS_YDET_ADULTNO WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END )) ADULTNO, 
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN NVL(S.SHIS_TOTALYRGL_NUMBER,0) WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER,0) END )) KIDS, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_NOOFROOMS,0)) ARRIVALS, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_NOOFROOMS,0)) DEPARTURES, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_ADULTNO,0)) ARR_ADULTNO, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_ADULTNO,0)) DEP_ADULTNO, 
  sum(decode(S.SHIS_ARR_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) ARR_KIDS, 
  sum(decode(S.SHIS_DEP_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) DEP_KIDS, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,4,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) NOSHOW_ROOM,
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,3,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) CANCEL_ROOM,
  sum(S.SHIS_NETROOMREV) NETROOMREV, 
  sum(S.SHIS_NETFBREV) NETFBREV, 
  sum(S.SHIS_NETMINIBARREV) NETMINIBARREV, 
  sum(S.SHIS_NETTELEREV) NETTELEREV, 
  sum(S.SHIS_NETMISCREV) NETMISCREV, 
  sum(S.SHIS_NETREV) NETREV, 
  sum(S.SHIS_GROSSROOMREV) GROSSROOMREV, 
  sum(S.SHIS_GROSSFBREV) GROSSFBREV, 
  sum(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(S.SHIS_GROSSTELEREV) GROSSTELEREV, 
  sum(S.SHIS_GROSSMISCREV) GROSSMISCREV, 
 sum(S.SHIS_GROSSREV) GROSSREV 
FROM 
  wdat, 
  shis S, 
  yras,
  ycat
WHERE 
(select v8_rep_dates.FI_FIRST_OF_YEAR from v8_rep_dates where v8_rep_dates.FIDELIODATE=wdat.WDAT_DATE)<=S.SHIS_WDAT_DATE and 
S.SHIS_WDAT_DATE<=wdat.WDAT_DATE and 
yras.YRAS_ID(+)=S.SHIS_CHANNEL_ID and
nvl(nvl(S.SHIS_YDET_YCAT_ID,(select ZFAG.ZFAG_YCAD_ID from zfag,zfac where ZFAG.ZFAG_ID=ZFAC.ZFAC_ZFAG_ID and ZFAC.ZFAC_ID=S.SHIS_ZFAC_ID)),(SELECT YCAD.YCAD_ID FROM YCAD WHERE YCAD.YCAD_DEFAULT=1))=YCAT.YCAT_ID  and
yras.YRAS_YRAC_ID=2
group by wdat.WDAT_DATE,yras.YRAS_ID, YCAT_SUM_ROOMTYPE_YCAD_ID) 
group by WDAT_DATE,CHANNEL_ID,YCAD_ID;

CREATE OR REPLACE VIEW V8_STAT_SHIS_CHANNEL_COMBI_CAD
AS 
SELECT 
  WDAT_DATE WDAT_DATE,
  CHANNEL_ID CHANNEL_ID,
  YCAD_ID YCAD_ID,
  sum(NOOFROOMS_D) NOOFROOMS_D,
  sum(ADULTNO_D) ADULTNO_D,
  sum(KIDS_D) KIDS_D,
  sum(ARRIVALS_D) ARRIVALS_D,
  sum(DEPARTURES_D) DEPARTURES_D,
  sum(ARR_ADULTNO_D) ARR_ADULTNO_D,
  sum(DEP_ADULTNO_D) DEP_ADULTNO_D,
  sum(ARR_KIDS_D) ARR_KIDS_D,
  sum(DEP_KIDS_D) DEP_KIDS_D,
  sum(NOSHOW_ROOM_D) NOSHOW_ROOM_D,
  sum(CANCEL_ROOM_D) CANCEL_ROOM_D, 
  sum(NETROOMREV_D) NETROOMREV_D, 
  sum(NETFBREV_D) NETFBREV_D, 
  sum(NETMINIBARREV_D) NETMINIBARREV_D, 
  sum(NETTELEREV_D) NETTELEREV_D, 
  sum(NETMISCREV_D) NETMISCREV_D, 
  sum(NETREV_D) NETREV_D,
  sum(GROSSROOMREV_D) GROSSROOMREV_D,
  sum(GROSSFBREV_D) GROSSFBREV_D,
  sum(GROSSMINIBARREV_D) GROSSMINIBARREV_D,
  sum(GROSSTELEREV_D) GROSSTELEREV_D,
  sum(GROSSMISCREV_D) GROSSMISCREV_D,
  sum(GROSSREV_D) GROSSREV_D,
  sum(NOOFROOMS_M) NOOFROOMS_M,
  sum(ADULTNO_M) ADULTNO_M,
  sum(KIDS_M) KIDS_M,
  sum(ARRIVALS_M) ARRIVALS_M,
  sum(DEPARTURES_M) DEPARTURES_M,
  sum(ARR_ADULTNO_M) ARR_ADULTNO_M,
  sum(DEP_ADULTNO_M) DEP_ADULTNO_M,
  sum(ARR_KIDS_M) ARR_KIDS_M,
  sum(DEP_KIDS_M) DEP_KIDS_M,
  sum(NOSHOW_ROOM_M) NOSHOW_ROOM_M,
  sum(CANCEL_ROOM_M) CANCEL_ROOM_M, 
  sum(NETROOMREV_M) NETROOMREV_M, 
  sum(NETFBREV_M) NETFBREV_M, 
  sum(NETMINIBARREV_M) NETMINIBARREV_M, 
  sum(NETTELEREV_M) NETTELEREV_M, 
  sum(NETMISCREV_M) NETMISCREV_M, 
  sum(NETREV_M) NETREV_M,
  sum(GROSSROOMREV_M) GROSSROOMREV_M,
  sum(GROSSFBREV_M) GROSSFBREV_M,
  sum(GROSSMINIBARREV_M) GROSSMINIBARREV_M,
  sum(GROSSTELEREV_M) GROSSTELEREV_M,
  sum(GROSSMISCREV_M) GROSSMISCREV_M,
  sum(GROSSREV_M) GROSSREV_M,
  sum(NOOFROOMS_Y) NOOFROOMS_Y,
  sum(ADULTNO_Y) ADULTNO_Y,
  sum(KIDS_Y) KIDS_Y,
  sum(ARRIVALS_Y) ARRIVALS_Y,
  sum(DEPARTURES_Y) DEPARTURES_Y,
  sum(ARR_ADULTNO_Y) ARR_ADULTNO_Y,
  sum(DEP_ADULTNO_Y) DEP_ADULTNO_Y,
  sum(ARR_KIDS_Y) ARR_KIDS_Y,
  sum(DEP_KIDS_Y) DEP_KIDS_Y,
  sum(NOSHOW_ROOM_Y) NOSHOW_ROOM_Y,
  sum(CANCEL_ROOM_Y) CANCEL_ROOM_Y, 
  sum(NETROOMREV_Y) NETROOMREV_Y, 
  sum(NETFBREV_Y) NETFBREV_Y, 
  sum(NETMINIBARREV_Y) NETMINIBARREV_Y, 
  sum(NETTELEREV_Y) NETTELEREV_Y, 
  sum(NETMISCREV_Y) NETMISCREV_Y, 
  sum(NETREV_Y) NETREV_Y,
  sum(GROSSROOMREV_Y) GROSSROOMREV_Y,
  sum(GROSSFBREV_Y) GROSSFBREV_Y,
  sum(GROSSMINIBARREV_Y) GROSSMINIBARREV_Y,
  sum(GROSSTELEREV_Y) GROSSTELEREV_Y,
  sum(GROSSMISCREV_Y) GROSSMISCREV_Y,
  sum(GROSSREV_Y) GROSSREV_Y
FROM (
SELECT WDAT_DATE, CHANNEL_ID, YCAD_ID,
NOOFROOMS NOOFROOMS_D, ADULTNO ADULTNO_D, KIDS KIDS_D, ARRIVALS ARRIVALS_D, DEPARTURES DEPARTURES_D, ARR_ADULTNO ARR_ADULTNO_D, 
DEP_ADULTNO DEP_ADULTNO_D, ARR_KIDS ARR_KIDS_D, DEP_KIDS DEP_KIDS_D, NOSHOW_ROOM NOSHOW_ROOM_D, CANCEL_ROOM CANCEL_ROOM_D,
NETROOMREV NETROOMREV_D, NETFBREV NETFBREV_D, NETMINIBARREV NETMINIBARREV_D, NETTELEREV NETTELEREV_D, NETMISCREV NETMISCREV_D, 
NETREV NETREV_D, GROSSROOMREV GROSSROOMREV_D, GROSSFBREV GROSSFBREV_D, GROSSMINIBARREV GROSSMINIBARREV_D, 
GROSSTELEREV GROSSTELEREV_D, GROSSMISCREV GROSSMISCREV_D, GROSSREV GROSSREV_D,
0 NOOFROOMS_M, 0 ADULTNO_M, 0 KIDS_M, 0 ARRIVALS_M, 0 DEPARTURES_M, 0 ARR_ADULTNO_M, 
0 DEP_ADULTNO_M, 0 ARR_KIDS_M, 0 DEP_KIDS_M, 0 NOSHOW_ROOM_M, 0 CANCEL_ROOM_M,
0 NETROOMREV_M, 0 NETFBREV_M, 0 NETMINIBARREV_M, 0 NETTELEREV_M, 0 NETMISCREV_M, 
0 NETREV_M, 0 GROSSROOMREV_M, 0 GROSSFBREV_M, 0 GROSSMINIBARREV_M, 
0 GROSSTELEREV_M, 0 GROSSMISCREV_M, 0 GROSSREV_M,
0 NOOFROOMS_Y, 0 ADULTNO_Y, 0 KIDS_Y, 0 ARRIVALS_Y, 0 DEPARTURES_Y, 0 ARR_ADULTNO_Y, 
0 DEP_ADULTNO_Y, 0 ARR_KIDS_Y, 0 DEP_KIDS_Y, 0 NOSHOW_ROOM_Y, 0 CANCEL_ROOM_Y,
0 NETROOMREV_Y, 0 NETFBREV_Y, 0 NETMINIBARREV_Y, 0 NETTELEREV_Y, 0 NETMISCREV_Y, 
0 NETREV_Y, 0 GROSSROOMREV_Y, 0 GROSSFBREV_Y, 0 GROSSMINIBARREV_Y, 
0 GROSSTELEREV_Y, 0 GROSSMISCREV_Y, 0 GROSSREV_Y
FROM V8_STAT_SHIS_CHANNEL_DAY_CAD
UNION
SELECT WDAT_DATE, CHANNEL_ID, YCAD_ID,
0 NOOFROOMS_D, 0 ADULTNO_D, 0 KIDS_D, 0 ARRIVALS_D, 0 DEPARTURES_D, 0 ARR_ADULTNO_D, 
0 DEP_ADULTNO_D, 0 ARR_KIDS_D, 0 DEP_KIDS_D, 0 NOSHOW_ROOM_D, 0 CANCEL_ROOM_D,
0 NETROOMREV_D, 0 NETFBREV_D, 0 NETMINIBARREV_D, 0 NETTELEREV_D, 0 NETMISCREV_D, 
0 NETREV_D, 0 GROSSROOMREV_D, 0 GROSSFBREV_D, 0 GROSSMINIBARREV_D, 
0 GROSSTELEREV_D, 0 GROSSMISCREV_D, 0 GROSSREV_D,
NOOFROOMS NOOFROOMS_M, ADULTNO ADULTNO_M, KIDS KIDS_M, ARRIVALS ARRIVALS_M, DEPARTURES DEPARTURES_M, ARR_ADULTNO ARR_ADULTNO_M, 
DEP_ADULTNO DEP_ADULTNO_M, ARR_KIDS ARR_KIDS_M, DEP_KIDS DEP_KIDS_M, NOSHOW_ROOM NOSHOW_ROOM_M, CANCEL_ROOM CANCEL_ROOM_M,
NETROOMREV NETROOMREV_M, NETFBREV NETFBREV_M, NETMINIBARREV NETMINIBARREV_M, NETTELEREV NETTELEREV_M, NETMISCREV NETMISCREV_M, 
NETREV NETREV_M, GROSSROOMREV GROSSROOMREV_M, GROSSFBREV GROSSFBREV_M, GROSSMINIBARREV GROSSMINIBARREV_M, 
GROSSTELEREV GROSSTELEREV_M, GROSSMISCREV GROSSMISCREV_M, GROSSREV GROSSREV_M,
0 NOOFROOMS_Y, 0 ADULTNO_Y, 0 KIDS_Y, 0 ARRIVALS_Y, 0 DEPARTURES_Y, 0 ARR_ADULTNO_Y, 
0 DEP_ADULTNO_Y, 0 ARR_KIDS_Y, 0 DEP_KIDS_Y, 0 NOSHOW_ROOM_Y, 0 CANCEL_ROOM_Y,
0 NETROOMREV_Y, 0 NETFBREV_Y, 0 NETMINIBARREV_Y, 0 NETTELEREV_Y, 0 NETMISCREV_Y, 
0 NETREV_Y, 0 GROSSROOMREV_Y, 0 GROSSFBREV_Y, 0 GROSSMINIBARREV_Y, 
0 GROSSTELEREV_Y, 0 GROSSMISCREV_Y, 0 GROSSREV_Y
FROM V8_STAT_SHIS_CHANNEL_MONTH_CAD
UNION
SELECT WDAT_DATE, CHANNEL_ID, YCAD_ID,
0 NOOFROOMS_D, 0 ADULTNO_D, 0 KIDS_D, 0 ARRIVALS_D, 0 DEPARTURES_D, 0 ARR_ADULTNO_D, 
0 DEP_ADULTNO_D, 0 ARR_KIDS_D, 0 DEP_KIDS_D, 0 NOSHOW_ROOM_D, 0 CANCEL_ROOM_D,
0 NETROOMREV_D, 0 NETFBREV_D, 0 NETMINIBARREV_D, 0 NETTELEREV_D, 0 NETMISCREV_D, 
0 NETREV_D, 0 GROSSROOMREV_D, 0 GROSSFBREV_D, 0 GROSSMINIBARREV_D, 
0 GROSSTELEREV_D, 0 GROSSMISCREV_D, 0 GROSSREV_D,
0 NOOFROOMS_M, 0 ADULTNO_M, 0 KIDS_M, 0 ARRIVALS_M, 0 DEPARTURES_M, 0 ARR_ADULTNO_M, 
0 DEP_ADULTNO_M, 0 ARR_KIDS_M, 0 DEP_KIDS_M, 0 NOSHOW_ROOM_M, 0 CANCEL_ROOM_M,
0 NETROOMREV_M, 0 NETFBREV_M, 0 NETMINIBARREV_M, 0 NETTELEREV_M, 0 NETMISCREV_M, 
0 NETREV_M, 0 GROSSROOMREV_M, 0 GROSSFBREV_M, 0 GROSSMINIBARREV_M, 
0 GROSSTELEREV_M, 0 GROSSMISCREV_M, 0 GROSSREV_M,
NOOFROOMS NOOFROOMS_Y, ADULTNO ADULTNO_Y, KIDS KIDS_Y, ARRIVALS ARRIVALS_Y, DEPARTURES DEPARTURES_Y, ARR_ADULTNO ARR_ADULTNO_Y, 
DEP_ADULTNO DEP_ADULTNO_Y, ARR_KIDS ARR_KIDS_Y, DEP_KIDS DEP_KIDS_Y, NOSHOW_ROOM NOSHOW_ROOM_Y, CANCEL_ROOM CANCEL_ROOM_Y,
NETROOMREV NETROOMREV_Y, NETFBREV NETFBREV_Y, NETMINIBARREV NETMINIBARREV_Y, NETTELEREV NETTELEREV_Y, NETMISCREV NETMISCREV_Y, 
NETREV NETREV_Y, GROSSROOMREV GROSSROOMREV_Y, GROSSFBREV GROSSFBREV_Y, GROSSMINIBARREV GROSSMINIBARREV_Y, 
GROSSTELEREV GROSSTELEREV_Y, GROSSMISCREV GROSSMISCREV_Y, GROSSREV GROSSREV_Y
FROM V8_STAT_SHIS_CHANNEL_YEAR_CAD)
GROUP BY WDAT_DATE, CHANNEL_ID, YCAD_ID;

CREATE OR REPLACE VIEW V8_STAT_SHIS_XCMS_1_DAY_LY
AS 
SELECT 
    WDAT_DATE WDAT_DATE,
    SHIS_WPHS_ID SHIS_WPHS_ID,
    NVL(XCMS_ID,0) XCMS_ID,
    SUM(NOOFROOMS) NOOFROOMS,
    SUM(ADULTNO) ADULTNO,
    SUM(KIDS) KIDS,
    SUM(ARRIVALS) ARRIVALS,
    SUM(DEPARTURES) DEPARTURES,
    SUM(ARR_ADULTNO) ARR_ADULTNO,
    SUM(DEP_ADULTNO) DEP_ADULTNO,
    SUM(ARR_KIDS) ARR_KIDS,
    SUM(DEP_KIDS) DEP_KIDS,
    SUM(NOSHOW_ROOM) NOSHOW_ROOM,
    SUM(CANCEL_ROOM) CANCEL_ROOM,
    SUM(NETROOMREV) NETROOMREV,
    SUM(NETFBREV) NETFBREV,
    SUM(NETMINIBARREV) NETMINIBARREV,
    SUM(NETTELEREV) NETTELEREV,
    SUM(NETMISCREV) NETMISCREV,
    SUM(NETREV) NETREV,
    SUM(GROSSROOMREV) GROSSROOMREV,
    SUM(GROSSFBREV) GROSSFBREV,
    SUM(GROSSMINIBARREV) GROSSMINIBARREV,
    SUM(GROSSTELEREV) GROSSTELEREV,
    SUM(GROSSMISCREV) GROSSMISCREV,
    SUM(GROSSREV) GROSSREV
FROM 
( 
    SELECT 
        WDAT.WDAT_DATE WDAT_DATE,
        S.SHIS_WPHS_ID SHIS_WPHS_ID, 
        S.SHIS_YCLN_1_XCMS_ID XCMS_ID,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 OR S.SHIS_DEP_ID = 1 AND S.SHIS_ARR_ID = 0 THEN 0 ELSE S.SHIS_YDET_NOOFROOMS END) NOOFROOMS,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 OR S.SHIS_DEP_ID = 1 AND S.SHIS_ARR_ID = 0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END) ADULTNO,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 OR S.SHIS_DEP_ID = 1 AND S.SHIS_ARR_ID = 0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER, 0) END) KIDS,
        SUM(DECODE(S.SHIS_ARR_ID, 1, S.SHIS_YDET_NOOFROOMS, 0)) ARRIVALS,
        SUM(DECODE(S.SHIS_DEP_ID, 1, S.SHIS_YDET_NOOFROOMS, 0)) DEPARTURES,
        SUM(DECODE(S.SHIS_ARR_ID, 1, S.SHIS_YDET_ADULTNO, 0)) ARR_ADULTNO,
        SUM(DECODE(S.SHIS_DEP_ID, 1, S.SHIS_YDET_ADULTNO, 0)) DEP_ADULTNO,
        SUM(DECODE(S.SHIS_ARR_ID, 1, NVL(S.SHIS_TOTALYRGL_NUMBER, 0), 0)) ARR_KIDS,
        SUM(DECODE(S.SHIS_DEP_ID, 1, NVL(S.SHIS_TOTALYRGL_NUMBER, 0), 0)) DEP_KIDS,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 AND S.SHIS_YRES_NOAVAILREASON = 4 AND TRUNC(S.SHIS_YRES_EXPARRTIME) = TRUNC(S.SHIS_WDAT_DATE) THEN S.SHIS_YDET_NOOFROOMS ELSE 0 END) NOSHOW_ROOM,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 AND S.SHIS_YRES_NOAVAILREASON = 3 AND TRUNC(S.SHIS_YRES_EXPARRTIME) = TRUNC(S.SHIS_WDAT_DATE) THEN S.SHIS_YDET_NOOFROOMS ELSE 0 END) CANCEL_ROOM,
        SUM(S.SHIS_NETROOMREV) NETROOMREV,
        SUM(S.SHIS_NETFBREV) NETFBREV,
        SUM(S.SHIS_NETMINIBARREV) NETMINIBARREV,
        SUM(S.SHIS_NETTELEREV) NETTELEREV,
        SUM(S.SHIS_NETMISCREV) NETMISCREV,
        SUM(S.SHIS_NETREV) NETREV,
        SUM(S.SHIS_GROSSROOMREV) GROSSROOMREV,
        SUM(S.SHIS_GROSSFBREV) GROSSFBREV,
        SUM(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV,
        SUM(S.SHIS_GROSSTELEREV) GROSSTELEREV,
        SUM(S.SHIS_GROSSMISCREV) GROSSMISCREV,
        SUM(S.SHIS_GROSSREV) GROSSREV
    FROM 
        WDAT,
        SHIS S
    WHERE
           add_months(WDAT.WDAT_DATE,-12) = S.SHIS_WDAT_DATE 
    GROUP BY WDAT.WDAT_DATE,S.SHIS_YCLN_1_XCMS_ID,SHIS_WPHS_ID
    UNION ALL
    SELECT 
        WDAT.WDAT_DATE WDAT_DATE,
        S.SHIS_WPHS_ID SHIS_WPHS_ID,  
        S.SHIS_XCMS_ID XCMS_ID,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 OR S.SHIS_DEP_ID = 1 AND S.SHIS_ARR_ID = 0 THEN 0 ELSE S.SHIS_YDET_NOOFROOMS END) NOOFROOMS,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 OR S.SHIS_DEP_ID = 1 AND S.SHIS_ARR_ID = 0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END) ADULTNO,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 OR S.SHIS_DEP_ID = 1 AND S.SHIS_ARR_ID = 0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER, 0) END) KIDS,
        SUM(DECODE(S.SHIS_ARR_ID, 1, S.SHIS_YDET_NOOFROOMS, 0)) ARRIVALS,
        SUM(DECODE(S.SHIS_DEP_ID, 1, S.SHIS_YDET_NOOFROOMS, 0)) DEPARTURES,
        SUM(DECODE(S.SHIS_ARR_ID, 1, S.SHIS_YDET_ADULTNO, 0)) ARR_ADULTNO,
        SUM(DECODE(S.SHIS_DEP_ID, 1, S.SHIS_YDET_ADULTNO, 0)) DEP_ADULTNO,
        SUM(DECODE(S.SHIS_ARR_ID, 1, NVL(S.SHIS_TOTALYRGL_NUMBER, 0), 0)) ARR_KIDS,
        SUM(DECODE(S.SHIS_DEP_ID, 1, NVL(S.SHIS_TOTALYRGL_NUMBER, 0), 0)) DEP_KIDS,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 AND S.SHIS_YRES_NOAVAILREASON = 4 AND TRUNC(S.SHIS_YRES_EXPARRTIME) = TRUNC(S.SHIS_WDAT_DATE) THEN S.SHIS_YDET_NOOFROOMS ELSE 0 END) NOSHOW_ROOM,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 AND S.SHIS_YRES_NOAVAILREASON = 3 AND TRUNC(S.SHIS_YRES_EXPARRTIME) = TRUNC(S.SHIS_WDAT_DATE) THEN S.SHIS_YDET_NOOFROOMS ELSE 0 END) CANCEL_ROOM,
        SUM(S.SHIS_NETROOMREV) NETROOMREV,
        SUM(S.SHIS_NETFBREV) NETFBREV,
        SUM(S.SHIS_NETMINIBARREV) NETMINIBARREV,
        SUM(S.SHIS_NETTELEREV) NETTELEREV,
        SUM(S.SHIS_NETMISCREV) NETMISCREV,
        SUM(S.SHIS_NETREV) NETREV,
        SUM(S.SHIS_GROSSROOMREV) GROSSROOMREV,
        SUM(S.SHIS_GROSSFBREV) GROSSFBREV,
        SUM(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV,
        SUM(S.SHIS_GROSSTELEREV) GROSSTELEREV,
        SUM(S.SHIS_GROSSMISCREV) GROSSMISCREV,
        SUM(S.SHIS_GROSSREV) GROSSREV
    FROM
        WDAT,
        SHIS S 
    WHERE
        add_months(WDAT.WDAT_DATE,-12) = S.SHIS_WDAT_DATE AND
        NVL((SELECT XCCA.XCCA_INTERNALCATEGORY FROM XCCA, XCMS WHERE XCMS.XCMS_XCCA_ID = XCCA.XCCA_ID AND XCMS.XCMS_ID = S.SHIS_XCMS_ID),0) = 1 AND
        NVL(S.SHIS_YCLN_1_XCMS_ID, 0) <> S.SHIS_XCMS_ID 
    GROUP BY WDAT.WDAT_DATE,S.SHIS_XCMS_ID,SHIS_WPHS_ID
) 
GROUP BY WDAT_DATE,XCMS_ID,SHIS_WPHS_ID;

CREATE OR REPLACE VIEW V8_STAT_SHIS_XCMS_1_MONTH_LY
AS 
SELECT 
    WDAT_DATE WDAT_DATE,
    SHIS_WPHS_ID SHIS_WPHS_ID,
    NVL(XCMS_ID,0) XCMS_ID,
    SUM(NOOFROOMS) NOOFROOMS,
    SUM(ADULTNO) ADULTNO,
    SUM(KIDS) KIDS,
    SUM(ARRIVALS) ARRIVALS,
    SUM(DEPARTURES) DEPARTURES,
    SUM(ARR_ADULTNO) ARR_ADULTNO,
    SUM(DEP_ADULTNO) DEP_ADULTNO,
    SUM(ARR_KIDS) ARR_KIDS,
    SUM(DEP_KIDS) DEP_KIDS,
    SUM(NOSHOW_ROOM) NOSHOW_ROOM,
    SUM(CANCEL_ROOM) CANCEL_ROOM,
    SUM(NETROOMREV) NETROOMREV,
    SUM(NETFBREV) NETFBREV,
    SUM(NETMINIBARREV) NETMINIBARREV,
    SUM(NETTELEREV) NETTELEREV,
    SUM(NETMISCREV) NETMISCREV,
    SUM(NETREV) NETREV,
    SUM(GROSSROOMREV) GROSSROOMREV,
    SUM(GROSSFBREV) GROSSFBREV,
    SUM(GROSSMINIBARREV) GROSSMINIBARREV,
    SUM(GROSSTELEREV) GROSSTELEREV,
    SUM(GROSSMISCREV) GROSSMISCREV,
    SUM(GROSSREV) GROSSREV
FROM 
( 
    SELECT 
        WDAT.WDAT_DATE WDAT_DATE, 
        S.SHIS_WPHS_ID SHIS_WPHS_ID,     
        S.SHIS_YCLN_1_XCMS_ID XCMS_ID, 
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 OR S.SHIS_DEP_ID = 1 AND S.SHIS_ARR_ID = 0 THEN 0 ELSE S.SHIS_YDET_NOOFROOMS END) NOOFROOMS,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 OR S.SHIS_DEP_ID = 1 AND S.SHIS_ARR_ID = 0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END) ADULTNO,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 OR S.SHIS_DEP_ID = 1 AND S.SHIS_ARR_ID = 0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER, 0) END) KIDS,
        SUM(DECODE(S.SHIS_ARR_ID, 1, S.SHIS_YDET_NOOFROOMS, 0)) ARRIVALS,
        SUM(DECODE(S.SHIS_DEP_ID, 1, S.SHIS_YDET_NOOFROOMS, 0)) DEPARTURES,
        SUM(DECODE(S.SHIS_ARR_ID, 1, S.SHIS_YDET_ADULTNO, 0)) ARR_ADULTNO,
        SUM(DECODE(S.SHIS_DEP_ID, 1, S.SHIS_YDET_ADULTNO, 0)) DEP_ADULTNO,
        SUM(DECODE(S.SHIS_ARR_ID, 1, NVL(S.SHIS_TOTALYRGL_NUMBER, 0), 0)) ARR_KIDS,
        SUM(DECODE(S.SHIS_DEP_ID, 1, NVL(S.SHIS_TOTALYRGL_NUMBER, 0), 0)) DEP_KIDS,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 AND S.SHIS_YRES_NOAVAILREASON = 4 AND TRUNC(S.SHIS_YRES_EXPARRTIME) = TRUNC(S.SHIS_WDAT_DATE) THEN S.SHIS_YDET_NOOFROOMS ELSE 0 END) NOSHOW_ROOM,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 AND S.SHIS_YRES_NOAVAILREASON = 3 AND TRUNC(S.SHIS_YRES_EXPARRTIME) = TRUNC(S.SHIS_WDAT_DATE) THEN S.SHIS_YDET_NOOFROOMS ELSE 0 END) CANCEL_ROOM,
        SUM(S.SHIS_NETROOMREV) NETROOMREV,
        SUM(S.SHIS_NETFBREV) NETFBREV,
        SUM(S.SHIS_NETMINIBARREV) NETMINIBARREV,
        SUM(S.SHIS_NETTELEREV) NETTELEREV,
        SUM(S.SHIS_NETMISCREV) NETMISCREV,
        SUM(S.SHIS_NETREV) NETREV,
        SUM(S.SHIS_GROSSROOMREV) GROSSROOMREV,
        SUM(S.SHIS_GROSSFBREV) GROSSFBREV,
        SUM(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV,
        SUM(S.SHIS_GROSSTELEREV) GROSSTELEREV,
        SUM(S.SHIS_GROSSMISCREV) GROSSMISCREV,
        SUM(S.SHIS_GROSSREV) GROSSREV
    FROM 
        WDAT, 
        SHIS S 
    WHERE 
     add_months(TRUNC(WDAT.WDAT_DATE,'MM'),-12) <= S.SHIS_WDAT_DATE AND
        S.SHIS_WDAT_DATE<=add_months(WDAT.WDAT_DATE,-12) 
    GROUP BY WDAT.WDAT_DATE,S.SHIS_YCLN_1_XCMS_ID,SHIS_WPHS_ID
    UNION ALL
    SELECT 
        WDAT.WDAT_DATE WDAT_DATE, 
        S.SHIS_WPHS_ID SHIS_WPHS_ID,  
        S.SHIS_XCMS_ID XCMS_ID, 
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 OR S.SHIS_DEP_ID = 1 AND S.SHIS_ARR_ID = 0 THEN 0 ELSE S.SHIS_YDET_NOOFROOMS END) NOOFROOMS,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 OR S.SHIS_DEP_ID = 1 AND S.SHIS_ARR_ID = 0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END) ADULTNO,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 OR S.SHIS_DEP_ID = 1 AND S.SHIS_ARR_ID = 0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER, 0) END) KIDS,
        SUM(DECODE(S.SHIS_ARR_ID, 1, S.SHIS_YDET_NOOFROOMS, 0)) ARRIVALS,
        SUM(DECODE(S.SHIS_DEP_ID, 1, S.SHIS_YDET_NOOFROOMS, 0)) DEPARTURES,
        SUM(DECODE(S.SHIS_ARR_ID, 1, S.SHIS_YDET_ADULTNO, 0)) ARR_ADULTNO,
        SUM(DECODE(S.SHIS_DEP_ID, 1, S.SHIS_YDET_ADULTNO, 0)) DEP_ADULTNO,
        SUM(DECODE(S.SHIS_ARR_ID, 1, NVL(S.SHIS_TOTALYRGL_NUMBER, 0), 0)) ARR_KIDS,
        SUM(DECODE(S.SHIS_DEP_ID, 1, NVL(S.SHIS_TOTALYRGL_NUMBER, 0), 0)) DEP_KIDS,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 AND S.SHIS_YRES_NOAVAILREASON = 4 AND TRUNC(S.SHIS_YRES_EXPARRTIME) = TRUNC(S.SHIS_WDAT_DATE) THEN S.SHIS_YDET_NOOFROOMS ELSE 0 END) NOSHOW_ROOM,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 AND S.SHIS_YRES_NOAVAILREASON = 3 AND TRUNC(S.SHIS_YRES_EXPARRTIME) = TRUNC(S.SHIS_WDAT_DATE) THEN S.SHIS_YDET_NOOFROOMS ELSE 0 END) CANCEL_ROOM,
        SUM(S.SHIS_NETROOMREV) NETROOMREV,
        SUM(S.SHIS_NETFBREV) NETFBREV,
        SUM(S.SHIS_NETMINIBARREV) NETMINIBARREV,
        SUM(S.SHIS_NETTELEREV) NETTELEREV,
        SUM(S.SHIS_NETMISCREV) NETMISCREV,
        SUM(S.SHIS_NETREV) NETREV,
        SUM(S.SHIS_GROSSROOMREV) GROSSROOMREV,
        SUM(S.SHIS_GROSSFBREV) GROSSFBREV,
        SUM(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV,
        SUM(S.SHIS_GROSSTELEREV) GROSSTELEREV,
        SUM(S.SHIS_GROSSMISCREV) GROSSMISCREV,
        SUM(S.SHIS_GROSSREV) GROSSREV
    FROM 
        WDAT, 
        SHIS S 
    WHERE 
        NVL((SELECT XCCA.XCCA_INTERNALCATEGORY FROM XCCA, XCMS WHERE XCMS.XCMS_XCCA_ID = XCCA.XCCA_ID AND XCMS.XCMS_ID = S.SHIS_XCMS_ID),0) = 1 AND
        NVL(S.SHIS_YCLN_1_XCMS_ID, 0) <> S.SHIS_XCMS_ID AND
        add_months(TRUNC(WDAT.WDAT_DATE,'MM'),-12) <= S.SHIS_WDAT_DATE AND
        S.SHIS_WDAT_DATE<=add_months(WDAT.WDAT_DATE,-12) 
    GROUP BY WDAT.WDAT_DATE,S.SHIS_XCMS_ID,SHIS_WPHS_ID
) 
GROUP BY WDAT_DATE,XCMS_ID,SHIS_WPHS_ID;

CREATE OR REPLACE VIEW V8_STAT_SHIS_XCMS_1_YEAR_LY
AS 
SELECT 
    WDAT_DATE WDAT_DATE,
    SHIS_WPHS_ID SHIS_WPHS_ID,
    NVL(XCMS_ID,0) XCMS_ID,
    SUM(NOOFROOMS) NOOFROOMS,
    SUM(ADULTNO) ADULTNO,
    SUM(KIDS) KIDS,
    SUM(ARRIVALS) ARRIVALS,
    SUM(DEPARTURES) DEPARTURES,
    SUM(ARR_ADULTNO) ARR_ADULTNO,
    SUM(DEP_ADULTNO) DEP_ADULTNO,
    SUM(ARR_KIDS) ARR_KIDS,
    SUM(DEP_KIDS) DEP_KIDS,
    SUM(NOSHOW_ROOM) NOSHOW_ROOM,
    SUM(CANCEL_ROOM) CANCEL_ROOM,
    SUM(NETROOMREV) NETROOMREV,
    SUM(NETFBREV) NETFBREV,
    SUM(NETMINIBARREV) NETMINIBARREV,
    SUM(NETTELEREV) NETTELEREV,
    SUM(NETMISCREV) NETMISCREV,
    SUM(NETREV) NETREV,
    SUM(GROSSROOMREV) GROSSROOMREV,
    SUM(GROSSFBREV) GROSSFBREV,
    SUM(GROSSMINIBARREV) GROSSMINIBARREV,
    SUM(GROSSTELEREV) GROSSTELEREV,
    SUM(GROSSMISCREV) GROSSMISCREV,
    SUM(GROSSREV) GROSSREV
FROM 
(
    SELECT 
        WDAT.WDAT_DATE WDAT_DATE, 
        S.SHIS_WPHS_ID SHIS_WPHS_ID,
        S.SHIS_YCLN_1_XCMS_ID XCMS_ID, 
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 OR S.SHIS_DEP_ID = 1 AND S.SHIS_ARR_ID = 0 THEN 0 ELSE S.SHIS_YDET_NOOFROOMS END) NOOFROOMS,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 OR S.SHIS_DEP_ID = 1 AND S.SHIS_ARR_ID = 0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END) ADULTNO,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 OR S.SHIS_DEP_ID = 1 AND S.SHIS_ARR_ID = 0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER, 0) END) KIDS,
        SUM(DECODE(S.SHIS_ARR_ID, 1, S.SHIS_YDET_NOOFROOMS, 0)) ARRIVALS,
        SUM(DECODE(S.SHIS_DEP_ID, 1, S.SHIS_YDET_NOOFROOMS, 0)) DEPARTURES,
        SUM(DECODE(S.SHIS_ARR_ID, 1, S.SHIS_YDET_ADULTNO, 0)) ARR_ADULTNO,
        SUM(DECODE(S.SHIS_DEP_ID, 1, S.SHIS_YDET_ADULTNO, 0)) DEP_ADULTNO,
        SUM(DECODE(S.SHIS_ARR_ID, 1, NVL(S.SHIS_TOTALYRGL_NUMBER, 0), 0)) ARR_KIDS,
        SUM(DECODE(S.SHIS_DEP_ID, 1, NVL(S.SHIS_TOTALYRGL_NUMBER, 0), 0)) DEP_KIDS,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 AND S.SHIS_YRES_NOAVAILREASON = 4 AND TRUNC(S.SHIS_YRES_EXPARRTIME) = TRUNC(S.SHIS_WDAT_DATE) THEN S.SHIS_YDET_NOOFROOMS ELSE 0 END) NOSHOW_ROOM,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 AND S.SHIS_YRES_NOAVAILREASON = 3 AND TRUNC(S.SHIS_YRES_EXPARRTIME) = TRUNC(S.SHIS_WDAT_DATE) THEN S.SHIS_YDET_NOOFROOMS ELSE 0 END) CANCEL_ROOM,
        SUM(S.SHIS_NETROOMREV) NETROOMREV,
        SUM(S.SHIS_NETFBREV) NETFBREV,
        SUM(S.SHIS_NETMINIBARREV) NETMINIBARREV,
        SUM(S.SHIS_NETTELEREV) NETTELEREV,
        SUM(S.SHIS_NETMISCREV) NETMISCREV,
        SUM(S.SHIS_NETREV) NETREV,
        SUM(S.SHIS_GROSSROOMREV) GROSSROOMREV,
        SUM(S.SHIS_GROSSFBREV) GROSSFBREV,
        SUM(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV,
        SUM(S.SHIS_GROSSTELEREV) GROSSTELEREV,
        SUM(S.SHIS_GROSSMISCREV) GROSSMISCREV,
        SUM(S.SHIS_GROSSREV) GROSSREV
    FROM
        WDAT,
        SHIS S
    WHERE 
        add_months(TRUNC(WDAT.WDAT_DATE,'YY'),-12) <= S.SHIS_WDAT_DATE AND
        S.SHIS_WDAT_DATE <= add_months(WDAT.WDAT_DATE,-12)
    GROUP BY WDAT.WDAT_DATE,S.SHIS_YCLN_1_XCMS_ID,SHIS_WPHS_ID
    UNION ALL
    SELECT 
        WDAT.WDAT_DATE WDAT_DATE, 
        S.SHIS_WPHS_ID SHIS_WPHS_ID, 
        S.SHIS_XCMS_ID XCMS_ID, 
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 OR S.SHIS_DEP_ID = 1 AND S.SHIS_ARR_ID = 0 THEN 0 ELSE S.SHIS_YDET_NOOFROOMS END) NOOFROOMS,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 OR S.SHIS_DEP_ID = 1 AND S.SHIS_ARR_ID = 0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END) ADULTNO,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 OR S.SHIS_DEP_ID = 1 AND S.SHIS_ARR_ID = 0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER, 0) END) KIDS,
        SUM(DECODE(S.SHIS_ARR_ID, 1, S.SHIS_YDET_NOOFROOMS, 0)) ARRIVALS,
        SUM(DECODE(S.SHIS_DEP_ID, 1, S.SHIS_YDET_NOOFROOMS, 0)) DEPARTURES,
        SUM(DECODE(S.SHIS_ARR_ID, 1, S.SHIS_YDET_ADULTNO, 0)) ARR_ADULTNO,
        SUM(DECODE(S.SHIS_DEP_ID, 1, S.SHIS_YDET_ADULTNO, 0)) DEP_ADULTNO,
        SUM(DECODE(S.SHIS_ARR_ID, 1, NVL(S.SHIS_TOTALYRGL_NUMBER, 0), 0)) ARR_KIDS,
        SUM(DECODE(S.SHIS_DEP_ID, 1, NVL(S.SHIS_TOTALYRGL_NUMBER, 0), 0)) DEP_KIDS,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 AND S.SHIS_YRES_NOAVAILREASON = 4 AND TRUNC(S.SHIS_YRES_EXPARRTIME) = TRUNC(S.SHIS_WDAT_DATE) THEN S.SHIS_YDET_NOOFROOMS ELSE 0 END) NOSHOW_ROOM,
        SUM(CASE WHEN S.SHIS_YRES_RESSTATUS = 3 AND S.SHIS_YRES_NOAVAILREASON = 3 AND TRUNC(S.SHIS_YRES_EXPARRTIME) = TRUNC(S.SHIS_WDAT_DATE) THEN S.SHIS_YDET_NOOFROOMS ELSE 0 END) CANCEL_ROOM,
        SUM(S.SHIS_NETROOMREV) NETROOMREV,
        SUM(S.SHIS_NETFBREV) NETFBREV,
        SUM(S.SHIS_NETMINIBARREV) NETMINIBARREV,
        SUM(S.SHIS_NETTELEREV) NETTELEREV,
        SUM(S.SHIS_NETMISCREV) NETMISCREV,
        SUM(S.SHIS_NETREV) NETREV,
        SUM(S.SHIS_GROSSROOMREV) GROSSROOMREV,
        SUM(S.SHIS_GROSSFBREV) GROSSFBREV,
        SUM(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV,
        SUM(S.SHIS_GROSSTELEREV) GROSSTELEREV,
        SUM(S.SHIS_GROSSMISCREV) GROSSMISCREV,
        SUM(S.SHIS_GROSSREV) GROSSREV
    FROM
        WDAT,
        SHIS S
    WHERE 
        NVL((SELECT XCCA.XCCA_INTERNALCATEGORY FROM XCCA, XCMS WHERE XCMS.XCMS_XCCA_ID = XCCA.XCCA_ID AND XCMS.XCMS_ID = S.SHIS_XCMS_ID),0) = 1 AND
        NVL(S.SHIS_YCLN_1_XCMS_ID, 0) <> S.SHIS_XCMS_ID AND
        add_months(TRUNC(WDAT.WDAT_DATE,'YYYY'),-12) <= S.SHIS_WDAT_DATE AND
        S.SHIS_WDAT_DATE <=add_months( WDAT.WDAT_DATE,-12) 
    GROUP BY WDAT.WDAT_DATE,S.SHIS_XCMS_ID,SHIS_WPHS_ID
) 
GROUP BY WDAT_DATE,XCMS_ID,SHIS_WPHS_ID;

CREATE OR REPLACE VIEW V8_STAT_SHIS_XCMS_1_COMBI_LYSD
AS 
SELECT 
    WDAT_DATE WDAT_DATE,
    SHIS_WPHS_ID SHIS_WPHS_ID,
    XCMS_ID XCMS_ID,
    SUM(NOOFROOMS_D) NOOFROOMS_D,
    SUM(ADULTNO_D) ADULTNO_D,
    SUM(KIDS_D) KIDS_D,
    SUM(ARRIVALS_D) ARRIVALS_D,
    SUM(DEPARTURES_D) DEPARTURES_D,
    SUM(ARR_ADULTNO_D) ARR_ADULTNO_D,
    SUM(DEP_ADULTNO_D) DEP_ADULTNO_D,
    SUM(ARR_KIDS_D) ARR_KIDS_D,
    SUM(DEP_KIDS_D) DEP_KIDS_D,
    SUM(NOSHOW_ROOM_D) NOSHOW_ROOM_D,
    SUM(CANCEL_ROOM_D) CANCEL_ROOM_D, 
    SUM(NETROOMREV_D) NETROOMREV_D, 
    SUM(NETFBREV_D) NETFBREV_D, 
    SUM(NETMINIBARREV_D) NETMINIBARREV_D, 
    SUM(NETTELEREV_D) NETTELEREV_D, 
    SUM(NETMISCREV_D) NETMISCREV_D, 
    SUM(NETREV_D) NETREV_D,
    SUM(GROSSROOMREV_D) GROSSROOMREV_D,
    SUM(GROSSFBREV_D) GROSSFBREV_D,
    SUM(GROSSMINIBARREV_D) GROSSMINIBARREV_D,
    SUM(GROSSTELEREV_D) GROSSTELEREV_D,
    SUM(GROSSMISCREV_D) GROSSMISCREV_D,
    SUM(GROSSREV_D) GROSSREV_D,
    SUM(NOOFROOMS_M) NOOFROOMS_M,
    SUM(ADULTNO_M) ADULTNO_M,
    SUM(KIDS_M) KIDS_M,
    SUM(ARRIVALS_M) ARRIVALS_M,
    SUM(DEPARTURES_M) DEPARTURES_M,
    SUM(ARR_ADULTNO_M) ARR_ADULTNO_M,
    SUM(DEP_ADULTNO_M) DEP_ADULTNO_M,
    SUM(ARR_KIDS_M) ARR_KIDS_M,
    SUM(DEP_KIDS_M) DEP_KIDS_M,
    SUM(NOSHOW_ROOM_M) NOSHOW_ROOM_M,
    SUM(CANCEL_ROOM_M) CANCEL_ROOM_M, 
    SUM(NETROOMREV_M) NETROOMREV_M, 
    SUM(NETFBREV_M) NETFBREV_M, 
    SUM(NETMINIBARREV_M) NETMINIBARREV_M, 
    SUM(NETTELEREV_M) NETTELEREV_M, 
    SUM(NETMISCREV_M) NETMISCREV_M, 
    SUM(NETREV_M) NETREV_M,
    SUM(GROSSROOMREV_M) GROSSROOMREV_M,
    SUM(GROSSFBREV_M) GROSSFBREV_M,
    SUM(GROSSMINIBARREV_M) GROSSMINIBARREV_M,
    SUM(GROSSTELEREV_M) GROSSTELEREV_M,
    SUM(GROSSMISCREV_M) GROSSMISCREV_M,
    SUM(GROSSREV_M) GROSSREV_M,
    SUM(NOOFROOMS_Y) NOOFROOMS_Y,
    SUM(ADULTNO_Y) ADULTNO_Y,
    SUM(KIDS_Y) KIDS_Y,
    SUM(ARRIVALS_Y) ARRIVALS_Y,
    SUM(DEPARTURES_Y) DEPARTURES_Y,
    SUM(ARR_ADULTNO_Y) ARR_ADULTNO_Y,
    SUM(DEP_ADULTNO_Y) DEP_ADULTNO_Y,
    SUM(ARR_KIDS_Y) ARR_KIDS_Y,
    SUM(DEP_KIDS_Y) DEP_KIDS_Y,
    SUM(NOSHOW_ROOM_Y) NOSHOW_ROOM_Y,
    SUM(CANCEL_ROOM_Y) CANCEL_ROOM_Y, 
    SUM(NETROOMREV_Y) NETROOMREV_Y, 
    SUM(NETFBREV_Y) NETFBREV_Y, 
    SUM(NETMINIBARREV_Y) NETMINIBARREV_Y, 
    SUM(NETTELEREV_Y) NETTELEREV_Y, 
    SUM(NETMISCREV_Y) NETMISCREV_Y, 
    SUM(NETREV_Y) NETREV_Y,
    SUM(GROSSROOMREV_Y) GROSSROOMREV_Y,
    SUM(GROSSFBREV_Y) GROSSFBREV_Y,
    SUM(GROSSMINIBARREV_Y) GROSSMINIBARREV_Y,
    SUM(GROSSTELEREV_Y) GROSSTELEREV_Y,
    SUM(GROSSMISCREV_Y) GROSSMISCREV_Y,
    SUM(GROSSREV_Y) GROSSREV_Y,
    SUM(NOOFROOMS_D_LY) NOOFROOMS_D_LY,
    SUM(ADULTNO_D_LY) ADULTNO_D_LY,
    SUM(KIDS_D_LY) KIDS_D_LY,
    SUM(ARRIVALS_D_LY) ARRIVALS_D_LY,
    SUM(DEPARTURES_D_LY) DEPARTURES_D_LY,
    SUM(ARR_ADULTNO_D_LY) ARR_ADULTNO_D_LY,
    SUM(DEP_ADULTNO_D_LY) DEP_ADULTNO_D_LY,
    SUM(ARR_KIDS_D_LY) ARR_KIDS_D_LY,
    SUM(DEP_KIDS_D_LY) DEP_KIDS_D_LY,
    SUM(NOSHOW_ROOM_D_LY) NOSHOW_ROOM_D_LY,
    SUM(CANCEL_ROOM_D_LY) CANCEL_ROOM_D_LY, 
    SUM(NETROOMREV_D_LY) NETROOMREV_D_LY, 
    SUM(NETFBREV_D_LY) NETFBREV_D_LY, 
    SUM(NETMINIBARREV_D_LY) NETMINIBARREV_D_LY, 
    SUM(NETTELEREV_D_LY) NETTELEREV_D_LY, 
    SUM(NETMISCREV_D_LY) NETMISCREV_D_LY, 
    SUM(NETREV_D_LY) NETREV_D_LY,
    SUM(GROSSROOMREV_D_LY) GROSSROOMREV_D_LY,
    SUM(GROSSFBREV_D_LY) GROSSFBREV_D_LY,
    SUM(GROSSMINIBARREV_D_LY) GROSSMINIBARREV_D_LY,
    SUM(GROSSTELEREV_D_LY) GROSSTELEREV_D_LY,
    SUM(GROSSMISCREV_D_LY) GROSSMISCREV_D_LY,
    SUM(GROSSREV_D_LY) GROSSREV_D_LY,
    SUM(NOOFROOMS_M_LY) NOOFROOMS_M_LY,
    SUM(ADULTNO_M_LY) ADULTNO_M_LY,
    SUM(KIDS_M_LY) KIDS_M_LY,
    SUM(ARRIVALS_M_LY) ARRIVALS_M_LY,
    SUM(DEPARTURES_M_LY) DEPARTURES_M_LY,
    SUM(ARR_ADULTNO_M_LY) ARR_ADULTNO_M_LY,
    SUM(DEP_ADULTNO_M_LY) DEP_ADULTNO_M_LY,
    SUM(ARR_KIDS_M_LY) ARR_KIDS_M_LY,
    SUM(DEP_KIDS_M_LY) DEP_KIDS_M_LY,
    SUM(NOSHOW_ROOM_M_LY) NOSHOW_ROOM_M_LY,
    SUM(CANCEL_ROOM_M_LY) CANCEL_ROOM_M_LY, 
    SUM(NETROOMREV_M_LY) NETROOMREV_M_LY, 
    SUM(NETFBREV_M_LY) NETFBREV_M_LY, 
    SUM(NETMINIBARREV_M_LY) NETMINIBARREV_M_LY, 
    SUM(NETTELEREV_M_LY) NETTELEREV_M_LY, 
    SUM(NETMISCREV_M_LY) NETMISCREV_M_LY, 
    SUM(NETREV_M_LY) NETREV_M_LY,
    SUM(GROSSROOMREV_M_LY) GROSSROOMREV_M_LY,
    SUM(GROSSFBREV_M_LY) GROSSFBREV_M_LY,
    SUM(GROSSMINIBARREV_M_LY) GROSSMINIBARREV_M_LY,
    SUM(GROSSTELEREV_M_LY) GROSSTELEREV_M_LY,
    SUM(GROSSMISCREV_M_LY) GROSSMISCREV_M_LY,
    SUM(GROSSREV_M_LY) GROSSREV_M_LY,
    SUM(NOOFROOMS_Y_LY) NOOFROOMS_Y_LY,
    SUM(ADULTNO_Y_LY) ADULTNO_Y_LY,
    SUM(KIDS_Y_LY) KIDS_Y_LY,
    SUM(ARRIVALS_Y_LY) ARRIVALS_Y_LY,
    SUM(DEPARTURES_Y_LY) DEPARTURES_Y_LY,
    SUM(ARR_ADULTNO_Y_LY) ARR_ADULTNO_Y_LY,
    SUM(DEP_ADULTNO_Y_LY) DEP_ADULTNO_Y_LY,
    SUM(ARR_KIDS_Y_LY) ARR_KIDS_Y_LY,
    SUM(DEP_KIDS_Y_LY) DEP_KIDS_Y_LY,
    SUM(NOSHOW_ROOM_Y_LY) NOSHOW_ROOM_Y_LY,
    SUM(CANCEL_ROOM_Y_LY) CANCEL_ROOM_Y_LY, 
    SUM(NETROOMREV_Y_LY) NETROOMREV_Y_LY, 
    SUM(NETFBREV_Y_LY) NETFBREV_Y_LY, 
    SUM(NETMINIBARREV_Y_LY) NETMINIBARREV_Y_LY, 
    SUM(NETTELEREV_Y_LY) NETTELEREV_Y_LY, 
    SUM(NETMISCREV_Y_LY) NETMISCREV_Y_LY, 
    SUM(NETREV_Y_LY) NETREV_Y_LY,
    SUM(GROSSROOMREV_Y_LY) GROSSROOMREV_Y_LY,
    SUM(GROSSFBREV_Y_LY) GROSSFBREV_Y_LY,
    SUM(GROSSMINIBARREV_Y_LY) GROSSMINIBARREV_Y_LY,
    SUM(GROSSTELEREV_Y_LY) GROSSTELEREV_Y_LY,
    SUM(GROSSMISCREV_Y_LY) GROSSMISCREV_Y_LY,
    SUM(GROSSREV_Y_LY) GROSSREV_Y_LY
FROM (
    SELECT
        WDAT_DATE, 
        SHIS_WPHS_ID,
        XCMS_ID,
        NOOFROOMS NOOFROOMS_D,
        ADULTNO ADULTNO_D,
        KIDS KIDS_D,
        ARRIVALS ARRIVALS_D,
        DEPARTURES DEPARTURES_D,
        ARR_ADULTNO ARR_ADULTNO_D, 
        DEP_ADULTNO DEP_ADULTNO_D,
        ARR_KIDS ARR_KIDS_D,
        DEP_KIDS DEP_KIDS_D,
        NOSHOW_ROOM NOSHOW_ROOM_D,
        CANCEL_ROOM CANCEL_ROOM_D,
        NETROOMREV NETROOMREV_D,
        NETFBREV NETFBREV_D,
        NETMINIBARREV NETMINIBARREV_D,
        NETTELEREV NETTELEREV_D,
        NETMISCREV NETMISCREV_D, 
        NETREV NETREV_D,
        GROSSROOMREV GROSSROOMREV_D,
        GROSSFBREV GROSSFBREV_D,
        GROSSMINIBARREV GROSSMINIBARREV_D, 
        GROSSTELEREV GROSSTELEREV_D,
        GROSSMISCREV GROSSMISCREV_D,
        GROSSREV GROSSREV_D,
        0 NOOFROOMS_M,
        0 ADULTNO_M,
        0 KIDS_M,
        0 ARRIVALS_M,
        0 DEPARTURES_M,
        0 ARR_ADULTNO_M, 
        0 DEP_ADULTNO_M,
        0 ARR_KIDS_M,
        0 DEP_KIDS_M,
        0 NOSHOW_ROOM_M,
        0 CANCEL_ROOM_M,
        0 NETROOMREV_M,
        0 NETFBREV_M,
        0 NETMINIBARREV_M,
        0 NETTELEREV_M,
        0 NETMISCREV_M, 
        0 NETREV_M,
        0 GROSSROOMREV_M,
        0 GROSSFBREV_M,
        0 GROSSMINIBARREV_M, 
        0 GROSSTELEREV_M,
        0 GROSSMISCREV_M,
        0 GROSSREV_M,
        0 NOOFROOMS_Y,
        0 ADULTNO_Y,
        0 KIDS_Y,
        0 ARRIVALS_Y,
        0 DEPARTURES_Y,
        0 ARR_ADULTNO_Y, 
        0 DEP_ADULTNO_Y,
        0 ARR_KIDS_Y,
        0 DEP_KIDS_Y,
        0 NOSHOW_ROOM_Y,
        0 CANCEL_ROOM_Y,
        0 NETROOMREV_Y,
        0 NETFBREV_Y,
        0 NETMINIBARREV_Y,
        0 NETTELEREV_Y,
        0 NETMISCREV_Y, 
        0 NETREV_Y,
        0 GROSSROOMREV_Y,
        0 GROSSFBREV_Y,
        0 GROSSMINIBARREV_Y, 
        0 GROSSTELEREV_Y,
        0 GROSSMISCREV_Y,
        0 GROSSREV_Y,
        0 NOOFROOMS_D_LY, 
        0 ADULTNO_D_LY, 
        0 KIDS_D_LY, 
        0 ARRIVALS_D_LY, 
        0 DEPARTURES_D_LY, 
        0 ARR_ADULTNO_D_LY, 
        0 DEP_ADULTNO_D_LY, 
        0 ARR_KIDS_D_LY, 
        0 DEP_KIDS_D_LY, 
        0 NOSHOW_ROOM_D_LY,
        0 CANCEL_ROOM_D_LY,         
        0 NETROOMREV_D_LY, 
        0 NETFBREV_D_LY, 
        0 NETMINIBARREV_D_LY, 
        0 NETTELEREV_D_LY, 
        0 NETMISCREV_D_LY, 
        0 NETREV_D_LY, 
        0 GROSSROOMREV_D_LY,
        0 GROSSFBREV_D_LY, 
        0 GROSSMINIBARREV_D_LY,
        0 GROSSTELEREV_D_LY,
        0 GROSSMISCREV_D_LY, 
        0 GROSSREV_D_LY,
        0 NOOFROOMS_M_LY,
        0 ADULTNO_M_LY, 
        0 KIDS_M_LY, 
        0 ARRIVALS_M_LY,
        0 DEPARTURES_M_LY,
        0 ARR_ADULTNO_M_LY, 
        0 DEP_ADULTNO_M_LY,
        0 ARR_KIDS_M_LY,
        0 DEP_KIDS_M_LY, 
        0 NOSHOW_ROOM_M_LY, 
        0 CANCEL_ROOM_M_LY, 
        0 NETROOMREV_M_LY, 
        0 NETFBREV_M_LY, 
        0 NETMINIBARREV_M_LY, 
        0 NETTELEREV_M_LY, 
        0 NETMISCREV_M_LY, 
        0 NETREV_M_LY, 
        0 GROSSROOMREV_M_LY,
        0 GROSSFBREV_M_LY, 
        0 GROSSMINIBARREV_M_LY,
        0 GROSSTELEREV_M_LY,
        0 GROSSMISCREV_M_LY,
        0 GROSSREV_M_LY, 
        0 NOOFROOMS_Y_LY, 
        0 ADULTNO_Y_LY,
        0 KIDS_Y_LY, 
        0 ARRIVALS_Y_LY,
        0 DEPARTURES_Y_LY, 
        0 ARR_ADULTNO_Y_LY, 
        0 DEP_ADULTNO_Y_LY, 
        0 ARR_KIDS_Y_LY, 
        0 DEP_KIDS_Y_LY, 
        0 NOSHOW_ROOM_Y_LY, 
        0 CANCEL_ROOM_Y_LY, 
        0 NETROOMREV_Y_LY, 
        0 NETFBREV_Y_LY, 
        0 NETMINIBARREV_Y_LY, 
        0 NETTELEREV_Y_LY, 
        0 NETMISCREV_Y_LY, 
        0 NETREV_Y_LY,
        0 GROSSROOMREV_Y_LY,
        0 GROSSFBREV_Y_LY,
        0 GROSSMINIBARREV_Y_LY, 
        0 GROSSTELEREV_Y_LY, 
        0 GROSSMISCREV_Y_LY, 
        0 GROSSREV_Y_LY
    FROM V8_STAT_SHIS_XCMS_1_DAY
    UNION ALL
    SELECT
        WDAT_DATE,
        SHIS_WPHS_ID,
        XCMS_ID,
        0 NOOFROOMS_D,
        0 ADULTNO_D,
        0 KIDS_D,
        0 ARRIVALS_D,
        0 DEPARTURES_D,
        0 ARR_ADULTNO_D, 
        0 DEP_ADULTNO_D,
        0 ARR_KIDS_D,
        0 DEP_KIDS_D,
        0 NOSHOW_ROOM_D,
        0 CANCEL_ROOM_D,
        0 NETROOMREV_D,
        0 NETFBREV_D,
        0 NETMINIBARREV_D,
        0 NETTELEREV_D,
        0 NETMISCREV_D, 
        0 NETREV_D,
        0 GROSSROOMREV_D,
        0 GROSSFBREV_D,
        0 GROSSMINIBARREV_D, 
        0 GROSSTELEREV_D,
        0 GROSSMISCREV_D,
        0 GROSSREV_D,
        NOOFROOMS NOOFROOMS_M,
        ADULTNO ADULTNO_M,
        KIDS KIDS_M,
        ARRIVALS ARRIVALS_M,
        DEPARTURES DEPARTURES_M,
        ARR_ADULTNO ARR_ADULTNO_M, 
        DEP_ADULTNO DEP_ADULTNO_M,
        ARR_KIDS ARR_KIDS_M,
        DEP_KIDS DEP_KIDS_M,
        NOSHOW_ROOM NOSHOW_ROOM_M,
        CANCEL_ROOM CANCEL_ROOM_M,
        NETROOMREV NETROOMREV_M,
        NETFBREV NETFBREV_M,
        NETMINIBARREV NETMINIBARREV_M,
        NETTELEREV NETTELEREV_M,
        NETMISCREV NETMISCREV_M, 
        NETREV NETREV_M,
        GROSSROOMREV GROSSROOMREV_M,
        GROSSFBREV GROSSFBREV_M,
        GROSSMINIBARREV GROSSMINIBARREV_M, 
        GROSSTELEREV GROSSTELEREV_M,
        GROSSMISCREV GROSSMISCREV_M,
        GROSSREV GROSSREV_M,
        0 NOOFROOMS_Y,
        0 ADULTNO_Y,
        0 KIDS_Y,
        0 ARRIVALS_Y,
        0 DEPARTURES_Y,
        0 ARR_ADULTNO_Y, 
        0 DEP_ADULTNO_Y,
        0 ARR_KIDS_Y,
        0 DEP_KIDS_Y,
        0 NOSHOW_ROOM_Y,
        0 CANCEL_ROOM_Y,
        0 NETROOMREV_Y,
        0 NETFBREV_Y,
        0 NETMINIBARREV_Y,
        0 NETTELEREV_Y,
        0 NETMISCREV_Y, 
        0 NETREV_Y,
        0 GROSSROOMREV_Y,
        0 GROSSFBREV_Y,
        0 GROSSMINIBARREV_Y, 
        0 GROSSTELEREV_Y,
        0 GROSSMISCREV_Y,
        0 GROSSREV_Y,
        0 NOOFROOMS_D_LY, 
        0 ADULTNO_D_LY, 
        0 KIDS_D_LY, 
        0 ARRIVALS_D_LY, 
        0 DEPARTURES_D_LY, 
        0 ARR_ADULTNO_D_LY, 
        0 DEP_ADULTNO_D_LY, 
        0 ARR_KIDS_D_LY, 
        0 DEP_KIDS_D_LY, 
        0 NOSHOW_ROOM_D_LY,
        0 CANCEL_ROOM_D_LY, 
        0 NETROOMREV_D_LY, 
        0 NETFBREV_D_LY, 
        0 NETMINIBARREV_D_LY, 
        0 NETTELEREV_D_LY, 
        0 NETMISCREV_D_LY, 
        0 NETREV_D_LY, 
        0 GROSSROOMREV_D_LY,
        0 GROSSFBREV_D_LY, 
        0 GROSSMINIBARREV_D_LY,
        0 GROSSTELEREV_D_LY,
        0 GROSSMISCREV_D_LY, 
        0 GROSSREV_D_LY,
        0 NOOFROOMS_M_LY,
        0 ADULTNO_M_LY, 
        0 KIDS_M_LY, 
        0 ARRIVALS_M_LY,
        0 DEPARTURES_M_LY,
        0 ARR_ADULTNO_M_LY, 
        0 DEP_ADULTNO_M_LY,
        0 ARR_KIDS_M_LY,
        0 DEP_KIDS_M_LY, 
        0 NOSHOW_ROOM_M_LY, 
        0 CANCEL_ROOM_M_LY, 
        0 NETROOMREV_M_LY, 
        0 NETFBREV_M_LY, 
        0 NETMINIBARREV_M_LY, 
        0 NETTELEREV_M_LY, 
        0 NETMISCREV_M_LY, 
        0 NETREV_M_LY, 
        0 GROSSROOMREV_M_LY,
        0 GROSSFBREV_M_LY, 
        0 GROSSMINIBARREV_M_LY,
        0 GROSSTELEREV_M_LY,
        0 GROSSMISCREV_M_LY,
        0 GROSSREV_M_LY, 
        0 NOOFROOMS_Y_LY, 
        0 ADULTNO_Y_LY,
        0 KIDS_Y_LY, 
        0 ARRIVALS_Y_LY,
        0 DEPARTURES_Y_LY, 
        0 ARR_ADULTNO_Y_LY, 
        0 DEP_ADULTNO_Y_LY, 
        0 ARR_KIDS_Y_LY, 
        0 DEP_KIDS_Y_LY, 
        0 NOSHOW_ROOM_Y_LY, 
        0 CANCEL_ROOM_Y_LY, 
        0 NETROOMREV_Y_LY, 
        0 NETFBREV_Y_LY, 
        0 NETMINIBARREV_Y_LY, 
        0 NETTELEREV_Y_LY, 
        0 NETMISCREV_Y_LY, 
        0 NETREV_Y_LY,
        0 GROSSROOMREV_Y_LY,
        0 GROSSFBREV_Y_LY,
        0 GROSSMINIBARREV_Y_LY, 
        0 GROSSTELEREV_Y_LY, 
        0 GROSSMISCREV_Y_LY, 
        0 GROSSREV_Y_LY
    FROM V8_STAT_SHIS_XCMS_1_MONTH
    UNION ALL
    SELECT
        WDAT_DATE,
        SHIS_WPHS_ID,
        XCMS_ID,
        0 NOOFROOMS_D,
        0 ADULTNO_D,
        0 KIDS_D,
        0 ARRIVALS_D,
        0 DEPARTURES_D,
        0 ARR_ADULTNO_D, 
        0 DEP_ADULTNO_D,
        0 ARR_KIDS_D,
        0 DEP_KIDS_D,
        0 NOSHOW_ROOM_D,
        0 CANCEL_ROOM_D,
        0 NETROOMREV_D,
        0 NETFBREV_D,
        0 NETMINIBARREV_D,
        0 NETTELEREV_D,
        0 NETMISCREV_D, 
        0 NETREV_D,
        0 GROSSROOMREV_D,
        0 GROSSFBREV_D,
        0 GROSSMINIBARREV_D, 
        0 GROSSTELEREV_D,
        0 GROSSMISCREV_D,
        0 GROSSREV_D,
        0 NOOFROOMS_M,
        0 ADULTNO_M,
        0 KIDS_M,
        0 ARRIVALS_M,
        0 DEPARTURES_M,
        0 ARR_ADULTNO_M, 
        0 DEP_ADULTNO_M,
        0 ARR_KIDS_M,
        0 DEP_KIDS_M,
        0 NOSHOW_ROOM_M,
        0 CANCEL_ROOM_M,
        0 NETROOMREV_M,
        0 NETFBREV_M,
        0 NETMINIBARREV_M,
        0 NETTELEREV_M,
        0 NETMISCREV_M, 
        0 NETREV_M,
        0 GROSSROOMREV_M,
        0 GROSSFBREV_M,
        0 GROSSMINIBARREV_M, 
        0 GROSSTELEREV_M,
        0 GROSSMISCREV_M,
        0 GROSSREV_M,
        NOOFROOMS NOOFROOMS_Y,
        ADULTNO ADULTNO_Y,
        KIDS KIDS_Y,
        ARRIVALS ARRIVALS_Y,
        DEPARTURES DEPARTURES_Y,
        ARR_ADULTNO ARR_ADULTNO_Y, 
        DEP_ADULTNO DEP_ADULTNO_Y,
        ARR_KIDS ARR_KIDS_Y,
        DEP_KIDS DEP_KIDS_Y,
        NOSHOW_ROOM NOSHOW_ROOM_Y,
        CANCEL_ROOM CANCEL_ROOM_Y,
        NETROOMREV NETROOMREV_Y,
        NETFBREV NETFBREV_Y,
        NETMINIBARREV NETMINIBARREV_Y,
        NETTELEREV NETTELEREV_Y,
        NETMISCREV NETMISCREV_Y, 
        NETREV NETREV_Y,
        GROSSROOMREV GROSSROOMREV_Y,
        GROSSFBREV GROSSFBREV_Y,
        GROSSMINIBARREV GROSSMINIBARREV_Y, 
        GROSSTELEREV GROSSTELEREV_Y,
        GROSSMISCREV GROSSMISCREV_Y,
        GROSSREV GROSSREV_Y,
        0 NOOFROOMS_D_LY, 
        0 ADULTNO_D_LY, 
        0 KIDS_D_LY, 
        0 ARRIVALS_D_LY, 
        0 DEPARTURES_D_LY, 
        0 ARR_ADULTNO_D_LY, 
        0 DEP_ADULTNO_D_LY, 
        0 ARR_KIDS_D_LY, 
        0 DEP_KIDS_D_LY, 
        0 NOSHOW_ROOM_D_LY,
        0 CANCEL_ROOM_D_LY, 
        0 NETROOMREV_D_LY, 
        0 NETFBREV_D_LY, 
        0 NETMINIBARREV_D_LY, 
        0 NETTELEREV_D_LY, 
        0 NETMISCREV_D_LY, 
        0 NETREV_D_LY, 
        0 GROSSROOMREV_D_LY,
        0 GROSSFBREV_D_LY, 
        0 GROSSMINIBARREV_D_LY,
        0 GROSSTELEREV_D_LY,
        0 GROSSMISCREV_D_LY, 
        0 GROSSREV_D_LY,
        0 NOOFROOMS_M_LY,
        0 ADULTNO_M_LY, 
        0 KIDS_M_LY, 
        0 ARRIVALS_M_LY,
        0 DEPARTURES_M_LY,
        0 ARR_ADULTNO_M_LY, 
        0 DEP_ADULTNO_M_LY,
        0 ARR_KIDS_M_LY,
        0 DEP_KIDS_M_LY, 
        0 NOSHOW_ROOM_M_LY, 
        0 CANCEL_ROOM_M_LY, 
        0 NETROOMREV_M_LY, 
        0 NETFBREV_M_LY, 
        0 NETMINIBARREV_M_LY, 
        0 NETTELEREV_M_LY, 
        0 NETMISCREV_M_LY, 
        0 NETREV_M_LY, 
        0 GROSSROOMREV_M_LY,
        0 GROSSFBREV_M_LY, 
        0 GROSSMINIBARREV_M_LY,
        0 GROSSTELEREV_M_LY,
        0 GROSSMISCREV_M_LY,
        0 GROSSREV_M_LY, 
        0 NOOFROOMS_Y_LY, 
        0 ADULTNO_Y_LY,
        0 KIDS_Y_LY, 
        0 ARRIVALS_Y_LY,
        0 DEPARTURES_Y_LY, 
        0 ARR_ADULTNO_Y_LY, 
        0 DEP_ADULTNO_Y_LY, 
        0 ARR_KIDS_Y_LY, 
        0 DEP_KIDS_Y_LY, 
        0 NOSHOW_ROOM_Y_LY, 
        0 CANCEL_ROOM_Y_LY, 
        0 NETROOMREV_Y_LY, 
        0 NETFBREV_Y_LY, 
        0 NETMINIBARREV_Y_LY, 
        0 NETTELEREV_Y_LY, 
        0 NETMISCREV_Y_LY, 
        0 NETREV_Y_LY,
        0 GROSSROOMREV_Y_LY,
        0 GROSSFBREV_Y_LY,
        0 GROSSMINIBARREV_Y_LY, 
        0 GROSSTELEREV_Y_LY, 
        0 GROSSMISCREV_Y_LY, 
        0 GROSSREV_Y_LY
    FROM V8_STAT_SHIS_XCMS_1_YEAR
    UNION ALL
    SELECT
        WDAT_DATE,
        SHIS_WPHS_ID,
        XCMS_ID,
        0 NOOFROOMS_D,
        0 ADULTNO_D,
        0 KIDS_D,
        0 ARRIVALS_D,
        0 DEPARTURES_D,
        0 ARR_ADULTNO_D, 
        0 DEP_ADULTNO_D,
        0 ARR_KIDS_D,
        0 DEP_KIDS_D,
        0 NOSHOW_ROOM_D,
        0 CANCEL_ROOM_D,
        0 NETROOMREV_D,
        0 NETFBREV_D,
        0 NETMINIBARREV_D,
        0 NETTELEREV_D,
        0 NETMISCREV_D, 
        0 NETREV_D,
        0 GROSSROOMREV_D,
        0 GROSSFBREV_D,
        0 GROSSMINIBARREV_D, 
        0 GROSSTELEREV_D,
        0 GROSSMISCREV_D,
        0 GROSSREV_D,
        0 NOOFROOMS_M,
        0 ADULTNO_M,
        0 KIDS_M,
        0 ARRIVALS_M,
        0 DEPARTURES_M,
        0 ARR_ADULTNO_M, 
        0 DEP_ADULTNO_M,
        0 ARR_KIDS_M,
        0 DEP_KIDS_M,
        0 NOSHOW_ROOM_M,
        0 CANCEL_ROOM_M,
        0 NETROOMREV_M,
        0 NETFBREV_M,
        0 NETMINIBARREV_M,
        0 NETTELEREV_M,
        0 NETMISCREV_M, 
        0 NETREV_M,
        0 GROSSROOMREV_M,
        0 GROSSFBREV_M,
        0 GROSSMINIBARREV_M, 
        0 GROSSTELEREV_M,
        0 GROSSMISCREV_M,
        0 GROSSREV_M,
        0 NOOFROOMS_Y,
        0 ADULTNO_Y,
        0 KIDS_Y,
        0 ARRIVALS_Y,
        0 DEPARTURES_Y,
        0 ARR_ADULTNO_Y, 
        0 DEP_ADULTNO_Y,
        0 ARR_KIDS_Y,
        0 DEP_KIDS_Y,
        0 NOSHOW_ROOM_Y,
        0 CANCEL_ROOM_Y,
        0 NETROOMREV_Y,
        0 NETFBREV_Y,
        0 NETMINIBARREV_Y,
        0 NETTELEREV_Y,
        0 NETMISCREV_Y, 
        0 NETREV_Y,
        0 GROSSROOMREV_Y,
        0 GROSSFBREV_Y,
        0 GROSSMINIBARREV_Y, 
        0 GROSSTELEREV_Y,
        0 GROSSMISCREV_Y,
        0 GROSSREV_Y,
        NOOFROOMS NOOFROOMS_D_LY,
        ADULTNO ADULTNO_D_LY,
        KIDS KIDS_D_LY,
        ARRIVALS ARRIVALS_D_LY,
        DEPARTURES DEPARTURES_D_LY,
        ARR_ADULTNO ARR_ADULTNO_D_LY, 
        DEP_ADULTNO DEP_ADULTNO_D_LY,
        ARR_KIDS ARR_KIDS_D_LY,
        DEP_KIDS DEP_KIDS_D_LY,
        NOSHOW_ROOM NOSHOW_ROOM_D_LY,
        CANCEL_ROOM CANCEL_ROOM_D_LY,
        NETROOMREV NETROOMREV_D_LY,
        NETFBREV NETFBREV_D_LY,
        NETMINIBARREV NETMINIBARREV_D_LY,
        NETTELEREV NETTELEREV_D_LY,
        NETMISCREV NETMISCREV_D_LY, 
        NETREV NETREV_D_LY,
        GROSSROOMREV GROSSROOMREV_D_LY,
        GROSSFBREV GROSSFBREV_D_LY,
        GROSSMINIBARREV GROSSMINIBARREV_D_LY, 
        GROSSTELEREV GROSSTELEREV_D_LY,
        GROSSMISCREV GROSSMISCREV_D_LY,
        GROSSREV GROSSREV_D_LY,
        0 NOOFROOMS_M_LY,
        0 ADULTNO_M_LY, 
        0 KIDS_M_LY, 
        0 ARRIVALS_M_LY,
        0 DEPARTURES_M_LY,
        0 ARR_ADULTNO_M_LY, 
        0 DEP_ADULTNO_M_LY,
        0 ARR_KIDS_M_LY,
        0 DEP_KIDS_M_LY, 
        0 NOSHOW_ROOM_M_LY, 
        0 CANCEL_ROOM_M_LY, 
        0 NETROOMREV_M_LY, 
        0 NETFBREV_M_LY, 
        0 NETMINIBARREV_M_LY, 
        0 NETTELEREV_M_LY, 
        0 NETMISCREV_M_LY, 
        0 NETREV_M_LY, 
        0 GROSSROOMREV_M_LY,
        0 GROSSFBREV_M_LY, 
        0 GROSSMINIBARREV_M_LY,
        0 GROSSTELEREV_M_LY,
        0 GROSSMISCREV_M_LY,
        0 GROSSREV_M_LY, 
        0 NOOFROOMS_Y_LY, 
        0 ADULTNO_Y_LY,
        0 KIDS_Y_LY, 
        0 ARRIVALS_Y_LY,
        0 DEPARTURES_Y_LY, 
        0 ARR_ADULTNO_Y_LY, 
        0 DEP_ADULTNO_Y_LY, 
        0 ARR_KIDS_Y_LY, 
        0 DEP_KIDS_Y_LY, 
        0 NOSHOW_ROOM_Y_LY, 
        0 CANCEL_ROOM_Y_LY, 
        0 NETROOMREV_Y_LY, 
        0 NETFBREV_Y_LY, 
        0 NETMINIBARREV_Y_LY, 
        0 NETTELEREV_Y_LY, 
        0 NETMISCREV_Y_LY, 
        0 NETREV_Y_LY,
        0 GROSSROOMREV_Y_LY,
        0 GROSSFBREV_Y_LY,
        0 GROSSMINIBARREV_Y_LY, 
        0 GROSSTELEREV_Y_LY, 
        0 GROSSMISCREV_Y_LY, 
        0 GROSSREV_Y_LY
    FROM V8_STAT_SHIS_XCMS_1_DAY_LY 
    UNION ALL
    SELECT
        WDAT_DATE,
        SHIS_WPHS_ID,
        XCMS_ID,
        0 NOOFROOMS_D,
        0 ADULTNO_D,
        0 KIDS_D,
        0 ARRIVALS_D,
        0 DEPARTURES_D,
        0 ARR_ADULTNO_D, 
        0 DEP_ADULTNO_D,
        0 ARR_KIDS_D,
        0 DEP_KIDS_D,
        0 NOSHOW_ROOM_D,
        0 CANCEL_ROOM_D,
        0 NETROOMREV_D,
        0 NETFBREV_D,
        0 NETMINIBARREV_D,
        0 NETTELEREV_D,
        0 NETMISCREV_D, 
        0 NETREV_D,
        0 GROSSROOMREV_D,
        0 GROSSFBREV_D,
        0 GROSSMINIBARREV_D, 
        0 GROSSTELEREV_D,
        0 GROSSMISCREV_D,
        0 GROSSREV_D,
        0 NOOFROOMS_M,
        0 ADULTNO_M,
        0 KIDS_M,
        0 ARRIVALS_M,
        0 DEPARTURES_M,
        0 ARR_ADULTNO_M, 
        0 DEP_ADULTNO_M,
        0 ARR_KIDS_M,
        0 DEP_KIDS_M,
        0 NOSHOW_ROOM_M,
        0 CANCEL_ROOM_M,
        0 NETROOMREV_M,
        0 NETFBREV_M,
        0 NETMINIBARREV_M,
        0 NETTELEREV_M,
        0 NETMISCREV_M, 
        0 NETREV_M,
        0 GROSSROOMREV_M,
        0 GROSSFBREV_M,
        0 GROSSMINIBARREV_M, 
        0 GROSSTELEREV_M,
        0 GROSSMISCREV_M,
        0 GROSSREV_M,
        0 NOOFROOMS_Y,
        0 ADULTNO_Y,
        0 KIDS_Y,
        0 ARRIVALS_Y,
        0 DEPARTURES_Y,
        0 ARR_ADULTNO_Y, 
        0 DEP_ADULTNO_Y,
        0 ARR_KIDS_Y,
        0 DEP_KIDS_Y,
        0 NOSHOW_ROOM_Y,
        0 CANCEL_ROOM_Y,
        0 NETROOMREV_Y,
        0 NETFBREV_Y,
        0 NETMINIBARREV_Y,
        0 NETTELEREV_Y,
        0 NETMISCREV_Y, 
        0 NETREV_Y,
        0 GROSSROOMREV_Y,
        0 GROSSFBREV_Y,
        0 GROSSMINIBARREV_Y, 
        0 GROSSTELEREV_Y,
        0 GROSSMISCREV_Y,
        0 GROSSREV_Y,
        0 NOOFROOMS_D_LY, 
        0 ADULTNO_D_LY, 
        0 KIDS_D_LY, 
        0 ARRIVALS_D_LY, 
        0 DEPARTURES_D_LY, 
        0 ARR_ADULTNO_D_LY, 
        0 DEP_ADULTNO_D_LY, 
        0 ARR_KIDS_D_LY, 
        0 DEP_KIDS_D_LY, 
        0 NOSHOW_ROOM_D_LY,
        0 CANCEL_ROOM_D_LY, 
        0 NETROOMREV_D_LY, 
        0 NETFBREV_D_LY, 
        0 NETMINIBARREV_D_LY, 
        0 NETTELEREV_D_LY, 
        0 NETMISCREV_D_LY, 
        0 NETREV_D_LY, 
        0 GROSSROOMREV_D_LY,
        0 GROSSFBREV_D_LY, 
        0 GROSSMINIBARREV_D_LY,
        0 GROSSTELEREV_D_LY,
        0 GROSSMISCREV_D_LY, 
        0 GROSSREV_D_LY,
        NOOFROOMS NOOFROOMS_M_LY,
        ADULTNO ADULTNO_M_LY,
        KIDS KIDS_M_LY,
        ARRIVALS ARRIVALS_M_LY,
        DEPARTURES DEPARTURES_M_LY,
        ARR_ADULTNO ARR_ADULTNO_M_LY, 
        DEP_ADULTNO DEP_ADULTNO_M_LY,
        ARR_KIDS ARR_KIDS_M_LY,
        DEP_KIDS DEP_KIDS_M_LY,
        NOSHOW_ROOM NOSHOW_ROOM_M_LY,
        CANCEL_ROOM CANCEL_ROOM_M_LY,
        NETROOMREV NETROOMREV_M_LY,
        NETFBREV NETFBREV_M_LY,
        NETMINIBARREV NETMINIBARREV_M_LY,
        NETTELEREV NETTELEREV_M_LY,
        NETMISCREV NETMISCREV_M_LY, 
        NETREV NETREV_M_LY,
        GROSSROOMREV GROSSROOMREV_M_LY,
        GROSSFBREV GROSSFBREV_M_LY,
        GROSSMINIBARREV GROSSMINIBARREV_M_LY, 
        GROSSTELEREV GROSSTELEREV_M_LY,
        GROSSMISCREV GROSSMISCREV_M_LY,
        GROSSREV GROSSREV_M_LY, 
        0 NOOFROOMS_Y_LY, 
        0 ADULTNO_Y_LY,
        0 KIDS_Y_LY, 
        0 ARRIVALS_Y_LY,
        0 DEPARTURES_Y_LY, 
        0 ARR_ADULTNO_Y_LY, 
        0 DEP_ADULTNO_Y_LY, 
        0 ARR_KIDS_Y_LY, 
        0 DEP_KIDS_Y_LY, 
        0 NOSHOW_ROOM_Y_LY, 
        0 CANCEL_ROOM_Y_LY, 
        0 NETROOMREV_Y_LY, 
        0 NETFBREV_Y_LY, 
        0 NETMINIBARREV_Y_LY, 
        0 NETTELEREV_Y_LY, 
        0 NETMISCREV_Y_LY, 
        0 NETREV_Y_LY,
        0 GROSSROOMREV_Y_LY,
        0 GROSSFBREV_Y_LY,
        0 GROSSMINIBARREV_Y_LY, 
        0 GROSSTELEREV_Y_LY, 
        0 GROSSMISCREV_Y_LY, 
        0 GROSSREV_Y_LY
    FROM V8_STAT_SHIS_XCMS_1_MONTH_LY
    UNION ALL
    SELECT
        WDAT_DATE,
        SHIS_WPHS_ID,
        XCMS_ID,
        0 NOOFROOMS_D,
        0 ADULTNO_D,
        0 KIDS_D,
        0 ARRIVALS_D,
        0 DEPARTURES_D,
        0 ARR_ADULTNO_D, 
        0 DEP_ADULTNO_D,
        0 ARR_KIDS_D,
        0 DEP_KIDS_D,
        0 NOSHOW_ROOM_D,
        0 CANCEL_ROOM_D,
        0 NETROOMREV_D,
        0 NETFBREV_D,
        0 NETMINIBARREV_D,
        0 NETTELEREV_D,
        0 NETMISCREV_D, 
        0 NETREV_D,
        0 GROSSROOMREV_D,
        0 GROSSFBREV_D,
        0 GROSSMINIBARREV_D, 
        0 GROSSTELEREV_D,
        0 GROSSMISCREV_D,
        0 GROSSREV_D,
        0 NOOFROOMS_M,
        0 ADULTNO_M,
        0 KIDS_M,
        0 ARRIVALS_M,
        0 DEPARTURES_M,
        0 ARR_ADULTNO_M, 
        0 DEP_ADULTNO_M,
        0 ARR_KIDS_M,
        0 DEP_KIDS_M,
        0 NOSHOW_ROOM_M,
        0 CANCEL_ROOM_M,
        0 NETROOMREV_M,
        0 NETFBREV_M,
        0 NETMINIBARREV_M,
        0 NETTELEREV_M,
        0 NETMISCREV_M, 
        0 NETREV_M,
        0 GROSSROOMREV_M,
        0 GROSSFBREV_M,
        0 GROSSMINIBARREV_M, 
        0 GROSSTELEREV_M,
        0 GROSSMISCREV_M,
        0 GROSSREV_M,
        0 NOOFROOMS_Y,
        0 ADULTNO_Y,
        0 KIDS_Y,
        0 ARRIVALS_Y,
        0 DEPARTURES_Y,
        0 ARR_ADULTNO_Y, 
        0 DEP_ADULTNO_Y,
        0 ARR_KIDS_Y,
        0 DEP_KIDS_Y,
        0 NOSHOW_ROOM_Y,
        0 CANCEL_ROOM_Y,
        0 NETROOMREV_Y,
        0 NETFBREV_Y,
        0 NETMINIBARREV_Y,
        0 NETTELEREV_Y,
        0 NETMISCREV_Y, 
        0 NETREV_Y,
        0 GROSSROOMREV_Y,
        0 GROSSFBREV_Y,
        0 GROSSMINIBARREV_Y, 
        0 GROSSTELEREV_Y,
        0 GROSSMISCREV_Y,
        0 GROSSREV_Y,
        0 NOOFROOMS_D_LY, 
        0 ADULTNO_D_LY, 
        0 KIDS_D_LY, 
        0 ARRIVALS_D_LY, 
        0 DEPARTURES_D_LY, 
        0 ARR_ADULTNO_D_LY, 
        0 DEP_ADULTNO_D_LY, 
        0 ARR_KIDS_D_LY, 
        0 DEP_KIDS_D_LY, 
        0 NOSHOW_ROOM_D_LY,
        0 CANCEL_ROOM_D_LY, 
        0 NETROOMREV_D_LY, 
        0 NETFBREV_D_LY, 
        0 NETMINIBARREV_D_LY, 
        0 NETTELEREV_D_LY, 
        0 NETMISCREV_D_LY, 
        0 NETREV_D_LY, 
        0 GROSSROOMREV_D_LY,
        0 GROSSFBREV_D_LY, 
        0 GROSSMINIBARREV_D_LY,
        0 GROSSTELEREV_D_LY,
        0 GROSSMISCREV_D_LY, 
        0 GROSSREV_D_LY,
        0 NOOFROOMS_M_LY,
        0 ADULTNO_M_LY, 
        0 KIDS_M_LY, 
        0 ARRIVALS_M_LY,
        0 DEPARTURES_M_LY,
        0 ARR_ADULTNO_M_LY, 
        0 DEP_ADULTNO_M_LY,
        0 ARR_KIDS_M_LY,
        0 DEP_KIDS_M_LY, 
        0 NOSHOW_ROOM_M_LY, 
        0 CANCEL_ROOM_M_LY, 
        0 NETROOMREV_M_LY, 
        0 NETFBREV_M_LY, 
        0 NETMINIBARREV_M_LY, 
        0 NETTELEREV_M_LY, 
        0 NETMISCREV_M_LY, 
        0 NETREV_M_LY, 
        0 GROSSROOMREV_M_LY,
        0 GROSSFBREV_M_LY, 
        0 GROSSMINIBARREV_M_LY,
        0 GROSSTELEREV_M_LY,
        0 GROSSMISCREV_M_LY,
        0 GROSSREV_M_LY,  
        NOOFROOMS NOOFROOMS_Y_LY,
        ADULTNO ADULTNO_Y_LY,
        KIDS KIDS_Y_LY,
        ARRIVALS ARRIVALS_Y_LY,
        DEPARTURES DEPARTURES_Y_LY,
        ARR_ADULTNO ARR_ADULTNO_Y_LY, 
        DEP_ADULTNO DEP_ADULTNO_Y_LY,
        ARR_KIDS ARR_KIDS_Y_LY,
        DEP_KIDS DEP_KIDS_Y_LY,
        NOSHOW_ROOM NOSHOW_ROOM_Y_LY,
        CANCEL_ROOM CANCEL_ROOM_Y_LY,
        NETROOMREV NETROOMREV_Y_LY,
        NETFBREV NETFBREV_Y_LY,
        NETMINIBARREV NETMINIBARREV_Y_LY,
        NETTELEREV NETTELEREV_Y_LY,
        NETMISCREV NETMISCREV_Y_LY, 
        NETREV NETREV_Y_LY,
        GROSSROOMREV GROSSROOMREV_Y_LY,
        GROSSFBREV GROSSFBREV_Y_LY,
        GROSSMINIBARREV GROSSMINIBARREV_Y_LY, 
        GROSSTELEREV GROSSTELEREV_Y_LY,
        GROSSMISCREV GROSSMISCREV_Y_LY,
        GROSSREV GROSSREV_Y_LY
    FROM V8_STAT_SHIS_XCMS_1_YEAR_LY)
GROUP BY WDAT_DATE,XCMS_ID,SHIS_WPHS_ID;

CREATE OR REPLACE VIEW V8_STAT_SHIS_XCMS_2_DAY_LY
AS 
SELECT 
  WDAT_DATE, 
  shis_wphs_id,  
  XCMS_ID, 
  sum(NOOFROOMS) NOOFROOMS, 
  sum(ADULTNO) ADULTNO, 
  sum(KIDS) KIDS, 
  sum(ARRIVALS) ARRIVALS, 
  sum(DEPARTURES) DEPARTURES, 
  sum(ARR_ADULTNO) ARR_ADULTNO, 
  sum(DEP_ADULTNO) DEP_ADULTNO, 
  sum(ARR_KIDS) ARR_KIDS, 
  sum(DEP_KIDS) DEP_KIDS, 
  sum(NOSHOW_ROOM) NOSHOW_ROOM, 
  sum(CANCEL_ROOM) CANCEL_ROOM, 
  sum(NETROOMREV) NETROOMREV, 
  sum(NETFBREV) NETFBREV, 
  sum(NETMINIBARREV) NETMINIBARREV, 
  sum(NETTELEREV) NETTELEREV, 
  sum(NETMISCREV) NETMISCREV, 
  sum(NETREV) NETREV, 
  sum(GROSSROOMREV) GROSSROOMREV, 
  sum(GROSSFBREV) GROSSFBREV, 
  sum(GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(GROSSTELEREV) GROSSTELEREV, 
  sum(GROSSMISCREV) GROSSMISCREV, 
  sum(GROSSREV) GROSSREV 
FROM 
( 
SELECT 
  wdat.WDAT_DATE WDAT_DATE, 
  s.shis_wphs_id shis_wphs_id,
  S.SHIS_YCLN_2_XCMS_ID XCMS_ID, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,0,decode(S.SHIS_DEP_ID,1,0,S.SHIS_YDET_NOOFROOMS))) NOOFROOMS,
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN S.SHIS_YDET_ADULTNO WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END )) ADULTNO, 
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN NVL(S.SHIS_TOTALYRGL_NUMBER,0) WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER,0) END )) KIDS, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_NOOFROOMS,0)) ARRIVALS, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_NOOFROOMS,0)) DEPARTURES, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_ADULTNO,0)) ARR_ADULTNO, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_ADULTNO,0)) DEP_ADULTNO, 
  sum(decode(S.SHIS_ARR_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) ARR_KIDS, 
  sum(decode(S.SHIS_DEP_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) DEP_KIDS, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,4,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) NOSHOW_ROOM,
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,3,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) CANCEL_ROOM,
  sum(S.SHIS_NETROOMREV) NETROOMREV, 
  sum(S.SHIS_NETFBREV) NETFBREV, 
  sum(S.SHIS_NETMINIBARREV) NETMINIBARREV, 
  sum(S.SHIS_NETTELEREV) NETTELEREV, 
  sum(S.SHIS_NETMISCREV) NETMISCREV, 
  sum(S.SHIS_NETREV) NETREV, 
  sum(S.SHIS_GROSSROOMREV) GROSSROOMREV, 
  sum(S.SHIS_GROSSFBREV) GROSSFBREV, 
  sum(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(S.SHIS_GROSSTELEREV) GROSSTELEREV, 
  sum(S.SHIS_GROSSMISCREV) GROSSMISCREV, 
 sum(S.SHIS_GROSSREV) GROSSREV 
FROM 
  wdat, 
  shis S 
  WHERE 
add_months(wdat.WDAT_DATE,-12)=S.SHIS_WDAT_DATE 
group by wdat.WDAT_DATE,S.SHIS_YCLN_2_XCMS_ID, shis_wphs_id
union 
SELECT 
  wdat.WDAT_DATE WDAT_DATE, 
  s.shis_wphs_id shis_wphs_id,  
  S.SHIS_XCMS_ID XCMS_ID, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,0,decode(S.SHIS_DEP_ID,1,0,S.SHIS_YDET_NOOFROOMS))) NOOFROOMS,
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN S.SHIS_YDET_ADULTNO WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END )) ADULTNO, 
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN NVL(S.SHIS_TOTALYRGL_NUMBER,0) WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER,0) END )) KIDS, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_NOOFROOMS,0)) ARRIVALS, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_NOOFROOMS,0)) DEPARTURES, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_ADULTNO,0)) ARR_ADULTNO, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_ADULTNO,0)) DEP_ADULTNO, 
  sum(decode(S.SHIS_ARR_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) ARR_KIDS, 
  sum(decode(S.SHIS_DEP_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) DEP_KIDS, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,4,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) NOSHOW_ROOM,
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,3,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) CANCEL_ROOM,
  sum(S.SHIS_NETROOMREV) NETROOMREV, 
  sum(S.SHIS_NETFBREV) NETFBREV, 
  sum(S.SHIS_NETMINIBARREV) NETMINIBARREV, 
  sum(S.SHIS_NETTELEREV) NETTELEREV, 
  sum(S.SHIS_NETMISCREV) NETMISCREV, 
  sum(S.SHIS_NETREV) NETREV, 
  sum(S.SHIS_GROSSROOMREV) GROSSROOMREV, 
  sum(S.SHIS_GROSSFBREV) GROSSFBREV, 
  sum(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(S.SHIS_GROSSTELEREV) GROSSTELEREV, 
  sum(S.SHIS_GROSSMISCREV) GROSSMISCREV, 
 sum(S.SHIS_GROSSREV) GROSSREV 
FROM 
  wdat, 
  shis S 
  WHERE 
add_months(wdat.WDAT_DATE,-12)=S.SHIS_WDAT_DATE 
and (select xcca.XCCA_INTERNALCATEGORY from xcca,xcms where xcca.XCCA_ID=xcms.XCMS_XCCA_ID and xcms.XCMS_ID=S.SHIS_XCMS_ID)=2
group by wdat.WDAT_DATE,S.SHIS_XCMS_ID, shis_wphs_id
) 
group by WDAT_DATE,XCMS_ID, shis_wphs_id;

CREATE OR REPLACE VIEW V8_STAT_SHIS_XCMS_2_MONTH_LY
AS 
SELECT 
WDAT_DATE WDAT_DATE,
    SHIS_WPHS_ID SHIS_WPHS_ID,
    NVL(XCMS_ID,0) XCMS_ID,
    SUM(NOOFROOMS) NOOFROOMS,
    SUM(ADULTNO) ADULTNO,
    SUM(KIDS) KIDS,
    SUM(ARRIVALS) ARRIVALS,
    SUM(DEPARTURES) DEPARTURES,
    SUM(ARR_ADULTNO) ARR_ADULTNO,
    SUM(DEP_ADULTNO) DEP_ADULTNO,
    SUM(ARR_KIDS) ARR_KIDS,
    SUM(DEP_KIDS) DEP_KIDS,
    SUM(NOSHOW_ROOM) NOSHOW_ROOM,
    SUM(CANCEL_ROOM) CANCEL_ROOM,
    SUM(NETROOMREV) NETROOMREV,
    SUM(NETFBREV) NETFBREV,
    SUM(NETMINIBARREV) NETMINIBARREV,
    SUM(NETTELEREV) NETTELEREV,
    SUM(NETMISCREV) NETMISCREV,
    SUM(NETREV) NETREV,
    SUM(GROSSROOMREV) GROSSROOMREV,
    SUM(GROSSFBREV) GROSSFBREV,
    SUM(GROSSMINIBARREV) GROSSMINIBARREV,
    SUM(GROSSTELEREV) GROSSTELEREV,
    SUM(GROSSMISCREV) GROSSMISCREV,
    SUM(GROSSREV) GROSSREV 
FROM 
( 
SELECT 
  wdat.WDAT_DATE WDAT_DATE, 
  s.shis_wphs_id shis_wphs_id,
  S.SHIS_YCLN_2_XCMS_ID XCMS_ID, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,0,decode(S.SHIS_DEP_ID,1,0,S.SHIS_YDET_NOOFROOMS))) NOOFROOMS,
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN S.SHIS_YDET_ADULTNO WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END )) ADULTNO, 
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN NVL(S.SHIS_TOTALYRGL_NUMBER,0) WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER,0) END )) KIDS, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_NOOFROOMS,0)) ARRIVALS, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_NOOFROOMS,0)) DEPARTURES, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_ADULTNO,0)) ARR_ADULTNO, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_ADULTNO,0)) DEP_ADULTNO, 
  sum(decode(S.SHIS_ARR_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) ARR_KIDS, 
  sum(decode(S.SHIS_DEP_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) DEP_KIDS, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,4,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) NOSHOW_ROOM,
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,3,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) CANCEL_ROOM,
  sum(S.SHIS_NETROOMREV) NETROOMREV, 
  sum(S.SHIS_NETFBREV) NETFBREV, 
  sum(S.SHIS_NETMINIBARREV) NETMINIBARREV, 
  sum(S.SHIS_NETTELEREV) NETTELEREV, 
  sum(S.SHIS_NETMISCREV) NETMISCREV, 
  sum(S.SHIS_NETREV) NETREV, 
  sum(S.SHIS_GROSSROOMREV) GROSSROOMREV, 
  sum(S.SHIS_GROSSFBREV) GROSSFBREV, 
  sum(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(S.SHIS_GROSSTELEREV) GROSSTELEREV, 
  sum(S.SHIS_GROSSMISCREV) GROSSMISCREV, 
 sum(S.SHIS_GROSSREV) GROSSREV 
FROM 
  wdat, 
  shis S 
  WHERE 
add_months(add_months(last_day(wdat.WDAT_DATE),-1)+1,-12)<=S.SHIS_WDAT_DATE and 
S.SHIS_WDAT_DATE<=add_months(wdat.WDAT_DATE,-12) 
group by wdat.WDAT_DATE,S.SHIS_YCLN_2_XCMS_ID, shis_wphs_id
union 
SELECT 
  wdat.WDAT_DATE WDAT_DATE, 
  s.shis_wphs_id shis_wphs_id,  
  S.SHIS_XCMS_ID XCMS_ID, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,0,decode(S.SHIS_DEP_ID,1,0,S.SHIS_YDET_NOOFROOMS))) NOOFROOMS,
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN S.SHIS_YDET_ADULTNO WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END )) ADULTNO, 
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN NVL(S.SHIS_TOTALYRGL_NUMBER,0) WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER,0) END )) KIDS, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_NOOFROOMS,0)) ARRIVALS, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_NOOFROOMS,0)) DEPARTURES, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_ADULTNO,0)) ARR_ADULTNO, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_ADULTNO,0)) DEP_ADULTNO, 
  sum(decode(S.SHIS_ARR_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) ARR_KIDS, 
  sum(decode(S.SHIS_DEP_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) DEP_KIDS, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,4,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) NOSHOW_ROOM,
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,3,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) CANCEL_ROOM,
  sum(S.SHIS_NETROOMREV) NETROOMREV, 
  sum(S.SHIS_NETFBREV) NETFBREV, 
  sum(S.SHIS_NETMINIBARREV) NETMINIBARREV, 
  sum(S.SHIS_NETTELEREV) NETTELEREV, 
  sum(S.SHIS_NETMISCREV) NETMISCREV, 
  sum(S.SHIS_NETREV) NETREV, 
  sum(S.SHIS_GROSSROOMREV) GROSSROOMREV, 
  sum(S.SHIS_GROSSFBREV) GROSSFBREV, 
  sum(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(S.SHIS_GROSSTELEREV) GROSSTELEREV, 
  sum(S.SHIS_GROSSMISCREV) GROSSMISCREV, 
 sum(S.SHIS_GROSSREV) GROSSREV 
FROM 
  wdat, 
  shis S 
  WHERE 
add_months(add_months(last_day(wdat.WDAT_DATE),-1)+1,-12)<=S.SHIS_WDAT_DATE and 
S.SHIS_WDAT_DATE<=add_months(wdat.WDAT_DATE,-12) 
and (select xcca.XCCA_INTERNALCATEGORY from xcca,xcms where xcca.XCCA_ID=xcms.XCMS_XCCA_ID and xcms.XCMS_ID=S.SHIS_XCMS_ID)=2 
group by wdat.WDAT_DATE,S.SHIS_XCMS_ID, shis_wphs_id 
) 
group by WDAT_DATE,XCMS_ID, shis_wphs_id;

CREATE OR REPLACE VIEW V8_STAT_SHIS_XCMS_2_YEAR_LY
AS 
SELECT 
WDAT_DATE WDAT_DATE,
    SHIS_WPHS_ID SHIS_WPHS_ID,
    NVL(XCMS_ID,0) XCMS_ID,
    SUM(NOOFROOMS) NOOFROOMS,
    SUM(ADULTNO) ADULTNO,
    SUM(KIDS) KIDS,
    SUM(ARRIVALS) ARRIVALS,
    SUM(DEPARTURES) DEPARTURES,
    SUM(ARR_ADULTNO) ARR_ADULTNO,
    SUM(DEP_ADULTNO) DEP_ADULTNO,
    SUM(ARR_KIDS) ARR_KIDS,
    SUM(DEP_KIDS) DEP_KIDS,
    SUM(NOSHOW_ROOM) NOSHOW_ROOM,
    SUM(CANCEL_ROOM) CANCEL_ROOM,
    SUM(NETROOMREV) NETROOMREV,
    SUM(NETFBREV) NETFBREV,
    SUM(NETMINIBARREV) NETMINIBARREV,
    SUM(NETTELEREV) NETTELEREV,
    SUM(NETMISCREV) NETMISCREV,
    SUM(NETREV) NETREV,
    SUM(GROSSROOMREV) GROSSROOMREV,
    SUM(GROSSFBREV) GROSSFBREV,
    SUM(GROSSMINIBARREV) GROSSMINIBARREV,
    SUM(GROSSTELEREV) GROSSTELEREV,
    SUM(GROSSMISCREV) GROSSMISCREV,
    SUM(GROSSREV) GROSSREV
FROM 
( 
SELECT 
  wdat.WDAT_DATE WDAT_DATE, 
  s.shis_wphs_id shis_wphs_id,    
  S.SHIS_YCLN_2_XCMS_ID XCMS_ID, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,0,decode(S.SHIS_DEP_ID,1,0,S.SHIS_YDET_NOOFROOMS))) NOOFROOMS,
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN S.SHIS_YDET_ADULTNO WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END )) ADULTNO, 
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN NVL(S.SHIS_TOTALYRGL_NUMBER,0) WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER,0) END )) KIDS, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_NOOFROOMS,0)) ARRIVALS, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_NOOFROOMS,0)) DEPARTURES, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_ADULTNO,0)) ARR_ADULTNO, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_ADULTNO,0)) DEP_ADULTNO, 
  sum(decode(S.SHIS_ARR_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) ARR_KIDS, 
  sum(decode(S.SHIS_DEP_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) DEP_KIDS, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,4,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) NOSHOW_ROOM,
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,3,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) CANCEL_ROOM,
  sum(S.SHIS_NETROOMREV) NETROOMREV, 
  sum(S.SHIS_NETFBREV) NETFBREV, 
  sum(S.SHIS_NETMINIBARREV) NETMINIBARREV, 
  sum(S.SHIS_NETTELEREV) NETTELEREV, 
  sum(S.SHIS_NETMISCREV) NETMISCREV, 
  sum(S.SHIS_NETREV) NETREV, 
  sum(S.SHIS_GROSSROOMREV) GROSSROOMREV, 
  sum(S.SHIS_GROSSFBREV) GROSSFBREV, 
  sum(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(S.SHIS_GROSSTELEREV) GROSSTELEREV, 
  sum(S.SHIS_GROSSMISCREV) GROSSMISCREV, 
 sum(S.SHIS_GROSSREV) GROSSREV 
FROM 
  wdat, 
  shis S 
  WHERE 
add_months(add_months(last_day(wdat.WDAT_DATE),-to_number(to_char(wdat_date,'mm')))+1,-12)<=S.SHIS_WDAT_DATE and 
S.SHIS_WDAT_DATE<=add_months(wdat.WDAT_DATE,-12)
group by wdat.WDAT_DATE,S.SHIS_YCLN_2_XCMS_ID, shis_wphs_id
union 
SELECT 
  wdat.WDAT_DATE WDAT_DATE, 
  s.shis_wphs_id shis_wphs_id,  
  S.SHIS_XCMS_ID XCMS_ID, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,0,decode(S.SHIS_DEP_ID,1,0,S.SHIS_YDET_NOOFROOMS))) NOOFROOMS,
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN S.SHIS_YDET_ADULTNO WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END )) ADULTNO, 
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN NVL(S.SHIS_TOTALYRGL_NUMBER,0) WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER,0) END )) KIDS, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_NOOFROOMS,0)) ARRIVALS, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_NOOFROOMS,0)) DEPARTURES, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_ADULTNO,0)) ARR_ADULTNO, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_ADULTNO,0)) DEP_ADULTNO, 
  sum(decode(S.SHIS_ARR_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) ARR_KIDS, 
  sum(decode(S.SHIS_DEP_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) DEP_KIDS, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,4,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) NOSHOW_ROOM,
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,3,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) CANCEL_ROOM,
  sum(S.SHIS_NETROOMREV) NETROOMREV, 
  sum(S.SHIS_NETFBREV) NETFBREV, 
  sum(S.SHIS_NETMINIBARREV) NETMINIBARREV, 
  sum(S.SHIS_NETTELEREV) NETTELEREV, 
  sum(S.SHIS_NETMISCREV) NETMISCREV, 
  sum(S.SHIS_NETREV) NETREV, 
  sum(S.SHIS_GROSSROOMREV) GROSSROOMREV, 
  sum(S.SHIS_GROSSFBREV) GROSSFBREV, 
  sum(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(S.SHIS_GROSSTELEREV) GROSSTELEREV, 
  sum(S.SHIS_GROSSMISCREV) GROSSMISCREV, 
 sum(S.SHIS_GROSSREV) GROSSREV 
FROM 
  wdat, 
  shis S 
  WHERE 
add_months(add_months(last_day(wdat.WDAT_DATE),-to_number(to_char(wdat_date,'mm')))+1,-12)<=S.SHIS_WDAT_DATE and 
S.SHIS_WDAT_DATE<=add_months(wdat.WDAT_DATE,-12)  
and (select xcca.XCCA_INTERNALCATEGORY from xcca,xcms where xcca.XCCA_ID=xcms.XCMS_XCCA_ID and xcms.XCMS_ID=S.SHIS_XCMS_ID)=2 
group by wdat.WDAT_DATE,S.SHIS_XCMS_ID, shis_wphs_id 
) 
group by WDAT_DATE,XCMS_ID, shis_wphs_id;

CREATE OR REPLACE VIEW V8_STAT_SHIS_XCMS_2_COMBI_LYSD
AS 
SELECT 
    WDAT_DATE WDAT_DATE,
    SHIS_WPHS_ID SHIS_WPHS_ID,
    XCMS_ID XCMS_ID,
    SUM(NOOFROOMS_D) NOOFROOMS_D,
    SUM(ADULTNO_D) ADULTNO_D,
    SUM(KIDS_D) KIDS_D,
    SUM(ARRIVALS_D) ARRIVALS_D,
    SUM(DEPARTURES_D) DEPARTURES_D,
    SUM(ARR_ADULTNO_D) ARR_ADULTNO_D,
    SUM(DEP_ADULTNO_D) DEP_ADULTNO_D,
    SUM(ARR_KIDS_D) ARR_KIDS_D,
    SUM(DEP_KIDS_D) DEP_KIDS_D,
    SUM(NOSHOW_ROOM_D) NOSHOW_ROOM_D,
    SUM(CANCEL_ROOM_D) CANCEL_ROOM_D, 
    SUM(NETROOMREV_D) NETROOMREV_D, 
    SUM(NETFBREV_D) NETFBREV_D, 
    SUM(NETMINIBARREV_D) NETMINIBARREV_D, 
    SUM(NETTELEREV_D) NETTELEREV_D, 
    SUM(NETMISCREV_D) NETMISCREV_D, 
    SUM(NETREV_D) NETREV_D,
    SUM(GROSSROOMREV_D) GROSSROOMREV_D,
    SUM(GROSSFBREV_D) GROSSFBREV_D,
    SUM(GROSSMINIBARREV_D) GROSSMINIBARREV_D,
    SUM(GROSSTELEREV_D) GROSSTELEREV_D,
    SUM(GROSSMISCREV_D) GROSSMISCREV_D,
    SUM(GROSSREV_D) GROSSREV_D,
    SUM(NOOFROOMS_M) NOOFROOMS_M,
    SUM(ADULTNO_M) ADULTNO_M,
    SUM(KIDS_M) KIDS_M,
    SUM(ARRIVALS_M) ARRIVALS_M,
    SUM(DEPARTURES_M) DEPARTURES_M,
    SUM(ARR_ADULTNO_M) ARR_ADULTNO_M,
    SUM(DEP_ADULTNO_M) DEP_ADULTNO_M,
    SUM(ARR_KIDS_M) ARR_KIDS_M,
    SUM(DEP_KIDS_M) DEP_KIDS_M,
    SUM(NOSHOW_ROOM_M) NOSHOW_ROOM_M,
    SUM(CANCEL_ROOM_M) CANCEL_ROOM_M, 
    SUM(NETROOMREV_M) NETROOMREV_M, 
    SUM(NETFBREV_M) NETFBREV_M, 
    SUM(NETMINIBARREV_M) NETMINIBARREV_M, 
    SUM(NETTELEREV_M) NETTELEREV_M, 
    SUM(NETMISCREV_M) NETMISCREV_M, 
    SUM(NETREV_M) NETREV_M,
    SUM(GROSSROOMREV_M) GROSSROOMREV_M,
    SUM(GROSSFBREV_M) GROSSFBREV_M,
    SUM(GROSSMINIBARREV_M) GROSSMINIBARREV_M,
    SUM(GROSSTELEREV_M) GROSSTELEREV_M,
    SUM(GROSSMISCREV_M) GROSSMISCREV_M,
    SUM(GROSSREV_M) GROSSREV_M,
    SUM(NOOFROOMS_Y) NOOFROOMS_Y,
    SUM(ADULTNO_Y) ADULTNO_Y,
    SUM(KIDS_Y) KIDS_Y,
    SUM(ARRIVALS_Y) ARRIVALS_Y,
    SUM(DEPARTURES_Y) DEPARTURES_Y,
    SUM(ARR_ADULTNO_Y) ARR_ADULTNO_Y,
    SUM(DEP_ADULTNO_Y) DEP_ADULTNO_Y,
    SUM(ARR_KIDS_Y) ARR_KIDS_Y,
    SUM(DEP_KIDS_Y) DEP_KIDS_Y,
    SUM(NOSHOW_ROOM_Y) NOSHOW_ROOM_Y,
    SUM(CANCEL_ROOM_Y) CANCEL_ROOM_Y, 
    SUM(NETROOMREV_Y) NETROOMREV_Y, 
    SUM(NETFBREV_Y) NETFBREV_Y, 
    SUM(NETMINIBARREV_Y) NETMINIBARREV_Y, 
    SUM(NETTELEREV_Y) NETTELEREV_Y, 
    SUM(NETMISCREV_Y) NETMISCREV_Y, 
    SUM(NETREV_Y) NETREV_Y,
    SUM(GROSSROOMREV_Y) GROSSROOMREV_Y,
    SUM(GROSSFBREV_Y) GROSSFBREV_Y,
    SUM(GROSSMINIBARREV_Y) GROSSMINIBARREV_Y,
    SUM(GROSSTELEREV_Y) GROSSTELEREV_Y,
    SUM(GROSSMISCREV_Y) GROSSMISCREV_Y,
    SUM(GROSSREV_Y) GROSSREV_Y,
    SUM(NOOFROOMS_D_LY) NOOFROOMS_D_LY,
    SUM(ADULTNO_D_LY) ADULTNO_D_LY,
    SUM(KIDS_D_LY) KIDS_D_LY,
    SUM(ARRIVALS_D_LY) ARRIVALS_D_LY,
    SUM(DEPARTURES_D_LY) DEPARTURES_D_LY,
    SUM(ARR_ADULTNO_D_LY) ARR_ADULTNO_D_LY,
    SUM(DEP_ADULTNO_D_LY) DEP_ADULTNO_D_LY,
    SUM(ARR_KIDS_D_LY) ARR_KIDS_D_LY,
    SUM(DEP_KIDS_D_LY) DEP_KIDS_D_LY,
    SUM(NOSHOW_ROOM_D_LY) NOSHOW_ROOM_D_LY,
    SUM(CANCEL_ROOM_D_LY) CANCEL_ROOM_D_LY, 
    SUM(NETROOMREV_D_LY) NETROOMREV_D_LY, 
    SUM(NETFBREV_D_LY) NETFBREV_D_LY, 
    SUM(NETMINIBARREV_D_LY) NETMINIBARREV_D_LY, 
    SUM(NETTELEREV_D_LY) NETTELEREV_D_LY, 
    SUM(NETMISCREV_D_LY) NETMISCREV_D_LY, 
    SUM(NETREV_D_LY) NETREV_D_LY,
    SUM(GROSSROOMREV_D_LY) GROSSROOMREV_D_LY,
    SUM(GROSSFBREV_D_LY) GROSSFBREV_D_LY,
    SUM(GROSSMINIBARREV_D_LY) GROSSMINIBARREV_D_LY,
    SUM(GROSSTELEREV_D_LY) GROSSTELEREV_D_LY,
    SUM(GROSSMISCREV_D_LY) GROSSMISCREV_D_LY,
    SUM(GROSSREV_D_LY) GROSSREV_D_LY,
    SUM(NOOFROOMS_M_LY) NOOFROOMS_M_LY,
    SUM(ADULTNO_M_LY) ADULTNO_M_LY,
    SUM(KIDS_M_LY) KIDS_M_LY,
    SUM(ARRIVALS_M_LY) ARRIVALS_M_LY,
    SUM(DEPARTURES_M_LY) DEPARTURES_M_LY,
    SUM(ARR_ADULTNO_M_LY) ARR_ADULTNO_M_LY,
    SUM(DEP_ADULTNO_M_LY) DEP_ADULTNO_M_LY,
    SUM(ARR_KIDS_M_LY) ARR_KIDS_M_LY,
    SUM(DEP_KIDS_M_LY) DEP_KIDS_M_LY,
    SUM(NOSHOW_ROOM_M_LY) NOSHOW_ROOM_M_LY,
    SUM(CANCEL_ROOM_M_LY) CANCEL_ROOM_M_LY, 
    SUM(NETROOMREV_M_LY) NETROOMREV_M_LY, 
    SUM(NETFBREV_M_LY) NETFBREV_M_LY, 
    SUM(NETMINIBARREV_M_LY) NETMINIBARREV_M_LY, 
    SUM(NETTELEREV_M_LY) NETTELEREV_M_LY, 
    SUM(NETMISCREV_M_LY) NETMISCREV_M_LY, 
    SUM(NETREV_M_LY) NETREV_M_LY,
    SUM(GROSSROOMREV_M_LY) GROSSROOMREV_M_LY,
    SUM(GROSSFBREV_M_LY) GROSSFBREV_M_LY,
    SUM(GROSSMINIBARREV_M_LY) GROSSMINIBARREV_M_LY,
    SUM(GROSSTELEREV_M_LY) GROSSTELEREV_M_LY,
    SUM(GROSSMISCREV_M_LY) GROSSMISCREV_M_LY,
    SUM(GROSSREV_M_LY) GROSSREV_M_LY,
    SUM(NOOFROOMS_Y_LY) NOOFROOMS_Y_LY,
    SUM(ADULTNO_Y_LY) ADULTNO_Y_LY,
    SUM(KIDS_Y_LY) KIDS_Y_LY,
    SUM(ARRIVALS_Y_LY) ARRIVALS_Y_LY,
    SUM(DEPARTURES_Y_LY) DEPARTURES_Y_LY,
    SUM(ARR_ADULTNO_Y_LY) ARR_ADULTNO_Y_LY,
    SUM(DEP_ADULTNO_Y_LY) DEP_ADULTNO_Y_LY,
    SUM(ARR_KIDS_Y_LY) ARR_KIDS_Y_LY,
    SUM(DEP_KIDS_Y_LY) DEP_KIDS_Y_LY,
    SUM(NOSHOW_ROOM_Y_LY) NOSHOW_ROOM_Y_LY,
    SUM(CANCEL_ROOM_Y_LY) CANCEL_ROOM_Y_LY, 
    SUM(NETROOMREV_Y_LY) NETROOMREV_Y_LY, 
    SUM(NETFBREV_Y_LY) NETFBREV_Y_LY, 
    SUM(NETMINIBARREV_Y_LY) NETMINIBARREV_Y_LY, 
    SUM(NETTELEREV_Y_LY) NETTELEREV_Y_LY, 
    SUM(NETMISCREV_Y_LY) NETMISCREV_Y_LY, 
    SUM(NETREV_Y_LY) NETREV_Y_LY,
    SUM(GROSSROOMREV_Y_LY) GROSSROOMREV_Y_LY,
    SUM(GROSSFBREV_Y_LY) GROSSFBREV_Y_LY,
    SUM(GROSSMINIBARREV_Y_LY) GROSSMINIBARREV_Y_LY,
    SUM(GROSSTELEREV_Y_LY) GROSSTELEREV_Y_LY,
    SUM(GROSSMISCREV_Y_LY) GROSSMISCREV_Y_LY,
    SUM(GROSSREV_Y_LY) GROSSREV_Y_LY
FROM (
    SELECT
        WDAT_DATE,
        SHIS_WPHS_ID,
        XCMS_ID, 
        NOOFROOMS NOOFROOMS_D,
        ADULTNO ADULTNO_D,
        KIDS KIDS_D,
        ARRIVALS ARRIVALS_D,
        DEPARTURES DEPARTURES_D,
        ARR_ADULTNO ARR_ADULTNO_D, 
        DEP_ADULTNO DEP_ADULTNO_D,
        ARR_KIDS ARR_KIDS_D,
        DEP_KIDS DEP_KIDS_D,
        NOSHOW_ROOM NOSHOW_ROOM_D,
        CANCEL_ROOM CANCEL_ROOM_D,
        NETROOMREV NETROOMREV_D,
        NETFBREV NETFBREV_D,
        NETMINIBARREV NETMINIBARREV_D,
        NETTELEREV NETTELEREV_D,
        NETMISCREV NETMISCREV_D, 
        NETREV NETREV_D,
        GROSSROOMREV GROSSROOMREV_D,
        GROSSFBREV GROSSFBREV_D,
        GROSSMINIBARREV GROSSMINIBARREV_D, 
        GROSSTELEREV GROSSTELEREV_D,
        GROSSMISCREV GROSSMISCREV_D,
        GROSSREV GROSSREV_D,
        0 NOOFROOMS_M,
        0 ADULTNO_M,
        0 KIDS_M,
        0 ARRIVALS_M,
        0 DEPARTURES_M,
        0 ARR_ADULTNO_M, 
        0 DEP_ADULTNO_M,
        0 ARR_KIDS_M,
        0 DEP_KIDS_M,
        0 NOSHOW_ROOM_M,
        0 CANCEL_ROOM_M,
        0 NETROOMREV_M,
        0 NETFBREV_M,
        0 NETMINIBARREV_M,
        0 NETTELEREV_M,
        0 NETMISCREV_M, 
        0 NETREV_M,
        0 GROSSROOMREV_M,
        0 GROSSFBREV_M,
        0 GROSSMINIBARREV_M, 
        0 GROSSTELEREV_M,
        0 GROSSMISCREV_M,
        0 GROSSREV_M,
        0 NOOFROOMS_Y,
        0 ADULTNO_Y,
        0 KIDS_Y,
        0 ARRIVALS_Y,
        0 DEPARTURES_Y,
        0 ARR_ADULTNO_Y, 
        0 DEP_ADULTNO_Y,
        0 ARR_KIDS_Y,
        0 DEP_KIDS_Y,
        0 NOSHOW_ROOM_Y,
        0 CANCEL_ROOM_Y,
        0 NETROOMREV_Y,
        0 NETFBREV_Y,
        0 NETMINIBARREV_Y,
        0 NETTELEREV_Y,
        0 NETMISCREV_Y, 
        0 NETREV_Y,
        0 GROSSROOMREV_Y,
        0 GROSSFBREV_Y,
        0 GROSSMINIBARREV_Y, 
        0 GROSSTELEREV_Y,
        0 GROSSMISCREV_Y,
        0 GROSSREV_Y,
        0 NOOFROOMS_D_LY, 
        0 ADULTNO_D_LY, 
        0 KIDS_D_LY, 
        0 ARRIVALS_D_LY, 
        0 DEPARTURES_D_LY, 
        0 ARR_ADULTNO_D_LY, 
        0 DEP_ADULTNO_D_LY, 
        0 ARR_KIDS_D_LY, 
        0 DEP_KIDS_D_LY, 
        0 NOSHOW_ROOM_D_LY,
        0 CANCEL_ROOM_D_LY, 
        0 NETROOMREV_D_LY, 
        0 NETFBREV_D_LY, 
        0 NETMINIBARREV_D_LY, 
        0 NETTELEREV_D_LY, 
        0 NETMISCREV_D_LY, 
        0 NETREV_D_LY, 
        0 GROSSROOMREV_D_LY,
        0 GROSSFBREV_D_LY, 
        0 GROSSMINIBARREV_D_LY,
        0 GROSSTELEREV_D_LY,
        0 GROSSMISCREV_D_LY, 
        0 GROSSREV_D_LY,
        0 NOOFROOMS_M_LY,
        0 ADULTNO_M_LY, 
        0 KIDS_M_LY, 
        0 ARRIVALS_M_LY,
        0 DEPARTURES_M_LY,
        0 ARR_ADULTNO_M_LY, 
        0 DEP_ADULTNO_M_LY,
        0 ARR_KIDS_M_LY,
        0 DEP_KIDS_M_LY, 
        0 NOSHOW_ROOM_M_LY, 
        0 CANCEL_ROOM_M_LY, 
        0 NETROOMREV_M_LY, 
        0 NETFBREV_M_LY, 
        0 NETMINIBARREV_M_LY, 
        0 NETTELEREV_M_LY, 
        0 NETMISCREV_M_LY, 
        0 NETREV_M_LY, 
        0 GROSSROOMREV_M_LY,
        0 GROSSFBREV_M_LY, 
        0 GROSSMINIBARREV_M_LY,
        0 GROSSTELEREV_M_LY,
        0 GROSSMISCREV_M_LY,
        0 GROSSREV_M_LY, 
        0 NOOFROOMS_Y_LY, 
        0 ADULTNO_Y_LY,
        0 KIDS_Y_LY, 
        0 ARRIVALS_Y_LY,
        0 DEPARTURES_Y_LY, 
        0 ARR_ADULTNO_Y_LY, 
        0 DEP_ADULTNO_Y_LY, 
        0 ARR_KIDS_Y_LY, 
        0 DEP_KIDS_Y_LY, 
        0 NOSHOW_ROOM_Y_LY, 
        0 CANCEL_ROOM_Y_LY, 
        0 NETROOMREV_Y_LY, 
        0 NETFBREV_Y_LY, 
        0 NETMINIBARREV_Y_LY, 
        0 NETTELEREV_Y_LY, 
        0 NETMISCREV_Y_LY, 
        0 NETREV_Y_LY,
        0 GROSSROOMREV_Y_LY,
        0 GROSSFBREV_Y_LY,
        0 GROSSMINIBARREV_Y_LY, 
        0 GROSSTELEREV_Y_LY, 
        0 GROSSMISCREV_Y_LY, 
        0 GROSSREV_Y_LY
    FROM V8_STAT_SHIS_XCMS_2_DAY
    UNION ALL
    SELECT
        WDAT_DATE,
        SHIS_WPHS_ID,
        XCMS_ID,
        0 NOOFROOMS_D,
        0 ADULTNO_D,
        0 KIDS_D,
        0 ARRIVALS_D,
        0 DEPARTURES_D,
        0 ARR_ADULTNO_D, 
        0 DEP_ADULTNO_D,
        0 ARR_KIDS_D,
        0 DEP_KIDS_D,
        0 NOSHOW_ROOM_D,
        0 CANCEL_ROOM_D,
        0 NETROOMREV_D,
        0 NETFBREV_D,
        0 NETMINIBARREV_D,
        0 NETTELEREV_D,
        0 NETMISCREV_D, 
        0 NETREV_D,
        0 GROSSROOMREV_D,
        0 GROSSFBREV_D,
        0 GROSSMINIBARREV_D, 
        0 GROSSTELEREV_D,
        0 GROSSMISCREV_D,
        0 GROSSREV_D,
        NOOFROOMS NOOFROOMS_M,
        ADULTNO ADULTNO_M,
        KIDS KIDS_M,
        ARRIVALS ARRIVALS_M,
        DEPARTURES DEPARTURES_M,
        ARR_ADULTNO ARR_ADULTNO_M, 
        DEP_ADULTNO DEP_ADULTNO_M,
        ARR_KIDS ARR_KIDS_M,
        DEP_KIDS DEP_KIDS_M,
        NOSHOW_ROOM NOSHOW_ROOM_M,
        CANCEL_ROOM CANCEL_ROOM_M,
        NETROOMREV NETROOMREV_M,
        NETFBREV NETFBREV_M,
        NETMINIBARREV NETMINIBARREV_M,
        NETTELEREV NETTELEREV_M,
        NETMISCREV NETMISCREV_M, 
        NETREV NETREV_M,
        GROSSROOMREV GROSSROOMREV_M,
        GROSSFBREV GROSSFBREV_M,
        GROSSMINIBARREV GROSSMINIBARREV_M, 
        GROSSTELEREV GROSSTELEREV_M,
        GROSSMISCREV GROSSMISCREV_M,
        GROSSREV GROSSREV_M,
        0 NOOFROOMS_Y,
        0 ADULTNO_Y,
        0 KIDS_Y,
        0 ARRIVALS_Y,
        0 DEPARTURES_Y,
        0 ARR_ADULTNO_Y, 
        0 DEP_ADULTNO_Y,
        0 ARR_KIDS_Y,
        0 DEP_KIDS_Y,
        0 NOSHOW_ROOM_Y,
        0 CANCEL_ROOM_Y,
        0 NETROOMREV_Y,
        0 NETFBREV_Y,
        0 NETMINIBARREV_Y,
        0 NETTELEREV_Y,
        0 NETMISCREV_Y, 
        0 NETREV_Y,
        0 GROSSROOMREV_Y,
        0 GROSSFBREV_Y,
        0 GROSSMINIBARREV_Y, 
        0 GROSSTELEREV_Y,
        0 GROSSMISCREV_Y,
        0 GROSSREV_Y,
        0 NOOFROOMS_D_LY, 
        0 ADULTNO_D_LY, 
        0 KIDS_D_LY, 
        0 ARRIVALS_D_LY, 
        0 DEPARTURES_D_LY, 
        0 ARR_ADULTNO_D_LY, 
        0 DEP_ADULTNO_D_LY, 
        0 ARR_KIDS_D_LY, 
        0 DEP_KIDS_D_LY, 
        0 NOSHOW_ROOM_D_LY,
        0 CANCEL_ROOM_D_LY, 
        0 NETROOMREV_D_LY, 
        0 NETFBREV_D_LY, 
        0 NETMINIBARREV_D_LY, 
        0 NETTELEREV_D_LY, 
        0 NETMISCREV_D_LY, 
        0 NETREV_D_LY, 
        0 GROSSROOMREV_D_LY,
        0 GROSSFBREV_D_LY, 
        0 GROSSMINIBARREV_D_LY,
        0 GROSSTELEREV_D_LY,
        0 GROSSMISCREV_D_LY, 
        0 GROSSREV_D_LY,
        0 NOOFROOMS_M_LY,
        0 ADULTNO_M_LY, 
        0 KIDS_M_LY, 
        0 ARRIVALS_M_LY,
        0 DEPARTURES_M_LY,
        0 ARR_ADULTNO_M_LY, 
        0 DEP_ADULTNO_M_LY,
        0 ARR_KIDS_M_LY,
        0 DEP_KIDS_M_LY, 
        0 NOSHOW_ROOM_M_LY, 
        0 CANCEL_ROOM_M_LY, 
        0 NETROOMREV_M_LY, 
        0 NETFBREV_M_LY, 
        0 NETMINIBARREV_M_LY, 
        0 NETTELEREV_M_LY, 
        0 NETMISCREV_M_LY, 
        0 NETREV_M_LY, 
        0 GROSSROOMREV_M_LY,
        0 GROSSFBREV_M_LY, 
        0 GROSSMINIBARREV_M_LY,
        0 GROSSTELEREV_M_LY,
        0 GROSSMISCREV_M_LY,
        0 GROSSREV_M_LY, 
        0 NOOFROOMS_Y_LY, 
        0 ADULTNO_Y_LY,
        0 KIDS_Y_LY, 
        0 ARRIVALS_Y_LY,
        0 DEPARTURES_Y_LY, 
        0 ARR_ADULTNO_Y_LY, 
        0 DEP_ADULTNO_Y_LY, 
        0 ARR_KIDS_Y_LY, 
        0 DEP_KIDS_Y_LY, 
        0 NOSHOW_ROOM_Y_LY, 
        0 CANCEL_ROOM_Y_LY, 
        0 NETROOMREV_Y_LY, 
        0 NETFBREV_Y_LY, 
        0 NETMINIBARREV_Y_LY, 
        0 NETTELEREV_Y_LY, 
        0 NETMISCREV_Y_LY, 
        0 NETREV_Y_LY,
        0 GROSSROOMREV_Y_LY,
        0 GROSSFBREV_Y_LY,
        0 GROSSMINIBARREV_Y_LY, 
        0 GROSSTELEREV_Y_LY, 
        0 GROSSMISCREV_Y_LY, 
        0 GROSSREV_Y_LY
    FROM V8_STAT_SHIS_XCMS_2_MONTH
    UNION ALL
    SELECT
        WDAT_DATE,
        SHIS_WPHS_ID,
        XCMS_ID,
        0 NOOFROOMS_D,
        0 ADULTNO_D,
        0 KIDS_D,
        0 ARRIVALS_D,
        0 DEPARTURES_D,
        0 ARR_ADULTNO_D, 
        0 DEP_ADULTNO_D,
        0 ARR_KIDS_D,
        0 DEP_KIDS_D,
        0 NOSHOW_ROOM_D,
        0 CANCEL_ROOM_D,
        0 NETROOMREV_D,
        0 NETFBREV_D,
        0 NETMINIBARREV_D,
        0 NETTELEREV_D,
        0 NETMISCREV_D, 
        0 NETREV_D,
        0 GROSSROOMREV_D,
        0 GROSSFBREV_D,
        0 GROSSMINIBARREV_D, 
        0 GROSSTELEREV_D,
        0 GROSSMISCREV_D,
        0 GROSSREV_D,
        0 NOOFROOMS_M,
        0 ADULTNO_M,
        0 KIDS_M,
        0 ARRIVALS_M,
        0 DEPARTURES_M,
        0 ARR_ADULTNO_M, 
        0 DEP_ADULTNO_M,
        0 ARR_KIDS_M,
        0 DEP_KIDS_M,
        0 NOSHOW_ROOM_M,
        0 CANCEL_ROOM_M,
        0 NETROOMREV_M,
        0 NETFBREV_M,
        0 NETMINIBARREV_M,
        0 NETTELEREV_M,
        0 NETMISCREV_M, 
        0 NETREV_M,
        0 GROSSROOMREV_M,
        0 GROSSFBREV_M,
        0 GROSSMINIBARREV_M, 
        0 GROSSTELEREV_M,
        0 GROSSMISCREV_M,
        0 GROSSREV_M,
        NOOFROOMS NOOFROOMS_Y,
        ADULTNO ADULTNO_Y,
        KIDS KIDS_Y,
        ARRIVALS ARRIVALS_Y,
        DEPARTURES DEPARTURES_Y,
        ARR_ADULTNO ARR_ADULTNO_Y, 
        DEP_ADULTNO DEP_ADULTNO_Y,
        ARR_KIDS ARR_KIDS_Y,
        DEP_KIDS DEP_KIDS_Y,
        NOSHOW_ROOM NOSHOW_ROOM_Y,
        CANCEL_ROOM CANCEL_ROOM_Y,
        NETROOMREV NETROOMREV_Y,
        NETFBREV NETFBREV_Y,
        NETMINIBARREV NETMINIBARREV_Y,
        NETTELEREV NETTELEREV_Y,
        NETMISCREV NETMISCREV_Y, 
        NETREV NETREV_Y,
        GROSSROOMREV GROSSROOMREV_Y,
        GROSSFBREV GROSSFBREV_Y,
        GROSSMINIBARREV GROSSMINIBARREV_Y, 
        GROSSTELEREV GROSSTELEREV_Y,
        GROSSMISCREV GROSSMISCREV_Y,
        GROSSREV GROSSREV_Y,
        0 NOOFROOMS_D_LY, 
        0 ADULTNO_D_LY, 
        0 KIDS_D_LY, 
        0 ARRIVALS_D_LY, 
        0 DEPARTURES_D_LY, 
        0 ARR_ADULTNO_D_LY, 
        0 DEP_ADULTNO_D_LY, 
        0 ARR_KIDS_D_LY, 
        0 DEP_KIDS_D_LY, 
        0 NOSHOW_ROOM_D_LY,
        0 CANCEL_ROOM_D_LY, 
        0 NETROOMREV_D_LY, 
        0 NETFBREV_D_LY, 
        0 NETMINIBARREV_D_LY, 
        0 NETTELEREV_D_LY, 
        0 NETMISCREV_D_LY, 
        0 NETREV_D_LY, 
        0 GROSSROOMREV_D_LY,
        0 GROSSFBREV_D_LY, 
        0 GROSSMINIBARREV_D_LY,
        0 GROSSTELEREV_D_LY,
        0 GROSSMISCREV_D_LY, 
        0 GROSSREV_D_LY,
        0 NOOFROOMS_M_LY,
        0 ADULTNO_M_LY, 
        0 KIDS_M_LY, 
        0 ARRIVALS_M_LY,
        0 DEPARTURES_M_LY,
        0 ARR_ADULTNO_M_LY, 
        0 DEP_ADULTNO_M_LY,
        0 ARR_KIDS_M_LY,
        0 DEP_KIDS_M_LY, 
        0 NOSHOW_ROOM_M_LY, 
        0 CANCEL_ROOM_M_LY, 
        0 NETROOMREV_M_LY, 
        0 NETFBREV_M_LY, 
        0 NETMINIBARREV_M_LY, 
        0 NETTELEREV_M_LY, 
        0 NETMISCREV_M_LY, 
        0 NETREV_M_LY, 
        0 GROSSROOMREV_M_LY,
        0 GROSSFBREV_M_LY, 
        0 GROSSMINIBARREV_M_LY,
        0 GROSSTELEREV_M_LY,
        0 GROSSMISCREV_M_LY,
        0 GROSSREV_M_LY, 
        0 NOOFROOMS_Y_LY, 
        0 ADULTNO_Y_LY,
        0 KIDS_Y_LY, 
        0 ARRIVALS_Y_LY,
        0 DEPARTURES_Y_LY, 
        0 ARR_ADULTNO_Y_LY, 
        0 DEP_ADULTNO_Y_LY, 
        0 ARR_KIDS_Y_LY, 
        0 DEP_KIDS_Y_LY, 
        0 NOSHOW_ROOM_Y_LY, 
        0 CANCEL_ROOM_Y_LY, 
        0 NETROOMREV_Y_LY, 
        0 NETFBREV_Y_LY, 
        0 NETMINIBARREV_Y_LY, 
        0 NETTELEREV_Y_LY, 
        0 NETMISCREV_Y_LY, 
        0 NETREV_Y_LY,
        0 GROSSROOMREV_Y_LY,
        0 GROSSFBREV_Y_LY,
        0 GROSSMINIBARREV_Y_LY, 
        0 GROSSTELEREV_Y_LY, 
        0 GROSSMISCREV_Y_LY, 
        0 GROSSREV_Y_LY
    FROM V8_STAT_SHIS_XCMS_2_YEAR
    UNION ALL
        SELECT
        WDAT_DATE,
        SHIS_WPHS_ID,
        XCMS_ID, 
        0 NOOFROOMS_D,
        0 ADULTNO_D,
        0 KIDS_D,
        0 ARRIVALS_D,
        0 DEPARTURES_D,
        0 ARR_ADULTNO_D, 
        0 DEP_ADULTNO_D,
        0 ARR_KIDS_D,
        0 DEP_KIDS_D,
        0 NOSHOW_ROOM_D,
        0 CANCEL_ROOM_D,
        0 NETROOMREV_D,
        0 NETFBREV_D,
        0 NETMINIBARREV_D,
        0 NETTELEREV_D,
        0 NETMISCREV_D, 
        0 NETREV_D,
        0 GROSSROOMREV_D,
        0 GROSSFBREV_D,
        0 GROSSMINIBARREV_D, 
        0 GROSSTELEREV_D,
        0 GROSSMISCREV_D,
        0 GROSSREV_D,
        0 NOOFROOMS_M,
        0 ADULTNO_M,
        0 KIDS_M,
        0 ARRIVALS_M,
        0 DEPARTURES_M,
        0 ARR_ADULTNO_M, 
        0 DEP_ADULTNO_M,
        0 ARR_KIDS_M,
        0 DEP_KIDS_M,
        0 NOSHOW_ROOM_M,
        0 CANCEL_ROOM_M,
        0 NETROOMREV_M,
        0 NETFBREV_M,
        0 NETMINIBARREV_M,
        0 NETTELEREV_M,
        0 NETMISCREV_M, 
        0 NETREV_M,
        0 GROSSROOMREV_M,
        0 GROSSFBREV_M,
        0 GROSSMINIBARREV_M, 
        0 GROSSTELEREV_M,
        0 GROSSMISCREV_M,
        0 GROSSREV_M,
        0 NOOFROOMS_Y,
        0 ADULTNO_Y,
        0 KIDS_Y,
        0 ARRIVALS_Y,
        0 DEPARTURES_Y,
        0 ARR_ADULTNO_Y, 
        0 DEP_ADULTNO_Y,
        0 ARR_KIDS_Y,
        0 DEP_KIDS_Y,
        0 NOSHOW_ROOM_Y,
        0 CANCEL_ROOM_Y,
        0 NETROOMREV_Y,
        0 NETFBREV_Y,
        0 NETMINIBARREV_Y,
        0 NETTELEREV_Y,
        0 NETMISCREV_Y, 
        0 NETREV_Y,
        0 GROSSROOMREV_Y,
        0 GROSSFBREV_Y,
        0 GROSSMINIBARREV_Y, 
        0 GROSSTELEREV_Y,
        0 GROSSMISCREV_Y,
        0 GROSSREV_Y,
        NOOFROOMS NOOFROOMS_D_LY,
        ADULTNO ADULTNO_D_LY,
        KIDS KIDS_D_LY,
        ARRIVALS ARRIVALS_D_LY,
        DEPARTURES DEPARTURES_D_LY,
        ARR_ADULTNO ARR_ADULTNO_D_LY, 
        DEP_ADULTNO DEP_ADULTNO_D_LY,
        ARR_KIDS ARR_KIDS_D_LY,
        DEP_KIDS DEP_KIDS_D_LY,
        NOSHOW_ROOM NOSHOW_ROOM_D_LY,
        CANCEL_ROOM CANCEL_ROOM_D_LY,
        NETROOMREV NETROOMREV_D_LY,
        NETFBREV NETFBREV_D_LY,
        NETMINIBARREV NETMINIBARREV_D_LY,
        NETTELEREV NETTELEREV_D_LY,
        NETMISCREV NETMISCREV_D_LY, 
        NETREV NETREV_D_LY,
        GROSSROOMREV GROSSROOMREV_D_LY,
        GROSSFBREV GROSSFBREV_D_LY,
        GROSSMINIBARREV GROSSMINIBARREV_D_LY, 
        GROSSTELEREV GROSSTELEREV_D_LY,
        GROSSMISCREV GROSSMISCREV_D_LY,
        GROSSREV GROSSREV_D_LY,
        0 NOOFROOMS_M_LY,
        0 ADULTNO_M_LY, 
        0 KIDS_M_LY, 
        0 ARRIVALS_M_LY,
        0 DEPARTURES_M_LY,
        0 ARR_ADULTNO_M_LY, 
        0 DEP_ADULTNO_M_LY,
        0 ARR_KIDS_M_LY,
        0 DEP_KIDS_M_LY, 
        0 NOSHOW_ROOM_M_LY, 
        0 CANCEL_ROOM_M_LY, 
        0 NETROOMREV_M_LY, 
        0 NETFBREV_M_LY, 
        0 NETMINIBARREV_M_LY, 
        0 NETTELEREV_M_LY, 
        0 NETMISCREV_M_LY, 
        0 NETREV_M_LY, 
        0 GROSSROOMREV_M_LY,
        0 GROSSFBREV_M_LY, 
        0 GROSSMINIBARREV_M_LY,
        0 GROSSTELEREV_M_LY,
        0 GROSSMISCREV_M_LY,
        0 GROSSREV_M_LY, 
        0 NOOFROOMS_Y_LY, 
        0 ADULTNO_Y_LY,
        0 KIDS_Y_LY, 
        0 ARRIVALS_Y_LY,
        0 DEPARTURES_Y_LY, 
        0 ARR_ADULTNO_Y_LY, 
        0 DEP_ADULTNO_Y_LY, 
        0 ARR_KIDS_Y_LY, 
        0 DEP_KIDS_Y_LY, 
        0 NOSHOW_ROOM_Y_LY, 
        0 CANCEL_ROOM_Y_LY, 
        0 NETROOMREV_Y_LY, 
        0 NETFBREV_Y_LY, 
        0 NETMINIBARREV_Y_LY, 
        0 NETTELEREV_Y_LY, 
        0 NETMISCREV_Y_LY, 
        0 NETREV_Y_LY,
        0 GROSSROOMREV_Y_LY,
        0 GROSSFBREV_Y_LY,
        0 GROSSMINIBARREV_Y_LY, 
        0 GROSSTELEREV_Y_LY, 
        0 GROSSMISCREV_Y_LY, 
        0 GROSSREV_Y_LY
    FROM V8_STAT_SHIS_XCMS_2_DAY_LY
    UNION ALL
    SELECT
        WDAT_DATE,
        SHIS_WPHS_ID,
        XCMS_ID, 
        0 NOOFROOMS_D,
        0 ADULTNO_D,
        0 KIDS_D,
        0 ARRIVALS_D,
        0 DEPARTURES_D,
        0 ARR_ADULTNO_D, 
        0 DEP_ADULTNO_D,
        0 ARR_KIDS_D,
        0 DEP_KIDS_D,
        0 NOSHOW_ROOM_D,
        0 CANCEL_ROOM_D,
        0 NETROOMREV_D,
        0 NETFBREV_D,
        0 NETMINIBARREV_D,
        0 NETTELEREV_D,
        0 NETMISCREV_D, 
        0 NETREV_D,
        0 GROSSROOMREV_D,
        0 GROSSFBREV_D,
        0 GROSSMINIBARREV_D, 
        0 GROSSTELEREV_D,
        0 GROSSMISCREV_D,
        0 GROSSREV_D,
        0 NOOFROOMS_M,
        0 ADULTNO_M,
        0 KIDS_M,
        0 ARRIVALS_M,
        0 DEPARTURES_M,
        0 ARR_ADULTNO_M, 
        0 DEP_ADULTNO_M,
        0 ARR_KIDS_M,
        0 DEP_KIDS_M,
        0 NOSHOW_ROOM_M,
        0 CANCEL_ROOM_M,
        0 NETROOMREV_M,
        0 NETFBREV_M,
        0 NETMINIBARREV_M,
        0 NETTELEREV_M,
        0 NETMISCREV_M, 
        0 NETREV_M,
        0 GROSSROOMREV_M,
        0 GROSSFBREV_M,
        0 GROSSMINIBARREV_M, 
        0 GROSSTELEREV_M,
        0 GROSSMISCREV_M,
        0 GROSSREV_M,
        0 NOOFROOMS_Y,
        0 ADULTNO_Y,
        0 KIDS_Y,
        0 ARRIVALS_Y,
        0 DEPARTURES_Y,
        0 ARR_ADULTNO_Y, 
        0 DEP_ADULTNO_Y,
        0 ARR_KIDS_Y,
        0 DEP_KIDS_Y,
        0 NOSHOW_ROOM_Y,
        0 CANCEL_ROOM_Y,
        0 NETROOMREV_Y,
        0 NETFBREV_Y,
        0 NETMINIBARREV_Y,
        0 NETTELEREV_Y,
        0 NETMISCREV_Y, 
        0 NETREV_Y,
        0 GROSSROOMREV_Y,
        0 GROSSFBREV_Y,
        0 GROSSMINIBARREV_Y, 
        0 GROSSTELEREV_Y,
        0 GROSSMISCREV_Y,
        0 GROSSREV_Y,
        0 NOOFROOMS_D_LY, 
        0 ADULTNO_D_LY, 
        0 KIDS_D_LY, 
        0 ARRIVALS_D_LY, 
        0 DEPARTURES_D_LY, 
        0 ARR_ADULTNO_D_LY, 
        0 DEP_ADULTNO_D_LY, 
        0 ARR_KIDS_D_LY, 
        0 DEP_KIDS_D_LY, 
        0 NOSHOW_ROOM_D_LY,
        0 CANCEL_ROOM_D_LY, 
        0 NETROOMREV_D_LY, 
        0 NETFBREV_D_LY, 
        0 NETMINIBARREV_D_LY, 
        0 NETTELEREV_D_LY, 
        0 NETMISCREV_D_LY, 
        0 NETREV_D_LY, 
        0 GROSSROOMREV_D_LY,
        0 GROSSFBREV_D_LY, 
        0 GROSSMINIBARREV_D_LY,
        0 GROSSTELEREV_D_LY,
        0 GROSSMISCREV_D_LY, 
        0 GROSSREV_D_LY,
        NOOFROOMS NOOFROOMS_M_LY,
        ADULTNO ADULTNO_M_LY,
        KIDS KIDS_M_LY,
        ARRIVALS ARRIVALS_M_LY,
        DEPARTURES DEPARTURES_M_LY,
        ARR_ADULTNO ARR_ADULTNO_M_LY, 
        DEP_ADULTNO DEP_ADULTNO_M_LY,
        ARR_KIDS ARR_KIDS_M_LY,
        DEP_KIDS DEP_KIDS_M_LY,
        NOSHOW_ROOM NOSHOW_ROOM_M_LY,
        CANCEL_ROOM CANCEL_ROOM_M_LY,
        NETROOMREV NETROOMREV_M_LY,
        NETFBREV NETFBREV_M_LY,
        NETMINIBARREV NETMINIBARREV_M_LY,
        NETTELEREV NETTELEREV_M_LY,
        NETMISCREV NETMISCREV_M_LY, 
        NETREV NETREV_M_LY,
        GROSSROOMREV GROSSROOMREV_M_LY,
        GROSSFBREV GROSSFBREV_M_LY,
        GROSSMINIBARREV GROSSMINIBARREV_M_LY, 
        GROSSTELEREV GROSSTELEREV_M_LY,
        GROSSMISCREV GROSSMISCREV_M_LY,
        GROSSREV GROSSREV_M_LY, 
        0 NOOFROOMS_Y_LY, 
        0 ADULTNO_Y_LY,
        0 KIDS_Y_LY, 
        0 ARRIVALS_Y_LY,
        0 DEPARTURES_Y_LY, 
        0 ARR_ADULTNO_Y_LY, 
        0 DEP_ADULTNO_Y_LY, 
        0 ARR_KIDS_Y_LY, 
        0 DEP_KIDS_Y_LY, 
        0 NOSHOW_ROOM_Y_LY, 
        0 CANCEL_ROOM_Y_LY, 
        0 NETROOMREV_Y_LY, 
        0 NETFBREV_Y_LY, 
        0 NETMINIBARREV_Y_LY, 
        0 NETTELEREV_Y_LY, 
        0 NETMISCREV_Y_LY, 
        0 NETREV_Y_LY,
        0 GROSSROOMREV_Y_LY,
        0 GROSSFBREV_Y_LY,
        0 GROSSMINIBARREV_Y_LY, 
        0 GROSSTELEREV_Y_LY, 
        0 GROSSMISCREV_Y_LY, 
        0 GROSSREV_Y_LY
    FROM V8_STAT_SHIS_XCMS_2_MONTH_LY
    UNION ALL 
    SELECT
        WDAT_DATE,
        SHIS_WPHS_ID,
        XCMS_ID, 
        0 NOOFROOMS_D,
        0 ADULTNO_D,
        0 KIDS_D,
        0 ARRIVALS_D,
        0 DEPARTURES_D,
        0 ARR_ADULTNO_D, 
        0 DEP_ADULTNO_D,
        0 ARR_KIDS_D,
        0 DEP_KIDS_D,
        0 NOSHOW_ROOM_D,
        0 CANCEL_ROOM_D,
        0 NETROOMREV_D,
        0 NETFBREV_D,
        0 NETMINIBARREV_D,
        0 NETTELEREV_D,
        0 NETMISCREV_D, 
        0 NETREV_D,
        0 GROSSROOMREV_D,
        0 GROSSFBREV_D,
        0 GROSSMINIBARREV_D, 
        0 GROSSTELEREV_D,
        0 GROSSMISCREV_D,
        0 GROSSREV_D,
        0 NOOFROOMS_M,
        0 ADULTNO_M,
        0 KIDS_M,
        0 ARRIVALS_M,
        0 DEPARTURES_M,
        0 ARR_ADULTNO_M, 
        0 DEP_ADULTNO_M,
        0 ARR_KIDS_M,
        0 DEP_KIDS_M,
        0 NOSHOW_ROOM_M,
        0 CANCEL_ROOM_M,
        0 NETROOMREV_M,
        0 NETFBREV_M,
        0 NETMINIBARREV_M,
        0 NETTELEREV_M,
        0 NETMISCREV_M, 
        0 NETREV_M,
        0 GROSSROOMREV_M,
        0 GROSSFBREV_M,
        0 GROSSMINIBARREV_M, 
        0 GROSSTELEREV_M,
        0 GROSSMISCREV_M,
        0 GROSSREV_M,
        0 NOOFROOMS_Y,
        0 ADULTNO_Y,
        0 KIDS_Y,
        0 ARRIVALS_Y,
        0 DEPARTURES_Y,
        0 ARR_ADULTNO_Y, 
        0 DEP_ADULTNO_Y,
        0 ARR_KIDS_Y,
        0 DEP_KIDS_Y,
        0 NOSHOW_ROOM_Y,
        0 CANCEL_ROOM_Y,
        0 NETROOMREV_Y,
        0 NETFBREV_Y,
        0 NETMINIBARREV_Y,
        0 NETTELEREV_Y,
        0 NETMISCREV_Y, 
        0 NETREV_Y,
        0 GROSSROOMREV_Y,
        0 GROSSFBREV_Y,
        0 GROSSMINIBARREV_Y, 
        0 GROSSTELEREV_Y,
        0 GROSSMISCREV_Y,
        0 GROSSREV_Y,
        0 NOOFROOMS_D_LY, 
        0 ADULTNO_D_LY, 
        0 KIDS_D_LY, 
        0 ARRIVALS_D_LY, 
        0 DEPARTURES_D_LY, 
        0 ARR_ADULTNO_D_LY, 
        0 DEP_ADULTNO_D_LY, 
        0 ARR_KIDS_D_LY, 
        0 DEP_KIDS_D_LY, 
        0 NOSHOW_ROOM_D_LY,
        0 CANCEL_ROOM_D_LY, 
        0 NETROOMREV_D_LY, 
        0 NETFBREV_D_LY, 
        0 NETMINIBARREV_D_LY, 
        0 NETTELEREV_D_LY, 
        0 NETMISCREV_D_LY, 
        0 NETREV_D_LY, 
        0 GROSSROOMREV_D_LY,
        0 GROSSFBREV_D_LY, 
        0 GROSSMINIBARREV_D_LY,
        0 GROSSTELEREV_D_LY,
        0 GROSSMISCREV_D_LY, 
        0 GROSSREV_D_LY,
        0 NOOFROOMS_M_LY,
        0 ADULTNO_M_LY, 
        0 KIDS_M_LY, 
        0 ARRIVALS_M_LY,
        0 DEPARTURES_M_LY,
        0 ARR_ADULTNO_M_LY, 
        0 DEP_ADULTNO_M_LY,
        0 ARR_KIDS_M_LY,
        0 DEP_KIDS_M_LY, 
        0 NOSHOW_ROOM_M_LY, 
        0 CANCEL_ROOM_M_LY, 
        0 NETROOMREV_M_LY, 
        0 NETFBREV_M_LY, 
        0 NETMINIBARREV_M_LY, 
        0 NETTELEREV_M_LY, 
        0 NETMISCREV_M_LY, 
        0 NETREV_M_LY, 
        0 GROSSROOMREV_M_LY,
        0 GROSSFBREV_M_LY, 
        0 GROSSMINIBARREV_M_LY,
        0 GROSSTELEREV_M_LY,
        0 GROSSMISCREV_M_LY,
        0 GROSSREV_M_LY,
        NOOFROOMS NOOFROOMS_Y_LY,
        ADULTNO ADULTNO_Y_LY,
        KIDS KIDS_Y_LY,
        ARRIVALS ARRIVALS_Y_LY,
        DEPARTURES DEPARTURES_Y_LY,
        ARR_ADULTNO ARR_ADULTNO_Y_LY, 
        DEP_ADULTNO DEP_ADULTNO_Y_LY,
        ARR_KIDS ARR_KIDS_Y_LY,
        DEP_KIDS DEP_KIDS_Y_LY,
        NOSHOW_ROOM NOSHOW_ROOM_Y_LY,
        CANCEL_ROOM CANCEL_ROOM_Y_LY,
        NETROOMREV NETROOMREV_Y_LY,
        NETFBREV NETFBREV_Y_LY,
        NETMINIBARREV NETMINIBARREV_Y_LY,
        NETTELEREV NETTELEREV_Y_LY,
        NETMISCREV NETMISCREV_Y_LY, 
        NETREV NETREV_Y_LY,
        GROSSROOMREV GROSSROOMREV_Y_LY,
        GROSSFBREV GROSSFBREV_Y_LY,
        GROSSMINIBARREV GROSSMINIBARREV_Y_LY, 
        GROSSTELEREV GROSSTELEREV_Y_LY,
        GROSSMISCREV GROSSMISCREV_Y_LY,
        GROSSREV GROSSREV_Y_LY
    FROM V8_STAT_SHIS_XCMS_2_YEAR_LY
)
GROUP BY WDAT_DATE,XCMS_ID,SHIS_WPHS_ID;

CREATE OR REPLACE VIEW V8_STAT_SHIS_XCMS_3_DAY_LY
AS 
SELECT 
  WDAT_DATE, 
  shis_wphs_id,  
  XCMS_ID, 
  sum(NOOFROOMS) NOOFROOMS, 
  sum(ADULTNO) ADULTNO, 
  sum(KIDS) KIDS, 
  sum(ARRIVALS) ARRIVALS, 
  sum(DEPARTURES) DEPARTURES, 
  sum(ARR_ADULTNO) ARR_ADULTNO, 
  sum(DEP_ADULTNO) DEP_ADULTNO, 
  sum(ARR_KIDS) ARR_KIDS, 
  sum(DEP_KIDS) DEP_KIDS, 
  sum(NOSHOW_ROOM) NOSHOW_ROOM, 
  sum(CANCEL_ROOM) CANCEL_ROOM, 
  sum(NETROOMREV) NETROOMREV, 
  sum(NETFBREV) NETFBREV, 
  sum(NETMINIBARREV) NETMINIBARREV, 
  sum(NETTELEREV) NETTELEREV, 
  sum(NETMISCREV) NETMISCREV, 
  sum(NETREV) NETREV, 
  sum(GROSSROOMREV) GROSSROOMREV, 
  sum(GROSSFBREV) GROSSFBREV, 
  sum(GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(GROSSTELEREV) GROSSTELEREV, 
  sum(GROSSMISCREV) GROSSMISCREV, 
  sum(GROSSREV) GROSSREV  
FROM 
( 
SELECT 
  add_months(wdat.WDAT_DATE,-12) WDAT_DATE, 
  s.shis_wphs_id shis_wphs_id,  
  S.SHIS_YCLN_3_XCMS_ID XCMS_ID, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,0,decode(S.SHIS_DEP_ID,1,0,S.SHIS_YDET_NOOFROOMS))) NOOFROOMS,
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN S.SHIS_YDET_ADULTNO WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END )) ADULTNO, 
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN NVL(S.SHIS_TOTALYRGL_NUMBER,0) WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER,0) END )) KIDS, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_NOOFROOMS,0)) ARRIVALS, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_NOOFROOMS,0)) DEPARTURES, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_ADULTNO,0)) ARR_ADULTNO, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_ADULTNO,0)) DEP_ADULTNO, 
  sum(decode(S.SHIS_ARR_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) ARR_KIDS, 
  sum(decode(S.SHIS_DEP_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) DEP_KIDS, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,4,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) NOSHOW_ROOM,
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,3,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) CANCEL_ROOM,
  sum(S.SHIS_NETROOMREV) NETROOMREV, 
  sum(S.SHIS_NETFBREV) NETFBREV, 
  sum(S.SHIS_NETMINIBARREV) NETMINIBARREV, 
  sum(S.SHIS_NETTELEREV) NETTELEREV, 
  sum(S.SHIS_NETMISCREV) NETMISCREV, 
  sum(S.SHIS_NETREV) NETREV, 
  sum(S.SHIS_GROSSROOMREV) GROSSROOMREV, 
  sum(S.SHIS_GROSSFBREV) GROSSFBREV, 
  sum(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(S.SHIS_GROSSTELEREV) GROSSTELEREV, 
  sum(S.SHIS_GROSSMISCREV) GROSSMISCREV, 
 sum(S.SHIS_GROSSREV) GROSSREV 
FROM 
  wdat, 
  shis S 
  WHERE 
add_months(wdat.WDAT_DATE,-12)=S.SHIS_WDAT_DATE 
group by wdat.WDAT_DATE,S.SHIS_YCLN_3_XCMS_ID,shis_wphs_id
union 
SELECT 
  wdat.WDAT_DATE WDAT_DATE, 
  s.shis_wphs_id shis_wphs_id,  
  S.SHIS_XCMS_ID XCMS_ID, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,0,decode(S.SHIS_DEP_ID,1,0,S.SHIS_YDET_NOOFROOMS))) NOOFROOMS,
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN S.SHIS_YDET_ADULTNO WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END )) ADULTNO, 
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN NVL(S.SHIS_TOTALYRGL_NUMBER,0) WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER,0) END )) KIDS, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_NOOFROOMS,0)) ARRIVALS, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_NOOFROOMS,0)) DEPARTURES, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_ADULTNO,0)) ARR_ADULTNO, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_ADULTNO,0)) DEP_ADULTNO, 
  sum(decode(S.SHIS_ARR_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) ARR_KIDS, 
  sum(decode(S.SHIS_DEP_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) DEP_KIDS, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,4,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) NOSHOW_ROOM,
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,3,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) CANCEL_ROOM,
  sum(S.SHIS_NETROOMREV) NETROOMREV, 
  sum(S.SHIS_NETFBREV) NETFBREV, 
  sum(S.SHIS_NETMINIBARREV) NETMINIBARREV, 
  sum(S.SHIS_NETTELEREV) NETTELEREV, 
  sum(S.SHIS_NETMISCREV) NETMISCREV, 
  sum(S.SHIS_NETREV) NETREV, 
  sum(S.SHIS_GROSSROOMREV) GROSSROOMREV, 
  sum(S.SHIS_GROSSFBREV) GROSSFBREV, 
  sum(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(S.SHIS_GROSSTELEREV) GROSSTELEREV, 
  sum(S.SHIS_GROSSMISCREV) GROSSMISCREV, 
 sum(S.SHIS_GROSSREV) GROSSREV 
FROM 
  wdat, 
  shis S 
  WHERE 
add_months(wdat.WDAT_DATE,-12)=S.SHIS_WDAT_DATE 
and (select xcca.XCCA_INTERNALCATEGORY from xcca,xcms where xcca.XCCA_ID=xcms.XCMS_XCCA_ID and xcms.XCMS_ID=S.SHIS_XCMS_ID)=3 
group by wdat.WDAT_DATE,S.SHIS_XCMS_ID ,shis_wphs_id
) 
group by WDAT_DATE,XCMS_ID,shis_wphs_id;

CREATE OR REPLACE VIEW V8_STAT_SHIS_XCMS_3_MONTH_LY
AS 
SELECT 
WDAT_DATE WDAT_DATE,
    SHIS_WPHS_ID SHIS_WPHS_ID,
    NVL(XCMS_ID,0) XCMS_ID,
    SUM(NOOFROOMS) NOOFROOMS,
    SUM(ADULTNO) ADULTNO,
    SUM(KIDS) KIDS,
    SUM(ARRIVALS) ARRIVALS,
    SUM(DEPARTURES) DEPARTURES,
    SUM(ARR_ADULTNO) ARR_ADULTNO,
    SUM(DEP_ADULTNO) DEP_ADULTNO,
    SUM(ARR_KIDS) ARR_KIDS,
    SUM(DEP_KIDS) DEP_KIDS,
    SUM(NOSHOW_ROOM) NOSHOW_ROOM,
    SUM(CANCEL_ROOM) CANCEL_ROOM,
    SUM(NETROOMREV) NETROOMREV,
    SUM(NETFBREV) NETFBREV,
    SUM(NETMINIBARREV) NETMINIBARREV,
    SUM(NETTELEREV) NETTELEREV,
    SUM(NETMISCREV) NETMISCREV,
    SUM(NETREV) NETREV,
    SUM(GROSSROOMREV) GROSSROOMREV,
    SUM(GROSSFBREV) GROSSFBREV,
    SUM(GROSSMINIBARREV) GROSSMINIBARREV,
    SUM(GROSSTELEREV) GROSSTELEREV,
    SUM(GROSSMISCREV) GROSSMISCREV,
    SUM(GROSSREV) GROSSREV
FROM 
( 
SELECT 
  wdat.WDAT_DATE WDAT_DATE, 
  s.shis_wphs_id shis_wphs_id,
  S.SHIS_YCLN_3_XCMS_ID XCMS_ID, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,0,decode(S.SHIS_DEP_ID,1,0,S.SHIS_YDET_NOOFROOMS))) NOOFROOMS,
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN S.SHIS_YDET_ADULTNO WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END )) ADULTNO, 
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN NVL(S.SHIS_TOTALYRGL_NUMBER,0) WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER,0) END )) KIDS, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_NOOFROOMS,0)) ARRIVALS, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_NOOFROOMS,0)) DEPARTURES, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_ADULTNO,0)) ARR_ADULTNO, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_ADULTNO,0)) DEP_ADULTNO, 
  sum(decode(S.SHIS_ARR_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) ARR_KIDS, 
  sum(decode(S.SHIS_DEP_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) DEP_KIDS, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,4,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) NOSHOW_ROOM,
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,3,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) CANCEL_ROOM,
  sum(S.SHIS_NETROOMREV) NETROOMREV, 
  sum(S.SHIS_NETFBREV) NETFBREV, 
  sum(S.SHIS_NETMINIBARREV) NETMINIBARREV, 
  sum(S.SHIS_NETTELEREV) NETTELEREV, 
  sum(S.SHIS_NETMISCREV) NETMISCREV, 
  sum(S.SHIS_NETREV) NETREV, 
  sum(S.SHIS_GROSSROOMREV) GROSSROOMREV, 
  sum(S.SHIS_GROSSFBREV) GROSSFBREV, 
  sum(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(S.SHIS_GROSSTELEREV) GROSSTELEREV, 
  sum(S.SHIS_GROSSMISCREV) GROSSMISCREV, 
 sum(S.SHIS_GROSSREV) GROSSREV 
FROM 
  wdat, 
  shis S 
  WHERE 
add_months(add_months(last_day(wdat.WDAT_DATE),-1)+1,-12)<=S.SHIS_WDAT_DATE and 
S.SHIS_WDAT_DATE<=add_months(wdat.WDAT_DATE,-12) 
group by wdat.WDAT_DATE,S.SHIS_YCLN_3_XCMS_ID,shis_wphs_id
union 
SELECT 
  wdat.WDAT_DATE WDAT_DATE, 
  s.shis_wphs_id shis_wphs_id,
  S.SHIS_XCMS_ID XCMS_ID, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,0,decode(S.SHIS_DEP_ID,1,0,S.SHIS_YDET_NOOFROOMS))) NOOFROOMS,
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN S.SHIS_YDET_ADULTNO WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END )) ADULTNO, 
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN NVL(S.SHIS_TOTALYRGL_NUMBER,0) WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER,0) END )) KIDS, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_NOOFROOMS,0)) ARRIVALS, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_NOOFROOMS,0)) DEPARTURES, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_ADULTNO,0)) ARR_ADULTNO, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_ADULTNO,0)) DEP_ADULTNO, 
  sum(decode(S.SHIS_ARR_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) ARR_KIDS, 
  sum(decode(S.SHIS_DEP_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) DEP_KIDS, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,4,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) NOSHOW_ROOM,
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,3,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) CANCEL_ROOM,
  sum(S.SHIS_NETROOMREV) NETROOMREV, 
  sum(S.SHIS_NETFBREV) NETFBREV, 
  sum(S.SHIS_NETMINIBARREV) NETMINIBARREV, 
  sum(S.SHIS_NETTELEREV) NETTELEREV, 
  sum(S.SHIS_NETMISCREV) NETMISCREV, 
  sum(S.SHIS_NETREV) NETREV, 
  sum(S.SHIS_GROSSROOMREV) GROSSROOMREV, 
  sum(S.SHIS_GROSSFBREV) GROSSFBREV, 
  sum(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(S.SHIS_GROSSTELEREV) GROSSTELEREV, 
  sum(S.SHIS_GROSSMISCREV) GROSSMISCREV, 
 sum(S.SHIS_GROSSREV) GROSSREV 
FROM 
  wdat, 
  shis S 
  WHERE 
add_months(add_months(last_day(wdat.WDAT_DATE),-1)+1,-12)<=S.SHIS_WDAT_DATE and 
S.SHIS_WDAT_DATE<=add_months(wdat.WDAT_DATE,-12) 
and (select xcca.XCCA_INTERNALCATEGORY from xcca,xcms where xcca.XCCA_ID=xcms.XCMS_XCCA_ID and xcms.XCMS_ID=S.SHIS_XCMS_ID)=3 
group by wdat.WDAT_DATE,S.SHIS_XCMS_ID ,shis_wphs_id
) 
group by WDAT_DATE,XCMS_ID,shis_wphs_id;

CREATE OR REPLACE VIEW V8_STAT_SHIS_XCMS_3_YEAR_LY
AS 
SELECT 
    WDAT_DATE WDAT_DATE,
    SHIS_WPHS_ID SHIS_WPHS_ID,
    NVL(XCMS_ID,0) XCMS_ID,
    SUM(NOOFROOMS) NOOFROOMS,
    SUM(ADULTNO) ADULTNO,
    SUM(KIDS) KIDS,
    SUM(ARRIVALS) ARRIVALS,
    SUM(DEPARTURES) DEPARTURES,
    SUM(ARR_ADULTNO) ARR_ADULTNO,
    SUM(DEP_ADULTNO) DEP_ADULTNO,
    SUM(ARR_KIDS) ARR_KIDS,
    SUM(DEP_KIDS) DEP_KIDS,
    SUM(NOSHOW_ROOM) NOSHOW_ROOM,
    SUM(CANCEL_ROOM) CANCEL_ROOM,
    SUM(NETROOMREV) NETROOMREV,
    SUM(NETFBREV) NETFBREV,
    SUM(NETMINIBARREV) NETMINIBARREV,
    SUM(NETTELEREV) NETTELEREV,
    SUM(NETMISCREV) NETMISCREV,
    SUM(NETREV) NETREV,
    SUM(GROSSROOMREV) GROSSROOMREV,
    SUM(GROSSFBREV) GROSSFBREV,
    SUM(GROSSMINIBARREV) GROSSMINIBARREV,
    SUM(GROSSTELEREV) GROSSTELEREV,
    SUM(GROSSMISCREV) GROSSMISCREV,
    SUM(GROSSREV) GROSSREV
    FROM 
( 
SELECT 
  wdat.WDAT_DATE WDAT_DATE, 
  s.shis_wphs_id shis_wphs_id,
  S.SHIS_YCLN_3_XCMS_ID XCMS_ID, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,0,decode(S.SHIS_DEP_ID,1,0,S.SHIS_YDET_NOOFROOMS))) NOOFROOMS,
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN S.SHIS_YDET_ADULTNO WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END )) ADULTNO, 
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN NVL(S.SHIS_TOTALYRGL_NUMBER,0) WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER,0) END )) KIDS, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_NOOFROOMS,0)) ARRIVALS, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_NOOFROOMS,0)) DEPARTURES, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_ADULTNO,0)) ARR_ADULTNO, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_ADULTNO,0)) DEP_ADULTNO, 
  sum(decode(S.SHIS_ARR_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) ARR_KIDS, 
  sum(decode(S.SHIS_DEP_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) DEP_KIDS, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,4,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) NOSHOW_ROOM,
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,3,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) CANCEL_ROOM,
  sum(S.SHIS_NETROOMREV) NETROOMREV, 
  sum(S.SHIS_NETFBREV) NETFBREV, 
  sum(S.SHIS_NETMINIBARREV) NETMINIBARREV, 
  sum(S.SHIS_NETTELEREV) NETTELEREV, 
  sum(S.SHIS_NETMISCREV) NETMISCREV, 
  sum(S.SHIS_NETREV) NETREV, 
  sum(S.SHIS_GROSSROOMREV) GROSSROOMREV, 
  sum(S.SHIS_GROSSFBREV) GROSSFBREV, 
  sum(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(S.SHIS_GROSSTELEREV) GROSSTELEREV, 
  sum(S.SHIS_GROSSMISCREV) GROSSMISCREV, 
 sum(S.SHIS_GROSSREV) GROSSREV 
FROM 
  wdat, 
  shis S 
  WHERE 
add_months(add_months(last_day(wdat.WDAT_DATE),-to_number(to_char(wdat_date,'mm')))+1,-12)<=S.SHIS_WDAT_DATE and 
S.SHIS_WDAT_DATE<=add_months(wdat.WDAT_DATE,-12) 
group by wdat.WDAT_DATE,S.SHIS_YCLN_3_XCMS_ID,shis_wphs_id
union 
SELECT 
  wdat.WDAT_DATE WDAT_DATE, 
  s.shis_wphs_id shis_wphs_id,
  S.SHIS_XCMS_ID XCMS_ID, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,0,decode(S.SHIS_DEP_ID,1,0,S.SHIS_YDET_NOOFROOMS))) NOOFROOMS,
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN S.SHIS_YDET_ADULTNO WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE S.SHIS_YDET_ADULTNO END )) ADULTNO, 
  SUM(DECODE(S.SHIS_YRES_RESSTATUS,3,0,CASE WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=1 THEN NVL(S.SHIS_TOTALYRGL_NUMBER,0) WHEN S.SHIS_DEP_ID=1 AND S.SHIS_ARR_ID=0 THEN 0 WHEN NVL(S.SHIS_YDET_ID,0)=0 THEN 0 ELSE NVL(S.SHIS_TOTALYRGL_NUMBER,0) END )) KIDS, 
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_NOOFROOMS,0)) ARRIVALS, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_NOOFROOMS,0)) DEPARTURES,  
  sum(decode(S.SHIS_ARR_ID,1,S.SHIS_YDET_ADULTNO,0)) ARR_ADULTNO, 
  sum(decode(S.SHIS_DEP_ID,1,S.SHIS_YDET_ADULTNO,0)) DEP_ADULTNO, 
  sum(decode(S.SHIS_ARR_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) ARR_KIDS, 
  sum(decode(S.SHIS_DEP_ID,1,nvl(S.SHIS_TOTALYRGL_NUMBER,0),0)) DEP_KIDS, 
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,4,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) NOSHOW_ROOM,
  sum(decode(S.SHIS_YRES_RESSTATUS,3,(decode(S.SHIS_YRES_NOAVAILREASON,3,(decode(trunc(S.SHIS_YRES_EXPARRTIME),trunc(S.SHIS_WDAT_DATE),S.SHIS_YDET_NOOFROOMS,0)))),0)) CANCEL_ROOM,
  sum(S.SHIS_NETROOMREV) NETROOMREV, 
  sum(S.SHIS_NETFBREV) NETFBREV, 
  sum(S.SHIS_NETMINIBARREV) NETMINIBARREV, 
  sum(S.SHIS_NETTELEREV) NETTELEREV, 
  sum(S.SHIS_NETMISCREV) NETMISCREV, 
  sum(S.SHIS_NETREV) NETREV, 
  sum(S.SHIS_GROSSROOMREV) GROSSROOMREV, 
  sum(S.SHIS_GROSSFBREV) GROSSFBREV, 
  sum(S.SHIS_GROSSMINIBARREV) GROSSMINIBARREV, 
  sum(S.SHIS_GROSSTELEREV) GROSSTELEREV, 
  sum(S.SHIS_GROSSMISCREV) GROSSMISCREV, 
 sum(S.SHIS_GROSSREV) GROSSREV 
FROM 
  wdat, 
  shis S 
  WHERE 
add_months(add_months(last_day(wdat.WDAT_DATE),-to_number(to_char(wdat_date,'mm')))+1,-12)<=S.SHIS_WDAT_DATE and 
S.SHIS_WDAT_DATE<=add_months(wdat.WDAT_DATE,-12) 
and (select xcca.XCCA_INTERNALCATEGORY from xcca,xcms where xcca.XCCA_ID=xcms.XCMS_XCCA_ID and xcms.XCMS_ID=S.SHIS_XCMS_ID)=3 
group by wdat.WDAT_DATE,S.SHIS_XCMS_ID ,shis_wphs_id
) 
group by WDAT_DATE,XCMS_ID,shis_wphs_id;

CREATE OR REPLACE VIEW V8_STAT_SHIS_XCMS_3_COMBI_LYSD
AS 
SELECT 
    WDAT_DATE WDAT_DATE,
    SHIS_WPHS_ID SHIS_WPHS_ID,
    XCMS_ID XCMS_ID,
    SUM(NOOFROOMS_D) NOOFROOMS_D,
    SUM(ADULTNO_D) ADULTNO_D,
    SUM(KIDS_D) KIDS_D,
    SUM(ARRIVALS_D) ARRIVALS_D,
    SUM(DEPARTURES_D) DEPARTURES_D,
    SUM(ARR_ADULTNO_D) ARR_ADULTNO_D,
    SUM(DEP_ADULTNO_D) DEP_ADULTNO_D,
    SUM(ARR_KIDS_D) ARR_KIDS_D,
    SUM(DEP_KIDS_D) DEP_KIDS_D,
    SUM(NOSHOW_ROOM_D) NOSHOW_ROOM_D,
    SUM(CANCEL_ROOM_D) CANCEL_ROOM_D, 
    SUM(NETROOMREV_D) NETROOMREV_D, 
    SUM(NETFBREV_D) NETFBREV_D, 
    SUM(NETMINIBARREV_D) NETMINIBARREV_D, 
    SUM(NETTELEREV_D) NETTELEREV_D, 
    SUM(NETMISCREV_D) NETMISCREV_D, 
    SUM(NETREV_D) NETREV_D,
    SUM(GROSSROOMREV_D) GROSSROOMREV_D,
    SUM(GROSSFBREV_D) GROSSFBREV_D,
    SUM(GROSSMINIBARREV_D) GROSSMINIBARREV_D,
    SUM(GROSSTELEREV_D) GROSSTELEREV_D,
    SUM(GROSSMISCREV_D) GROSSMISCREV_D,
    SUM(GROSSREV_D) GROSSREV_D,
    SUM(NOOFROOMS_M) NOOFROOMS_M,
    SUM(ADULTNO_M) ADULTNO_M,
    SUM(KIDS_M) KIDS_M,
    SUM(ARRIVALS_M) ARRIVALS_M,
    SUM(DEPARTURES_M) DEPARTURES_M,
    SUM(ARR_ADULTNO_M) ARR_ADULTNO_M,
    SUM(DEP_ADULTNO_M) DEP_ADULTNO_M,
    SUM(ARR_KIDS_M) ARR_KIDS_M,
    SUM(DEP_KIDS_M) DEP_KIDS_M,
    SUM(NOSHOW_ROOM_M) NOSHOW_ROOM_M,
    SUM(CANCEL_ROOM_M) CANCEL_ROOM_M, 
    SUM(NETROOMREV_M) NETROOMREV_M, 
    SUM(NETFBREV_M) NETFBREV_M, 
    SUM(NETMINIBARREV_M) NETMINIBARREV_M, 
    SUM(NETTELEREV_M) NETTELEREV_M, 
    SUM(NETMISCREV_M) NETMISCREV_M, 
    SUM(NETREV_M) NETREV_M,
    SUM(GROSSROOMREV_M) GROSSROOMREV_M,
    SUM(GROSSFBREV_M) GROSSFBREV_M,
    SUM(GROSSMINIBARREV_M) GROSSMINIBARREV_M,
    SUM(GROSSTELEREV_M) GROSSTELEREV_M,
    SUM(GROSSMISCREV_M) GROSSMISCREV_M,
    SUM(GROSSREV_M) GROSSREV_M,
    SUM(NOOFROOMS_Y) NOOFROOMS_Y,
    SUM(ADULTNO_Y) ADULTNO_Y,
    SUM(KIDS_Y) KIDS_Y,
    SUM(ARRIVALS_Y) ARRIVALS_Y,
    SUM(DEPARTURES_Y) DEPARTURES_Y,
    SUM(ARR_ADULTNO_Y) ARR_ADULTNO_Y,
    SUM(DEP_ADULTNO_Y) DEP_ADULTNO_Y,
    SUM(ARR_KIDS_Y) ARR_KIDS_Y,
    SUM(DEP_KIDS_Y) DEP_KIDS_Y,
    SUM(NOSHOW_ROOM_Y) NOSHOW_ROOM_Y,
    SUM(CANCEL_ROOM_Y) CANCEL_ROOM_Y, 
    SUM(NETROOMREV_Y) NETROOMREV_Y, 
    SUM(NETFBREV_Y) NETFBREV_Y, 
    SUM(NETMINIBARREV_Y) NETMINIBARREV_Y, 
    SUM(NETTELEREV_Y) NETTELEREV_Y, 
    SUM(NETMISCREV_Y) NETMISCREV_Y, 
    SUM(NETREV_Y) NETREV_Y,
    SUM(GROSSROOMREV_Y) GROSSROOMREV_Y,
    SUM(GROSSFBREV_Y) GROSSFBREV_Y,
    SUM(GROSSMINIBARREV_Y) GROSSMINIBARREV_Y,
    SUM(GROSSTELEREV_Y) GROSSTELEREV_Y,
    SUM(GROSSMISCREV_Y) GROSSMISCREV_Y,
    SUM(GROSSREV_Y) GROSSREV_Y,
    SUM(NOOFROOMS_D_LY) NOOFROOMS_D_LY,
    SUM(ADULTNO_D_LY) ADULTNO_D_LY,
    SUM(KIDS_D_LY) KIDS_D_LY,
    SUM(ARRIVALS_D_LY) ARRIVALS_D_LY,
    SUM(DEPARTURES_D_LY) DEPARTURES_D_LY,
    SUM(ARR_ADULTNO_D_LY) ARR_ADULTNO_D_LY,
    SUM(DEP_ADULTNO_D_LY) DEP_ADULTNO_D_LY,
    SUM(ARR_KIDS_D_LY) ARR_KIDS_D_LY,
    SUM(DEP_KIDS_D_LY) DEP_KIDS_D_LY,
    SUM(NOSHOW_ROOM_D_LY) NOSHOW_ROOM_D_LY,
    SUM(CANCEL_ROOM_D_LY) CANCEL_ROOM_D_LY, 
    SUM(NETROOMREV_D_LY) NETROOMREV_D_LY, 
    SUM(NETFBREV_D_LY) NETFBREV_D_LY, 
    SUM(NETMINIBARREV_D_LY) NETMINIBARREV_D_LY, 
    SUM(NETTELEREV_D_LY) NETTELEREV_D_LY, 
    SUM(NETMISCREV_D_LY) NETMISCREV_D_LY, 
    SUM(NETREV_D_LY) NETREV_D_LY,
    SUM(GROSSROOMREV_D_LY) GROSSROOMREV_D_LY,
    SUM(GROSSFBREV_D_LY) GROSSFBREV_D_LY,
    SUM(GROSSMINIBARREV_D_LY) GROSSMINIBARREV_D_LY,
    SUM(GROSSTELEREV_D_LY) GROSSTELEREV_D_LY,
    SUM(GROSSMISCREV_D_LY) GROSSMISCREV_D_LY,
    SUM(GROSSREV_D_LY) GROSSREV_D_LY,
    SUM(NOOFROOMS_M_LY) NOOFROOMS_M_LY,
    SUM(ADULTNO_M_LY) ADULTNO_M_LY,
    SUM(KIDS_M_LY) KIDS_M_LY,
    SUM(ARRIVALS_M_LY) ARRIVALS_M_LY,
    SUM(DEPARTURES_M_LY) DEPARTURES_M_LY,
    SUM(ARR_ADULTNO_M_LY) ARR_ADULTNO_M_LY,
    SUM(DEP_ADULTNO_M_LY) DEP_ADULTNO_M_LY,
    SUM(ARR_KIDS_M_LY) ARR_KIDS_M_LY,
    SUM(DEP_KIDS_M_LY) DEP_KIDS_M_LY,
    SUM(NOSHOW_ROOM_M_LY) NOSHOW_ROOM_M_LY,
    SUM(CANCEL_ROOM_M_LY) CANCEL_ROOM_M_LY, 
    SUM(NETROOMREV_M_LY) NETROOMREV_M_LY, 
    SUM(NETFBREV_M_LY) NETFBREV_M_LY, 
    SUM(NETMINIBARREV_M_LY) NETMINIBARREV_M_LY, 
    SUM(NETTELEREV_M_LY) NETTELEREV_M_LY, 
    SUM(NETMISCREV_M_LY) NETMISCREV_M_LY, 
    SUM(NETREV_M_LY) NETREV_M_LY,
    SUM(GROSSROOMREV_M_LY) GROSSROOMREV_M_LY,
    SUM(GROSSFBREV_M_LY) GROSSFBREV_M_LY,
    SUM(GROSSMINIBARREV_M_LY) GROSSMINIBARREV_M_LY,
    SUM(GROSSTELEREV_M_LY) GROSSTELEREV_M_LY,
    SUM(GROSSMISCREV_M_LY) GROSSMISCREV_M_LY,
    SUM(GROSSREV_M_LY) GROSSREV_M_LY,
    SUM(NOOFROOMS_Y_LY) NOOFROOMS_Y_LY,
    SUM(ADULTNO_Y_LY) ADULTNO_Y_LY,
    SUM(KIDS_Y_LY) KIDS_Y_LY,
    SUM(ARRIVALS_Y_LY) ARRIVALS_Y_LY,
    SUM(DEPARTURES_Y_LY) DEPARTURES_Y_LY,
    SUM(ARR_ADULTNO_Y_LY) ARR_ADULTNO_Y_LY,
    SUM(DEP_ADULTNO_Y_LY) DEP_ADULTNO_Y_LY,
    SUM(ARR_KIDS_Y_LY) ARR_KIDS_Y_LY,
    SUM(DEP_KIDS_Y_LY) DEP_KIDS_Y_LY,
    SUM(NOSHOW_ROOM_Y_LY) NOSHOW_ROOM_Y_LY,
    SUM(CANCEL_ROOM_Y_LY) CANCEL_ROOM_Y_LY, 
    SUM(NETROOMREV_Y_LY) NETROOMREV_Y_LY, 
    SUM(NETFBREV_Y_LY) NETFBREV_Y_LY, 
    SUM(NETMINIBARREV_Y_LY) NETMINIBARREV_Y_LY, 
    SUM(NETTELEREV_Y_LY) NETTELEREV_Y_LY, 
    SUM(NETMISCREV_Y_LY) NETMISCREV_Y_LY, 
    SUM(NETREV_Y_LY) NETREV_Y_LY,
    SUM(GROSSROOMREV_Y_LY) GROSSROOMREV_Y_LY,
    SUM(GROSSFBREV_Y_LY) GROSSFBREV_Y_LY,
    SUM(GROSSMINIBARREV_Y_LY) GROSSMINIBARREV_Y_LY,
    SUM(GROSSTELEREV_Y_LY) GROSSTELEREV_Y_LY,
    SUM(GROSSMISCREV_Y_LY) GROSSMISCREV_Y_LY,
    SUM(GROSSREV_Y_LY) GROSSREV_Y_LY
FROM (
    SELECT
        WDAT_DATE,
        SHIS_WPHS_ID,
        XCMS_ID, 
        NOOFROOMS NOOFROOMS_D,
        ADULTNO ADULTNO_D,
        KIDS KIDS_D,
        ARRIVALS ARRIVALS_D,
        DEPARTURES DEPARTURES_D,
        ARR_ADULTNO ARR_ADULTNO_D, 
        DEP_ADULTNO DEP_ADULTNO_D,
        ARR_KIDS ARR_KIDS_D,
        DEP_KIDS DEP_KIDS_D,
        NOSHOW_ROOM NOSHOW_ROOM_D,
        CANCEL_ROOM CANCEL_ROOM_D,
        NETROOMREV NETROOMREV_D,
        NETFBREV NETFBREV_D,
        NETMINIBARREV NETMINIBARREV_D,
        NETTELEREV NETTELEREV_D,
        NETMISCREV NETMISCREV_D, 
        NETREV NETREV_D,
        GROSSROOMREV GROSSROOMREV_D,
        GROSSFBREV GROSSFBREV_D,
        GROSSMINIBARREV GROSSMINIBARREV_D, 
        GROSSTELEREV GROSSTELEREV_D,
        GROSSMISCREV GROSSMISCREV_D,
        GROSSREV GROSSREV_D,
        0 NOOFROOMS_M,
        0 ADULTNO_M,
        0 KIDS_M,
        0 ARRIVALS_M,
        0 DEPARTURES_M,
        0 ARR_ADULTNO_M, 
        0 DEP_ADULTNO_M,
        0 ARR_KIDS_M,
        0 DEP_KIDS_M,
        0 NOSHOW_ROOM_M,
        0 CANCEL_ROOM_M,
        0 NETROOMREV_M,
        0 NETFBREV_M,
        0 NETMINIBARREV_M,
        0 NETTELEREV_M,
        0 NETMISCREV_M, 
        0 NETREV_M,
        0 GROSSROOMREV_M,
        0 GROSSFBREV_M,
        0 GROSSMINIBARREV_M, 
        0 GROSSTELEREV_M,
        0 GROSSMISCREV_M,
        0 GROSSREV_M,
        0 NOOFROOMS_Y,
        0 ADULTNO_Y,
        0 KIDS_Y,
        0 ARRIVALS_Y,
        0 DEPARTURES_Y,
        0 ARR_ADULTNO_Y, 
        0 DEP_ADULTNO_Y,
        0 ARR_KIDS_Y,
        0 DEP_KIDS_Y,
        0 NOSHOW_ROOM_Y,
        0 CANCEL_ROOM_Y,
        0 NETROOMREV_Y,
        0 NETFBREV_Y,
        0 NETMINIBARREV_Y,
        0 NETTELEREV_Y,
        0 NETMISCREV_Y, 
        0 NETREV_Y,
        0 GROSSROOMREV_Y,
        0 GROSSFBREV_Y,
        0 GROSSMINIBARREV_Y, 
        0 GROSSTELEREV_Y,
        0 GROSSMISCREV_Y,
        0 GROSSREV_Y,
        0 NOOFROOMS_D_LY, 
        0 ADULTNO_D_LY, 
        0 KIDS_D_LY, 
        0 ARRIVALS_D_LY, 
        0 DEPARTURES_D_LY, 
        0 ARR_ADULTNO_D_LY, 
        0 DEP_ADULTNO_D_LY, 
        0 ARR_KIDS_D_LY, 
        0 DEP_KIDS_D_LY, 
        0 NOSHOW_ROOM_D_LY,
        0 CANCEL_ROOM_D_LY, 
        0 NETROOMREV_D_LY, 
        0 NETFBREV_D_LY, 
        0 NETMINIBARREV_D_LY, 
        0 NETTELEREV_D_LY, 
        0 NETMISCREV_D_LY, 
        0 NETREV_D_LY, 
        0 GROSSROOMREV_D_LY,
        0 GROSSFBREV_D_LY, 
        0 GROSSMINIBARREV_D_LY,
        0 GROSSTELEREV_D_LY,
        0 GROSSMISCREV_D_LY, 
        0 GROSSREV_D_LY,
        0 NOOFROOMS_M_LY,
        0 ADULTNO_M_LY, 
        0 KIDS_M_LY, 
        0 ARRIVALS_M_LY,
        0 DEPARTURES_M_LY,
        0 ARR_ADULTNO_M_LY, 
        0 DEP_ADULTNO_M_LY,
        0 ARR_KIDS_M_LY,
        0 DEP_KIDS_M_LY, 
        0 NOSHOW_ROOM_M_LY, 
        0 CANCEL_ROOM_M_LY, 
        0 NETROOMREV_M_LY, 
        0 NETFBREV_M_LY, 
        0 NETMINIBARREV_M_LY, 
        0 NETTELEREV_M_LY, 
        0 NETMISCREV_M_LY, 
        0 NETREV_M_LY, 
        0 GROSSROOMREV_M_LY,
        0 GROSSFBREV_M_LY, 
        0 GROSSMINIBARREV_M_LY,
        0 GROSSTELEREV_M_LY,
        0 GROSSMISCREV_M_LY,
        0 GROSSREV_M_LY, 
        0 NOOFROOMS_Y_LY, 
        0 ADULTNO_Y_LY,
        0 KIDS_Y_LY, 
        0 ARRIVALS_Y_LY,
        0 DEPARTURES_Y_LY, 
        0 ARR_ADULTNO_Y_LY, 
        0 DEP_ADULTNO_Y_LY, 
        0 ARR_KIDS_Y_LY, 
        0 DEP_KIDS_Y_LY, 
        0 NOSHOW_ROOM_Y_LY, 
        0 CANCEL_ROOM_Y_LY, 
        0 NETROOMREV_Y_LY, 
        0 NETFBREV_Y_LY, 
        0 NETMINIBARREV_Y_LY, 
        0 NETTELEREV_Y_LY, 
        0 NETMISCREV_Y_LY, 
        0 NETREV_Y_LY,
        0 GROSSROOMREV_Y_LY,
        0 GROSSFBREV_Y_LY,
        0 GROSSMINIBARREV_Y_LY, 
        0 GROSSTELEREV_Y_LY, 
        0 GROSSMISCREV_Y_LY, 
        0 GROSSREV_Y_LY
    FROM V8_STAT_SHIS_XCMS_3_DAY
    UNION ALL
    SELECT
        WDAT_DATE,
        SHIS_WPHS_ID,
        XCMS_ID,
        0 NOOFROOMS_D,
        0 ADULTNO_D,
        0 KIDS_D,
        0 ARRIVALS_D,
        0 DEPARTURES_D,
        0 ARR_ADULTNO_D, 
        0 DEP_ADULTNO_D,
        0 ARR_KIDS_D,
        0 DEP_KIDS_D,
        0 NOSHOW_ROOM_D,
        0 CANCEL_ROOM_D,
        0 NETROOMREV_D,
        0 NETFBREV_D,
        0 NETMINIBARREV_D,
        0 NETTELEREV_D,
        0 NETMISCREV_D, 
        0 NETREV_D,
        0 GROSSROOMREV_D,
        0 GROSSFBREV_D,
        0 GROSSMINIBARREV_D, 
        0 GROSSTELEREV_D,
        0 GROSSMISCREV_D,
        0 GROSSREV_D,
        NOOFROOMS NOOFROOMS_M,
        ADULTNO ADULTNO_M,
        KIDS KIDS_M,
        ARRIVALS ARRIVALS_M,
        DEPARTURES DEPARTURES_M,
        ARR_ADULTNO ARR_ADULTNO_M, 
        DEP_ADULTNO DEP_ADULTNO_M,
        ARR_KIDS ARR_KIDS_M,
        DEP_KIDS DEP_KIDS_M,
        NOSHOW_ROOM NOSHOW_ROOM_M,
        CANCEL_ROOM CANCEL_ROOM_M,
        NETROOMREV NETROOMREV_M,
        NETFBREV NETFBREV_M,
        NETMINIBARREV NETMINIBARREV_M,
        NETTELEREV NETTELEREV_M,
        NETMISCREV NETMISCREV_M, 
        NETREV NETREV_M,
        GROSSROOMREV GROSSROOMREV_M,
        GROSSFBREV GROSSFBREV_M,
        GROSSMINIBARREV GROSSMINIBARREV_M, 
        GROSSTELEREV GROSSTELEREV_M,
        GROSSMISCREV GROSSMISCREV_M,
        GROSSREV GROSSREV_M,
        0 NOOFROOMS_Y,
        0 ADULTNO_Y,
        0 KIDS_Y,
        0 ARRIVALS_Y,
        0 DEPARTURES_Y,
        0 ARR_ADULTNO_Y, 
        0 DEP_ADULTNO_Y,
        0 ARR_KIDS_Y,
        0 DEP_KIDS_Y,
        0 NOSHOW_ROOM_Y,
        0 CANCEL_ROOM_Y,
        0 NETROOMREV_Y,
        0 NETFBREV_Y,
        0 NETMINIBARREV_Y,
        0 NETTELEREV_Y,
        0 NETMISCREV_Y, 
        0 NETREV_Y,
        0 GROSSROOMREV_Y,
        0 GROSSFBREV_Y,
        0 GROSSMINIBARREV_Y, 
        0 GROSSTELEREV_Y,
        0 GROSSMISCREV_Y,
        0 GROSSREV_Y,
        0 NOOFROOMS_D_LY, 
        0 ADULTNO_D_LY, 
        0 KIDS_D_LY, 
        0 ARRIVALS_D_LY, 
        0 DEPARTURES_D_LY, 
        0 ARR_ADULTNO_D_LY, 
        0 DEP_ADULTNO_D_LY, 
        0 ARR_KIDS_D_LY, 
        0 DEP_KIDS_D_LY, 
        0 NOSHOW_ROOM_D_LY,
        0 CANCEL_ROOM_D_LY, 
        0 NETROOMREV_D_LY, 
        0 NETFBREV_D_LY, 
        0 NETMINIBARREV_D_LY, 
        0 NETTELEREV_D_LY, 
        0 NETMISCREV_D_LY, 
        0 NETREV_D_LY, 
        0 GROSSROOMREV_D_LY,
        0 GROSSFBREV_D_LY, 
        0 GROSSMINIBARREV_D_LY,
        0 GROSSTELEREV_D_LY,
        0 GROSSMISCREV_D_LY, 
        0 GROSSREV_D_LY,
        0 NOOFROOMS_M_LY,
        0 ADULTNO_M_LY, 
        0 KIDS_M_LY, 
        0 ARRIVALS_M_LY,
        0 DEPARTURES_M_LY,
        0 ARR_ADULTNO_M_LY, 
        0 DEP_ADULTNO_M_LY,
        0 ARR_KIDS_M_LY,
        0 DEP_KIDS_M_LY, 
        0 NOSHOW_ROOM_M_LY, 
        0 CANCEL_ROOM_M_LY, 
        0 NETROOMREV_M_LY, 
        0 NETFBREV_M_LY, 
        0 NETMINIBARREV_M_LY, 
        0 NETTELEREV_M_LY, 
        0 NETMISCREV_M_LY, 
        0 NETREV_M_LY, 
        0 GROSSROOMREV_M_LY,
        0 GROSSFBREV_M_LY, 
        0 GROSSMINIBARREV_M_LY,
        0 GROSSTELEREV_M_LY,
        0 GROSSMISCREV_M_LY,
        0 GROSSREV_M_LY, 
        0 NOOFROOMS_Y_LY, 
        0 ADULTNO_Y_LY,
        0 KIDS_Y_LY, 
        0 ARRIVALS_Y_LY,
        0 DEPARTURES_Y_LY, 
        0 ARR_ADULTNO_Y_LY, 
        0 DEP_ADULTNO_Y_LY, 
        0 ARR_KIDS_Y_LY, 
        0 DEP_KIDS_Y_LY, 
        0 NOSHOW_ROOM_Y_LY, 
        0 CANCEL_ROOM_Y_LY, 
        0 NETROOMREV_Y_LY, 
        0 NETFBREV_Y_LY, 
        0 NETMINIBARREV_Y_LY, 
        0 NETTELEREV_Y_LY, 
        0 NETMISCREV_Y_LY, 
        0 NETREV_Y_LY,
        0 GROSSROOMREV_Y_LY,
        0 GROSSFBREV_Y_LY,
        0 GROSSMINIBARREV_Y_LY, 
        0 GROSSTELEREV_Y_LY, 
        0 GROSSMISCREV_Y_LY, 
        0 GROSSREV_Y_LY
    FROM V8_STAT_SHIS_XCMS_3_MONTH
    UNION ALL
    SELECT
        WDAT_DATE,
        SHIS_WPHS_ID,
        XCMS_ID,
        0 NOOFROOMS_D,
        0 ADULTNO_D,
        0 KIDS_D,
        0 ARRIVALS_D,
        0 DEPARTURES_D,
        0 ARR_ADULTNO_D, 
        0 DEP_ADULTNO_D,
        0 ARR_KIDS_D,
        0 DEP_KIDS_D,
        0 NOSHOW_ROOM_D,
        0 CANCEL_ROOM_D,
        0 NETROOMREV_D,
        0 NETFBREV_D,
        0 NETMINIBARREV_D,
        0 NETTELEREV_D,
        0 NETMISCREV_D, 
        0 NETREV_D,
        0 GROSSROOMREV_D,
        0 GROSSFBREV_D,
        0 GROSSMINIBARREV_D, 
        0 GROSSTELEREV_D,
        0 GROSSMISCREV_D,
        0 GROSSREV_D,
        0 NOOFROOMS_M,
        0 ADULTNO_M,
        0 KIDS_M,
        0 ARRIVALS_M,
        0 DEPARTURES_M,
        0 ARR_ADULTNO_M, 
        0 DEP_ADULTNO_M,
        0 ARR_KIDS_M,
        0 DEP_KIDS_M,
        0 NOSHOW_ROOM_M,
        0 CANCEL_ROOM_M,
        0 NETROOMREV_M,
        0 NETFBREV_M,
        0 NETMINIBARREV_M,
        0 NETTELEREV_M,
        0 NETMISCREV_M, 
        0 NETREV_M,
        0 GROSSROOMREV_M,
        0 GROSSFBREV_M,
        0 GROSSMINIBARREV_M, 
        0 GROSSTELEREV_M,
        0 GROSSMISCREV_M,
        0 GROSSREV_M,
        NOOFROOMS NOOFROOMS_Y,
        ADULTNO ADULTNO_Y,
        KIDS KIDS_Y,
        ARRIVALS ARRIVALS_Y,
        DEPARTURES DEPARTURES_Y,
        ARR_ADULTNO ARR_ADULTNO_Y, 
        DEP_ADULTNO DEP_ADULTNO_Y,
        ARR_KIDS ARR_KIDS_Y,
        DEP_KIDS DEP_KIDS_Y,
        NOSHOW_ROOM NOSHOW_ROOM_Y,
        CANCEL_ROOM CANCEL_ROOM_Y,
        NETROOMREV NETROOMREV_Y,
        NETFBREV NETFBREV_Y,
        NETMINIBARREV NETMINIBARREV_Y,
        NETTELEREV NETTELEREV_Y,
        NETMISCREV NETMISCREV_Y, 
        NETREV NETREV_Y,
        GROSSROOMREV GROSSROOMREV_Y,
        GROSSFBREV GROSSFBREV_Y,
        GROSSMINIBARREV GROSSMINIBARREV_Y, 
        GROSSTELEREV GROSSTELEREV_Y,
        GROSSMISCREV GROSSMISCREV_Y,
        GROSSREV GROSSREV_Y,
        0 NOOFROOMS_D_LY, 
        0 ADULTNO_D_LY, 
        0 KIDS_D_LY, 
        0 ARRIVALS_D_LY, 
        0 DEPARTURES_D_LY, 
        0 ARR_ADULTNO_D_LY, 
        0 DEP_ADULTNO_D_LY, 
        0 ARR_KIDS_D_LY, 
        0 DEP_KIDS_D_LY, 
        0 NOSHOW_ROOM_D_LY,
        0 CANCEL_ROOM_D_LY, 
        0 NETROOMREV_D_LY, 
        0 NETFBREV_D_LY, 
        0 NETMINIBARREV_D_LY, 
        0 NETTELEREV_D_LY, 
        0 NETMISCREV_D_LY, 
        0 NETREV_D_LY, 
        0 GROSSROOMREV_D_LY,
        0 GROSSFBREV_D_LY, 
        0 GROSSMINIBARREV_D_LY,
        0 GROSSTELEREV_D_LY,
        0 GROSSMISCREV_D_LY, 
        0 GROSSREV_D_LY,
        0 NOOFROOMS_M_LY,
        0 ADULTNO_M_LY, 
        0 KIDS_M_LY, 
        0 ARRIVALS_M_LY,
        0 DEPARTURES_M_LY,
        0 ARR_ADULTNO_M_LY, 
        0 DEP_ADULTNO_M_LY,
        0 ARR_KIDS_M_LY,
        0 DEP_KIDS_M_LY, 
        0 NOSHOW_ROOM_M_LY, 
        0 CANCEL_ROOM_M_LY, 
        0 NETROOMREV_M_LY, 
        0 NETFBREV_M_LY, 
        0 NETMINIBARREV_M_LY, 
        0 NETTELEREV_M_LY, 
        0 NETMISCREV_M_LY, 
        0 NETREV_M_LY, 
        0 GROSSROOMREV_M_LY,
        0 GROSSFBREV_M_LY, 
        0 GROSSMINIBARREV_M_LY,
        0 GROSSTELEREV_M_LY,
        0 GROSSMISCREV_M_LY,
        0 GROSSREV_M_LY, 
        0 NOOFROOMS_Y_LY, 
        0 ADULTNO_Y_LY,
        0 KIDS_Y_LY, 
        0 ARRIVALS_Y_LY,
        0 DEPARTURES_Y_LY, 
        0 ARR_ADULTNO_Y_LY, 
        0 DEP_ADULTNO_Y_LY, 
        0 ARR_KIDS_Y_LY, 
        0 DEP_KIDS_Y_LY, 
        0 NOSHOW_ROOM_Y_LY, 
        0 CANCEL_ROOM_Y_LY, 
        0 NETROOMREV_Y_LY, 
        0 NETFBREV_Y_LY, 
        0 NETMINIBARREV_Y_LY, 
        0 NETTELEREV_Y_LY, 
        0 NETMISCREV_Y_LY, 
        0 NETREV_Y_LY,
        0 GROSSROOMREV_Y_LY,
        0 GROSSFBREV_Y_LY,
        0 GROSSMINIBARREV_Y_LY, 
        0 GROSSTELEREV_Y_LY, 
        0 GROSSMISCREV_Y_LY, 
        0 GROSSREV_Y_LY
    FROM V8_STAT_SHIS_XCMS_3_YEAR
    UNION ALL
        SELECT
        WDAT_DATE,
        SHIS_WPHS_ID,
        XCMS_ID, 
        0 NOOFROOMS_D,
        0 ADULTNO_D,
        0 KIDS_D,
        0 ARRIVALS_D,
        0 DEPARTURES_D,
        0 ARR_ADULTNO_D, 
        0 DEP_ADULTNO_D,
        0 ARR_KIDS_D,
        0 DEP_KIDS_D,
        0 NOSHOW_ROOM_D,
        0 CANCEL_ROOM_D,
        0 NETROOMREV_D,
        0 NETFBREV_D,
        0 NETMINIBARREV_D,
        0 NETTELEREV_D,
        0 NETMISCREV_D, 
        0 NETREV_D,
        0 GROSSROOMREV_D,
        0 GROSSFBREV_D,
        0 GROSSMINIBARREV_D, 
        0 GROSSTELEREV_D,
        0 GROSSMISCREV_D,
        0 GROSSREV_D,
        0 NOOFROOMS_M,
        0 ADULTNO_M,
        0 KIDS_M,
        0 ARRIVALS_M,
        0 DEPARTURES_M,
        0 ARR_ADULTNO_M, 
        0 DEP_ADULTNO_M,
        0 ARR_KIDS_M,
        0 DEP_KIDS_M,
        0 NOSHOW_ROOM_M,
        0 CANCEL_ROOM_M,
        0 NETROOMREV_M,
        0 NETFBREV_M,
        0 NETMINIBARREV_M,
        0 NETTELEREV_M,
        0 NETMISCREV_M, 
        0 NETREV_M,
        0 GROSSROOMREV_M,
        0 GROSSFBREV_M,
        0 GROSSMINIBARREV_M, 
        0 GROSSTELEREV_M,
        0 GROSSMISCREV_M,
        0 GROSSREV_M,
        0 NOOFROOMS_Y,
        0 ADULTNO_Y,
        0 KIDS_Y,
        0 ARRIVALS_Y,
        0 DEPARTURES_Y,
        0 ARR_ADULTNO_Y, 
        0 DEP_ADULTNO_Y,
        0 ARR_KIDS_Y,
        0 DEP_KIDS_Y,
        0 NOSHOW_ROOM_Y,
        0 CANCEL_ROOM_Y,
        0 NETROOMREV_Y,
        0 NETFBREV_Y,
        0 NETMINIBARREV_Y,
        0 NETTELEREV_Y,
        0 NETMISCREV_Y, 
        0 NETREV_Y,
        0 GROSSROOMREV_Y,
        0 GROSSFBREV_Y,
        0 GROSSMINIBARREV_Y, 
        0 GROSSTELEREV_Y,
        0 GROSSMISCREV_Y,
        0 GROSSREV_Y,
        NOOFROOMS NOOFROOMS_D_LY,
        ADULTNO ADULTNO_D_LY,
        KIDS KIDS_D_LY,
        ARRIVALS ARRIVALS_D_LY,
        DEPARTURES DEPARTURES_D_LY,
        ARR_ADULTNO ARR_ADULTNO_D_LY, 
        DEP_ADULTNO DEP_ADULTNO_D_LY,
        ARR_KIDS ARR_KIDS_D_LY,
        DEP_KIDS DEP_KIDS_D_LY,
        NOSHOW_ROOM NOSHOW_ROOM_D_LY,
        CANCEL_ROOM CANCEL_ROOM_D_LY,
        NETROOMREV NETROOMREV_D_LY,
        NETFBREV NETFBREV_D_LY,
        NETMINIBARREV NETMINIBARREV_D_LY,
        NETTELEREV NETTELEREV_D_LY,
        NETMISCREV NETMISCREV_D_LY, 
        NETREV NETREV_D_LY,
        GROSSROOMREV GROSSROOMREV_D_LY,
        GROSSFBREV GROSSFBREV_D_LY,
        GROSSMINIBARREV GROSSMINIBARREV_D_LY, 
        GROSSTELEREV GROSSTELEREV_D_LY,
        GROSSMISCREV GROSSMISCREV_D_LY,
        GROSSREV GROSSREV_D_LY,
        0 NOOFROOMS_M_LY,
        0 ADULTNO_M_LY, 
        0 KIDS_M_LY, 
        0 ARRIVALS_M_LY,
        0 DEPARTURES_M_LY,
        0 ARR_ADULTNO_M_LY, 
        0 DEP_ADULTNO_M_LY,
        0 ARR_KIDS_M_LY,
        0 DEP_KIDS_M_LY, 
        0 NOSHOW_ROOM_M_LY, 
        0 CANCEL_ROOM_M_LY, 
        0 NETROOMREV_M_LY, 
        0 NETFBREV_M_LY, 
        0 NETMINIBARREV_M_LY, 
        0 NETTELEREV_M_LY, 
        0 NETMISCREV_M_LY, 
        0 NETREV_M_LY, 
        0 GROSSROOMREV_M_LY,
        0 GROSSFBREV_M_LY, 
        0 GROSSMINIBARREV_M_LY,
        0 GROSSTELEREV_M_LY,
        0 GROSSMISCREV_M_LY,
        0 GROSSREV_M_LY, 
        0 NOOFROOMS_Y_LY, 
        0 ADULTNO_Y_LY,
        0 KIDS_Y_LY, 
        0 ARRIVALS_Y_LY,
        0 DEPARTURES_Y_LY, 
        0 ARR_ADULTNO_Y_LY, 
        0 DEP_ADULTNO_Y_LY, 
        0 ARR_KIDS_Y_LY, 
        0 DEP_KIDS_Y_LY, 
        0 NOSHOW_ROOM_Y_LY, 
        0 CANCEL_ROOM_Y_LY, 
        0 NETROOMREV_Y_LY, 
        0 NETFBREV_Y_LY, 
        0 NETMINIBARREV_Y_LY, 
        0 NETTELEREV_Y_LY, 
        0 NETMISCREV_Y_LY, 
        0 NETREV_Y_LY,
        0 GROSSROOMREV_Y_LY,
        0 GROSSFBREV_Y_LY,
        0 GROSSMINIBARREV_Y_LY, 
        0 GROSSTELEREV_Y_LY, 
        0 GROSSMISCREV_Y_LY, 
        0 GROSSREV_Y_LY
    FROM V8_STAT_SHIS_XCMS_3_DAY_LY
    UNION ALL
    SELECT
        WDAT_DATE,
        SHIS_WPHS_ID,
        XCMS_ID, 
        0 NOOFROOMS_D,
        0 ADULTNO_D,
        0 KIDS_D,
        0 ARRIVALS_D,
        0 DEPARTURES_D,
        0 ARR_ADULTNO_D, 
        0 DEP_ADULTNO_D,
        0 ARR_KIDS_D,
        0 DEP_KIDS_D,
        0 NOSHOW_ROOM_D,
        0 CANCEL_ROOM_D,
        0 NETROOMREV_D,
        0 NETFBREV_D,
        0 NETMINIBARREV_D,
        0 NETTELEREV_D,
        0 NETMISCREV_D, 
        0 NETREV_D,
        0 GROSSROOMREV_D,
        0 GROSSFBREV_D,
        0 GROSSMINIBARREV_D, 
        0 GROSSTELEREV_D,
        0 GROSSMISCREV_D,
        0 GROSSREV_D,
        0 NOOFROOMS_M,
        0 ADULTNO_M,
        0 KIDS_M,
        0 ARRIVALS_M,
        0 DEPARTURES_M,
        0 ARR_ADULTNO_M, 
        0 DEP_ADULTNO_M,
        0 ARR_KIDS_M,
        0 DEP_KIDS_M,
        0 NOSHOW_ROOM_M,
        0 CANCEL_ROOM_M,
        0 NETROOMREV_M,
        0 NETFBREV_M,
        0 NETMINIBARREV_M,
        0 NETTELEREV_M,
        0 NETMISCREV_M, 
        0 NETREV_M,
        0 GROSSROOMREV_M,
        0 GROSSFBREV_M,
        0 GROSSMINIBARREV_M, 
        0 GROSSTELEREV_M,
        0 GROSSMISCREV_M,
        0 GROSSREV_M,
        0 NOOFROOMS_Y,
        0 ADULTNO_Y,
        0 KIDS_Y,
        0 ARRIVALS_Y,
        0 DEPARTURES_Y,
        0 ARR_ADULTNO_Y, 
        0 DEP_ADULTNO_Y,
        0 ARR_KIDS_Y,
        0 DEP_KIDS_Y,
        0 NOSHOW_ROOM_Y,
        0 CANCEL_ROOM_Y,
        0 NETROOMREV_Y,
        0 NETFBREV_Y,
        0 NETMINIBARREV_Y,
        0 NETTELEREV_Y,
        0 NETMISCREV_Y, 
        0 NETREV_Y,
        0 GROSSROOMREV_Y,
        0 GROSSFBREV_Y,
        0 GROSSMINIBARREV_Y, 
        0 GROSSTELEREV_Y,
        0 GROSSMISCREV_Y,
        0 GROSSREV_Y,
        0 NOOFROOMS_D_LY, 
        0 ADULTNO_D_LY, 
        0 KIDS_D_LY, 
        0 ARRIVALS_D_LY, 
        0 DEPARTURES_D_LY, 
        0 ARR_ADULTNO_D_LY, 
        0 DEP_ADULTNO_D_LY, 
        0 ARR_KIDS_D_LY, 
        0 DEP_KIDS_D_LY, 
        0 NOSHOW_ROOM_D_LY,
        0 CANCEL_ROOM_D_LY, 
        0 NETROOMREV_D_LY, 
        0 NETFBREV_D_LY, 
        0 NETMINIBARREV_D_LY, 
        0 NETTELEREV_D_LY, 
        0 NETMISCREV_D_LY, 
        0 NETREV_D_LY, 
        0 GROSSROOMREV_D_LY,
        0 GROSSFBREV_D_LY, 
        0 GROSSMINIBARREV_D_LY,
        0 GROSSTELEREV_D_LY,
        0 GROSSMISCREV_D_LY, 
        0 GROSSREV_D_LY,
        NOOFROOMS NOOFROOMS_M_LY,
        ADULTNO ADULTNO_M_LY,
        KIDS KIDS_M_LY,
        ARRIVALS ARRIVALS_M_LY,
        DEPARTURES DEPARTURES_M_LY,
        ARR_ADULTNO ARR_ADULTNO_M_LY, 
        DEP_ADULTNO DEP_ADULTNO_M_LY,
        ARR_KIDS ARR_KIDS_M_LY,
        DEP_KIDS DEP_KIDS_M_LY,
        NOSHOW_ROOM NOSHOW_ROOM_M_LY,
        CANCEL_ROOM CANCEL_ROOM_M_LY,
        NETROOMREV NETROOMREV_M_LY,
        NETFBREV NETFBREV_M_LY,
        NETMINIBARREV NETMINIBARREV_M_LY,
        NETTELEREV NETTELEREV_M_LY,
        NETMISCREV NETMISCREV_M_LY, 
        NETREV NETREV_M_LY,
        GROSSROOMREV GROSSROOMREV_M_LY,
        GROSSFBREV GROSSFBREV_M_LY,
        GROSSMINIBARREV GROSSMINIBARREV_M_LY, 
        GROSSTELEREV GROSSTELEREV_M_LY,
        GROSSMISCREV GROSSMISCREV_M_LY,
        GROSSREV GROSSREV_M_LY, 
        0 NOOFROOMS_Y_LY, 
        0 ADULTNO_Y_LY,
        0 KIDS_Y_LY, 
        0 ARRIVALS_Y_LY,
        0 DEPARTURES_Y_LY, 
        0 ARR_ADULTNO_Y_LY, 
        0 DEP_ADULTNO_Y_LY, 
        0 ARR_KIDS_Y_LY, 
        0 DEP_KIDS_Y_LY, 
        0 NOSHOW_ROOM_Y_LY, 
        0 CANCEL_ROOM_Y_LY, 
        0 NETROOMREV_Y_LY, 
        0 NETFBREV_Y_LY, 
        0 NETMINIBARREV_Y_LY, 
        0 NETTELEREV_Y_LY, 
        0 NETMISCREV_Y_LY, 
        0 NETREV_Y_LY,
        0 GROSSROOMREV_Y_LY,
        0 GROSSFBREV_Y_LY,
        0 GROSSMINIBARREV_Y_LY, 
        0 GROSSTELEREV_Y_LY, 
        0 GROSSMISCREV_Y_LY, 
        0 GROSSREV_Y_LY
    FROM V8_STAT_SHIS_XCMS_3_MONTH_LY
    UNION ALL 
    SELECT
        WDAT_DATE,
        SHIS_WPHS_ID,
        XCMS_ID, 
        0 NOOFROOMS_D,
        0 ADULTNO_D,
        0 KIDS_D,
        0 ARRIVALS_D,
        0 DEPARTURES_D,
        0 ARR_ADULTNO_D, 
        0 DEP_ADULTNO_D,
        0 ARR_KIDS_D,
        0 DEP_KIDS_D,
        0 NOSHOW_ROOM_D,
        0 CANCEL_ROOM_D,
        0 NETROOMREV_D,
        0 NETFBREV_D,
        0 NETMINIBARREV_D,
        0 NETTELEREV_D,
        0 NETMISCREV_D, 
        0 NETREV_D,
        0 GROSSROOMREV_D,
        0 GROSSFBREV_D,
        0 GROSSMINIBARREV_D, 
        0 GROSSTELEREV_D,
        0 GROSSMISCREV_D,
        0 GROSSREV_D,
        0 NOOFROOMS_M,
        0 ADULTNO_M,
        0 KIDS_M,
        0 ARRIVALS_M,
        0 DEPARTURES_M,
        0 ARR_ADULTNO_M, 
        0 DEP_ADULTNO_M,
        0 ARR_KIDS_M,
        0 DEP_KIDS_M,
        0 NOSHOW_ROOM_M,
        0 CANCEL_ROOM_M,
        0 NETROOMREV_M,
        0 NETFBREV_M,
        0 NETMINIBARREV_M,
        0 NETTELEREV_M,
        0 NETMISCREV_M, 
        0 NETREV_M,
        0 GROSSROOMREV_M,
        0 GROSSFBREV_M,
        0 GROSSMINIBARREV_M, 
        0 GROSSTELEREV_M,
        0 GROSSMISCREV_M,
        0 GROSSREV_M,
        0 NOOFROOMS_Y,
        0 ADULTNO_Y,
        0 KIDS_Y,
        0 ARRIVALS_Y,
        0 DEPARTURES_Y,
        0 ARR_ADULTNO_Y, 
        0 DEP_ADULTNO_Y,
        0 ARR_KIDS_Y,
        0 DEP_KIDS_Y,
        0 NOSHOW_ROOM_Y,
        0 CANCEL_ROOM_Y,
        0 NETROOMREV_Y,
        0 NETFBREV_Y,
        0 NETMINIBARREV_Y,
        0 NETTELEREV_Y,
        0 NETMISCREV_Y, 
        0 NETREV_Y,
        0 GROSSROOMREV_Y,
        0 GROSSFBREV_Y,
        0 GROSSMINIBARREV_Y, 
        0 GROSSTELEREV_Y,
        0 GROSSMISCREV_Y,
        0 GROSSREV_Y,
        0 NOOFROOMS_D_LY, 
        0 ADULTNO_D_LY, 
        0 KIDS_D_LY, 
        0 ARRIVALS_D_LY, 
        0 DEPARTURES_D_LY, 
        0 ARR_ADULTNO_D_LY, 
        0 DEP_ADULTNO_D_LY, 
        0 ARR_KIDS_D_LY, 
        0 DEP_KIDS_D_LY, 
        0 NOSHOW_ROOM_D_LY,
        0 CANCEL_ROOM_D_LY, 
        0 NETROOMREV_D_LY, 
        0 NETFBREV_D_LY, 
        0 NETMINIBARREV_D_LY, 
        0 NETTELEREV_D_LY, 
        0 NETMISCREV_D_LY, 
        0 NETREV_D_LY, 
        0 GROSSROOMREV_D_LY,
        0 GROSSFBREV_D_LY, 
        0 GROSSMINIBARREV_D_LY,
        0 GROSSTELEREV_D_LY,
        0 GROSSMISCREV_D_LY, 
        0 GROSSREV_D_LY,
        0 NOOFROOMS_M_LY,
        0 ADULTNO_M_LY, 
        0 KIDS_M_LY, 
        0 ARRIVALS_M_LY,
        0 DEPARTURES_M_LY,
        0 ARR_ADULTNO_M_LY, 
        0 DEP_ADULTNO_M_LY,
        0 ARR_KIDS_M_LY,
        0 DEP_KIDS_M_LY, 
        0 NOSHOW_ROOM_M_LY, 
        0 CANCEL_ROOM_M_LY, 
        0 NETROOMREV_M_LY, 
        0 NETFBREV_M_LY, 
        0 NETMINIBARREV_M_LY, 
        0 NETTELEREV_M_LY, 
        0 NETMISCREV_M_LY, 
        0 NETREV_M_LY, 
        0 GROSSROOMREV_M_LY,
        0 GROSSFBREV_M_LY, 
        0 GROSSMINIBARREV_M_LY,
        0 GROSSTELEREV_M_LY,
        0 GROSSMISCREV_M_LY,
        0 GROSSREV_M_LY,
        NOOFROOMS NOOFROOMS_Y_LY,
        ADULTNO ADULTNO_Y_LY,
        KIDS KIDS_Y_LY,
        ARRIVALS ARRIVALS_Y_LY,
        DEPARTURES DEPARTURES_Y_LY,
        ARR_ADULTNO ARR_ADULTNO_Y_LY, 
        DEP_ADULTNO DEP_ADULTNO_Y_LY,
        ARR_KIDS ARR_KIDS_Y_LY,
        DEP_KIDS DEP_KIDS_Y_LY,
        NOSHOW_ROOM NOSHOW_ROOM_Y_LY,
        CANCEL_ROOM CANCEL_ROOM_Y_LY,
        NETROOMREV NETROOMREV_Y_LY,
        NETFBREV NETFBREV_Y_LY,
        NETMINIBARREV NETMINIBARREV_Y_LY,
        NETTELEREV NETTELEREV_Y_LY,
        NETMISCREV NETMISCREV_Y_LY, 
        NETREV NETREV_Y_LY,
        GROSSROOMREV GROSSROOMREV_Y_LY,
        GROSSFBREV GROSSFBREV_Y_LY,
        GROSSMINIBARREV GROSSMINIBARREV_Y_LY, 
        GROSSTELEREV GROSSTELEREV_Y_LY,
        GROSSMISCREV GROSSMISCREV_Y_LY,
        GROSSREV GROSSREV_Y_LY
    FROM V8_STAT_SHIS_XCMS_3_YEAR_LY
)
GROUP BY WDAT_DATE,XCMS_ID,SHIS_WPHS_ID;

CREATE OR REPLACE VIEW V8_REP_ARR_DEP_IN_MEALPLAN
AS 
select
YDET_DATE,
YDET_ID,
(case when(YDET_DATE=YRES_EXPARRTIME) then 1 else 0 END) ARRIVAL,
(case when(YDET_DATE=YRES_EXPDEPTIME) then 1 else 0 END) DEPARTURE,
(case when(YDET_DATE<>YRES_EXPARRTIME and YDET_DATE<>YRES_EXPDEPTIME) then 1 else 0 END) INHOUSE,
FIDELIODATUM,
YDET_YRES_ID,
YRES_EXPARRTIME,
YRES_EXPDEPTIME,
YRES_XCMS_ID,
YDET_NOOFROOMS,
(YDET_NOOFROOMS*YDET_ADULTNO) YDET_ADULTNO,
(YDET_NOOFROOMS*TOTALKIDS) TOTALKIDS,
(YDET_NOOFROOMS*PAX) PAX,
YRES_RESSTATUS,
NO_BREAKFAST,
NO_LUNCH,
NO_DINNER
from
(SELECT 
ydet.YDET_DATE YDET_DATE,
YDET.YDET_ID,
V8_sys_fideliodate FIDELIODATUM,
ydet.YDET_YRES_ID YDET_YRES_ID,
(SELECT yres.YRES_EXPARRTIME FROM yres WHERE yres.YRES_ID = ydet.YDET_YRES_ID) YRES_EXPARRTIME,
(SELECT yres.YRES_EXPDEPTIME FROM yres WHERE yres.YRES_ID = ydet.YDET_YRES_ID) YRES_EXPDEPTIME,
(SELECT yres.yres_xcms_id FROM yres WHERE yres.YRES_ID = ydet.YDET_YRES_ID) YRES_XCMS_ID,
 NVL ((DECODE (NVL (ydet.YDET_SHARENUM, 0), 0, ydet.YDET_NOOFROOMS,(CASE WHEN ydet.YDET_ID =(SELECT MIN (ydet_2.YDET_ID) FROM ydet ydet_2 WHERE ydet_2.YDET_SHARENUM = ydet.YDET_SHARENUM) THEN (SELECT AVG (ydet_2.YDET_NOOFROOMS) FROM ydet ydet_2 WHERE ydet_2.YDET_SHARENUM = ydet.YDET_SHARENUM) ELSE 0 END))), 0) YDET_NOOFROOMS,
 NVL (ydet.YDET_ADULTNO, 0) YDET_ADULTNO,
 NVL ( (SELECT SUM (yrgl_number) FROM yrgl WHERE yrgl.YRGL_YDET_ID = ydet.YDET_ID), 0) TOTALKIDS,
 ( NVL (ydet.YDET_ADULTNO, 0) + NVL ( (SELECT SUM (yrgl_number) FROM yrgl WHERE yrgl.YRGL_YDET_ID = ydet.YDET_ID), 0)) PAX,
 (SELECT YRES.yres_RESSTATUS FROM yres WHERE yres.Yres_ID = ydet_Yres_id) yres_RESSTATUS,
 NVL((SELECT ybfo.YBFO_B FROM ybfo WHERE ybfo.YBFO_YRES_ID = YDET_YRES_ID AND ybfo.YBFO_DATE = YDET_DATE), 0)  NO_BREAKFAST,
NVL((SELECT ybfo.YBFO_L FROM ybfo WHERE ybfo.YBFO_YRES_ID = YDET_YRES_ID AND ybfo.YBFO_DATE = YDET_DATE), 0)  NO_LUNCH,
NVL ((SELECT ybfo.YBFO_D FROM ybfo WHERE     ybfo.YBFO_YRES_ID = YDET_YRES_ID AND ybfo.YBFO_DATE = YDET_DATE), 0) NO_DINNER
 FROM wdat, ydet
 where  wdat.WDAT_DATE = ydet.YDET_DATE(+)
 and (SELECT YRES.yres_RESSTATUS FROM yres WHERE yres.Yres_ID = ydet_Yres_id) <>3
 --and ydet_yres_id=4100
union all
(select
YDET_DATE,
YDET_ID,
FIDELIODATUM,
YDET_YRES_ID,
YRES_EXPARRTIME,
YRES_EXPDEPTIME,
YRES_XCMS_ID,
YDET_NOOFROOMS,
YDET_ADULTNO,
TOTALKIDS,
PAX,
YRES_RESSTATUS,
NVL((SELECT ybfo.YBFO_B FROM ybfo WHERE ybfo.YBFO_YRES_ID = YDET_YRES_ID AND ybfo.YBFO_DATE = YDET_DATE), 0)  NO_BREAKFAST,
NVL((SELECT ybfo.YBFO_L FROM ybfo WHERE ybfo.YBFO_YRES_ID = YDET_YRES_ID AND ybfo.YBFO_DATE = YDET_DATE), 0)  NO_LUNCH,
NVL ((SELECT ybfo.YBFO_D FROM ybfo WHERE     ybfo.YBFO_YRES_ID = YDET_YRES_ID AND ybfo.YBFO_DATE = YDET_DATE), 0) NO_DINNER
from
(select
ydet_date ydet_Date,
max(YDET_ID) YDET_ID,
MAX( FIDELIODATUM) FIDELIODATUM,
YDET_YRES_ID,
MAX(YRES_EXPARRTIME)YRES_EXPARRTIME,
MAX(YRES_EXPDEPTIME) YRES_EXPDEPTIME,
MAX(YRES_XCMS_ID) YRES_XCMS_ID,
YDET_NOOFROOMS,
YDET_ADULTNO,
TOTALKIDS,
PAX,
yres_RESSTATUS from
(SELECT 
(SELECT yres.YRES_EXPDEPTIME FROM yres WHERE yres.YRES_ID = ydet.YDET_YRES_ID) YDET_DATE,
YDET_ID,
V8_sys_fideliodate FIDELIODATUM,
ydet.YDET_YRES_ID YDET_YRES_ID,
(SELECT yres.YRES_EXPARRTIME FROM yres WHERE yres.YRES_ID = ydet.YDET_YRES_ID) YRES_EXPARRTIME,
(SELECT yres.YRES_EXPDEPTIME FROM yres WHERE yres.YRES_ID = ydet.YDET_YRES_ID) YRES_EXPDEPTIME,
(SELECT yres.yres_xcms_id FROM yres WHERE yres.YRES_ID = ydet.YDET_YRES_ID) YRES_XCMS_ID,
 NVL ((DECODE (NVL (ydet.YDET_SHARENUM, 0), 0, ydet.YDET_NOOFROOMS,(CASE WHEN ydet.YDET_ID =(SELECT MIN (ydet_2.YDET_ID) FROM ydet ydet_2 WHERE ydet_2.YDET_SHARENUM = ydet.YDET_SHARENUM) THEN (SELECT AVG (ydet_2.YDET_NOOFROOMS) FROM ydet ydet_2 WHERE ydet_2.YDET_SHARENUM = ydet.YDET_SHARENUM) ELSE 0 END))), 0) YDET_NOOFROOMS,
 NVL (ydet.YDET_ADULTNO, 0) YDET_ADULTNO,
 NVL ( (SELECT SUM (yrgl_number) FROM yrgl WHERE yrgl.YRGL_YDET_ID = ydet.YDET_ID), 0) TOTALKIDS,
 ( NVL (ydet.YDET_ADULTNO, 0) + NVL ( (SELECT SUM (yrgl_number) FROM yrgl WHERE yrgl.YRGL_YDET_ID = ydet.YDET_ID), 0)) PAX,
 (SELECT YRES.yres_RESSTATUS FROM yres WHERE yres.Yres_ID = ydet_Yres_id) yres_RESSTATUS
 FROM wdat, ydet
 where  wdat.WDAT_DATE = ydet.YDET_DATE(+) 
 and (SELECT YRES.yres_RESSTATUS FROM yres WHERE yres.Yres_ID = ydet_Yres_id)<>3
 --and ydet_yres_id=4100
 )
 group by YDET_DATE, YDET_YRES_ID, yres_RESSTATUS, YDET_NOOFROOMS,
YDET_ADULTNO,
TOTALKIDS,
PAX
)
)
order by ydet_date, ydet_yres_id);

CREATE OR REPLACE VIEW V8_REP_QUEST_ANSWERS
AS 
SELECT WQUG.WQUG_ID QUESTGROUP_ID,
          WQUG.WQUG_LONGDESC QUESTIONGROUP,
          WQNR.WQNR_ID QUESTFORM_ID,
          WQNR.WQNR_SHORTDESC QUESTFORM_SHORT,
          WQNR.WQNR_LONGDESC QUESTFORM_LONG,
          WQUE.WQUE_QUESTIONTEXT QUESTIONTEXT,
          WQUE.WQUE_QUESTIONKIND QUESTIONTYPE,
          WQAN.WQAN_SHORTDESC ANSWERTEXT,
          WQAN.WQAN_VALUE ANSWERVALUE,
          QNRF.QNRF_ENTERED ENTERED,
          TRUNC (QNRF.QNRF_ENTERED) ENTEREDDATE,
          QNRF.QNRF_GUEST_XCMS_ID GUEST_ID,
          QNRF.QNRF_GUEST_XCMS_ID MAILING_XCMS_ID,
          (SELECT A.NAME2
             FROM v8_rep_name a
            WHERE A.XCMS_ID = QNRF.QNRF_GUEST_XCMS_ID)
             GUESTNAME,
          QNRF.QNRF_YRES_ID RESERVATION_ID,
          (SELECT YRES.YRES_EXPARRTIME
             FROM yres
            WHERE YRES.YRES_ID = QNRF.QNRF_YRES_ID)
             ARRIVALDATE,
          (SELECT YRES.YRES_EXPDEPTIME
             FROM yres
            WHERE YRES.YRES_ID = QNRF.QNRF_YRES_ID)
             DEPARTUREDATE,
          DECODE (QNRF.QNRF_INPUTSOURCE,
                  2, 'Web entered',
                  1, 'Manual entered')
             INPUTSOURCE,
          1 COUNTNO,
          QNRF.QNRF_COMMENT QNRF_COMMENT,
          DECODE (QNRF.QNRF_COMMENT, NULL, 0, 1) IS_QNRF_COMMENT
     FROM WQUG,
          wqnr,
          wque,
          wqnl,
          WQAN,
          QNRF,
          QNRA
    WHERE     WQNL.WQNL_WQUG_ID = WQUG.WQUG_ID
          AND WQNL.WQNL_WQNR_ID = WQNR.WQNR_ID
          AND WQNL.WQNL_WQUE_ID = WQUE.WQUE_ID
          AND WQUE.WQUE_ID = WQAN.WQAN_WQUE_ID
          AND QNRF.QNRF_ID = QNRA.QNRA_QNRF_ID
          AND QNRF.QNRF_WQNR_ID = WQNR.WQNR_ID
          AND WQAN.WQAN_ID = QNRA.QNRA_WQAN_ID
   UNION ALL
   -- Entries with FREE TEXT
   SELECT (SELECT WQNL.WQNL_WQUG_ID
             FROM wqnl
            WHERE WQNL.WQNL_ID = QNRA.QNRA_WQNL_ID)
             questiongroup_id,
          (SELECT wqug_longdesc
             FROM wqug, wqnl
            WHERE WQNL.WQNL_ID = QNRA.QNRA_WQNL_ID AND wqug_id = wqnl_wqug_id)
             questiongroup,
          (SELECT WQNL.WQNL_WQNR_ID
             FROM wqnl
            WHERE WQNL.WQNL_ID = QNRA.QNRA_WQNL_ID)
             QUESTFORM_ID,
          (SELECT WQNR.WQNR_SHORTDESC
             FROM wqnr, wqnl
            WHERE     WQNL.WQNL_ID = QNRA.QNRA_WQNL_ID
                  AND WQNR.WQNR_ID = WQNL.WQNL_WQNR_ID)
             QUESTFORM_SHORT,
          (SELECT WQNR.WQNR_LONGDESC
             FROM wqnr, wqnl
            WHERE     WQNL.WQNL_ID = QNRA.QNRA_WQNL_ID
                  AND WQNR.WQNR_ID = WQNL.WQNL_WQNR_ID)
             QUESTFORM_LONG,
          (SELECT WQUE.WQUE_QUESTIONTEXT
             FROM wque, wqnl
            WHERE     WQNL.WQNL_ID = QNRA.QNRA_WQNL_ID
                  AND WQUE.WQUE_ID = WQNL.WQNL_WQUE_ID)
             QUESTIONTEXT,
          (SELECT WQUE.WQUE_QUESTIONKIND
             FROM wque, wqnl
            WHERE     WQNL.WQNL_ID = QNRA.QNRA_WQNL_ID
                  AND WQUE.WQUE_ID = WQNL.WQNL_WQUE_ID)
             QUESTIONTYPE,
          QNRA.QNRA_TEXT ANSWERTEXT,
          0 ANSWERVALUE,
          (SELECT QNRF.QNRF_ENTERED
             FROM qnrf
            WHERE QNRF.QNRF_ID = QNRA.QNRA_QNRF_ID)
             ENTERED,
          (SELECT TRUNC (QNRF.QNRF_ENTERED)
             FROM qnrf
            WHERE QNRF.QNRF_ID = QNRA.QNRA_QNRF_ID)
             ENTEREDDATE,
          (SELECT QNRF.QNRF_GUEST_XCMS_ID  --TT#23397
             FROM qnrf
            WHERE QNRF.QNRF_ID = QNRA.QNRA_QNRF_ID)
             GUEST_ID,
          (SELECT QNRF.QNRF_guest_XCMS_ID  --TT#23397
             FROM qnrf
            WHERE QNRF.QNRF_ID = QNRA.QNRA_QNRF_ID)
             MAILING_XCMS_ID,
          (SELECT v8_rep_name.NAME2
             FROM qnrf, v8_rep_name
            WHERE     v8_rep_name.xcms_id = QNRF.QNRF_GUEST_XCMS_ID --TT#23397
                  AND QNRF.QNRF_ID = QNRA.QNRA_QNRF_ID)
             GUESTNAME,
          (SELECT QNRF.QNRF_YRES_ID
             FROM qnrf
            WHERE QNRF.QNRF_ID = QNRA.QNRA_QNRF_ID)
             RESERVATION_ID,
          (SELECT YRES_EXPARRTIME
             FROM qnrf, yres
            WHERE     yres.yres_id = QNRF.QNRF_YRES_ID
                  AND QNRF.QNRF_ID = QNRA.QNRA_QNRF_ID)
             ARRIVALDATE,
          (SELECT YRES_EXPDEPTIME
             FROM qnrf, yres
            WHERE     yres.yres_id = QNRF.QNRF_YRES_ID
                  AND QNRF.QNRF_ID = QNRA.QNRA_QNRF_ID)
             DEPARTUREDATE,
          (SELECT DECODE (QNRF.QNRF_INPUTSOURCE,
                          2, 'Web entered',
                          1, 'Manual entered')
             FROM qnrf
            WHERE QNRF.QNRF_ID = QNRA.QNRA_QNRF_ID)
             INPUTSOURCE,
          1 COUNTNO,
          (SELECT QNRF.QNRF_COMMENT
             FROM qnrf
            WHERE QNRF.QNRF_ID = QNRA.QNRA_QNRF_ID)
             QNRF_COMMENT,
          DECODE ( (SELECT QNRF.QNRF_COMMENT
                      FROM qnrf
                     WHERE QNRF.QNRF_ID = QNRA.QNRA_QNRF_ID),
                  NULL, 0,
                  1)
             IS_QNRF_COMMENT
     FROM qnra
    WHERE NVL (QNRA.QNRA_WQAN_ID, 0) = 0;

CREATE OR REPLACE VIEW V8_REP_QUEST_GUEST_COMMENTS
AS 
SELECT  
     QNRF_ID, 
     WQNR_ID,
         (   x1.xcms_name1
          || DECODE (x1.xcms_name3, '', '', ', ' || x1.xcms_name3)
          || DECODE (x1.xcms_name2,
                     '', '',
                     DECODE (x1.xcms_name3, '', ', ', ' ') || x1.xcms_name2
                    )
         ) RESERVATIONNAME,
         (   x2.xcms_name1
          || DECODE (x2.xcms_name3, '', '', ', ' || x2.xcms_name3)
          || DECODE (x2.xcms_name2,
                     '', '',
                     DECODE (x2.xcms_name3, '', ', ', ' ') || x2.xcms_name2
                    )
         ) PROFILENAME,
    QNRF_FILLOUTDATE, 
    qnrf_entered, 
    qnrf_lastmodified, 
    qnrf_yres_id yres_id,
    YRES.YRES_EXPARRTIME ARRIVAL, 
    YRES.YRES_EXPDEPTIME DEPARTURE, 
    decode(nvl(YRES.YRES_YRMS_ID,0),0,(select YRMS.YRMS_SHORTDESC from ydet, yrms where YDET.YDET_YRMS_ID=YRMS.YRMS_ID and YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),(select YRMS.YRMS_SHORTDESC from yrms where YRMS.YRMS_ID=YRES.YRES_YRMS_ID))YRMS_SHORTDESC, 
    decode(QNRF.QNRF_INPUTSOURCE,2,'Web entered',1,'Manual entered') INPUTSOURCE,
    QNRF.QNRF_COMMENT, 
    wqnr_shortdesc, 
    wqnr_longdesc,
    (select WQUG.WQUG_LONGDESC from wqug where WQUG.WQUG_ID=(select WQNL.WQNL_WQUG_ID from wqnl where WQNL.WQNL_WQNR_ID= QNRF.QNRF_WQNR_ID and rownum=1)) DEPARTMENT_GROUP
FROM qnrf,wqnr, yres, xcms x1, xcms x2
WHERE qnrf_wqnr_id = wqnr_id
     AND qnrf_yres_id = yres_id(+)
     AND yres_xcms_id = x1.xcms_id(+)
     AND qnrf_guest_xcms_id = x2.xcms_id(+)
     AND QNRF.QNRF_COMMENT is not null
ORDER BY qnrf_entered, qnrf_yres_id, qnrf_xcms_id;

CREATE OR REPLACE VIEW V8_REP_PSTAT_STAY_IND
AS 
select s.SHIS_YRMS_SHORTDESC, 
       sum(s.SHIS_GROSSMISCREV) GROSSMISCBARREV,
       sum(s.SHIS_GROSSTELEREV) GROSSTELEREV,
       sum(s.SHIS_GROSSMINIBARREV) GROSSMINIBARREV,
       sum(s.SHIS_GROSSFBREV) GROSSFBREV,
       sum(s.SHIS_GROSSROOMREV) GROSSROOMREV, 
       sum(s.SHIS_GROSSREV) GROSSREV,           
       s.SHIS_YRES_EXPARRTIME, 
       s.SHIS_YRES_EXPDEPTIME, 
       shis_xcms_id, 
       sum(shis_ydet_noofrooms)  nights, 
       (select v8_rep_name.NAME2 from v8_rep_name where v8_rep_name.XCMS_ID = xcms.XCMS_ID) GuestName,
       decode(shis_ARR_ID,1,1,0) stays 
from shis s, xcms, 
     (select distinct shis_yres_id from shis where shis_xcms_id is not null and SHIS_YRES_RESSTATUS<>3) stays 
where 
     s.shis_yres_id = stays.shis_yres_id and xcms_id = shis_xcms_id and SHIS_DEP_ID <> 1 and s.SHIS_YRES_RESSTATUS<>3     
group by 
 s.SHIS_YRMS_SHORTDESC,s.shis_arr_id, s.SHIS_YRES_EXPARRTIME, s.SHIS_YRES_EXPDEPTIME, shis_xcms_id, xcms.XCMS_ID;

CREATE OR REPLACE VIEW V8_REP_PSTAT_STAY_COMPANY
AS 
select guest.XCMS_ID GUESTID, 
       cmp.XCMS_ID COMPANYID, 
       s.SHIS_YRES_ID, 
       s.SHIS_YRMS_SHORTDESC, 
       s.SHIS_BOOKERNAME,
       s.SHIS_XCMA_SHORTDESC,
       s.SHIS_YBLH_SHORTDESC,
       s.SHIS_YCAT_SHORTDESC,
       s.SHIS_YRCH_SHORTDESC,
       sum(s.SHIS_GROSSMISCREV) GROSSMISCBARREV,
       sum(s.SHIS_GROSSTELEREV) GROSSTELEREV,
       sum(s.SHIS_GROSSMINIBARREV) GROSSMINIBARREV,
       sum(s.SHIS_GROSSFBREV) GROSSFBREV,
       sum(s.SHIS_GROSSROOMREV) GROSSROOMREV, 
       sum(s.SHIS_GROSSREV) GROSSREV,       
       s.SHIS_YRES_EXPARRTIME, 
       s.SHIS_YRES_EXPDEPTIME, 
       sum(shis_ydet_noofrooms)  nights, 
       cmp.xcms_name1 companyName, 
       (select v8_rep_name.NAME2 from v8_rep_name where v8_rep_name.XCMS_ID = guest.XCMS_ID) GuestName,
       decode(shis_ARR_ID,1,1,0) stays
from shis s, xcms cmp, xcms guest
where 
  /*exists (select 1 from shis where shis_yres_id=s.shis_yres_id and 
      (SHIS_YCLN_1_XCMS_ID is not null 
   or SHIS_YCLN_2_XCMS_ID is not null 
   or SHIS_YCLN_3_XCMS_ID is not null 
   or SHIS_YCLN_4_XCMS_ID is not null 
   or SHIS_YCLN_5_XCMS_ID is not null 
   or SHIS_YCLN_6_XCMS_ID is not null
   )
   )  
     and */(cmp.xcms_id in (SHIS_YCLN_1_XCMS_ID, SHIS_YCLN_2_XCMS_ID, SHIS_YCLN_3_XCMS_ID, SHIS_YCLN_4_XCMS_ID, SHIS_YCLN_5_XCMS_ID, SHIS_YCLN_6_XCMS_ID))
     and SHIS_DEP_ID <> 1
     and guest.XCMS_ID = s.shis_xcms_id 
     and s.SHIS_YRES_RESSTATUS<>3     
group by 
 s.SHIS_YRES_ID, s.SHIS_YRMS_SHORTDESC, s.shis_arr_id,s.SHIS_YRES_EXPARRTIME, s.SHIS_YRES_EXPDEPTIME, cmp.xcms_name1, guest.xcms_name1 , guest.xcms_name2, guest.xcms_name3,guest.XCMS_ID,cmp.XCMS_ID,       s.SHIS_BOOKERNAME,
       s.SHIS_XCMA_SHORTDESC,
       s.SHIS_YBLH_SHORTDESC,
       s.SHIS_YCAT_SHORTDESC,
       s.SHIS_YRCH_SHORTDESC;

CREATE OR REPLACE VIEW V8_CCM_CANCELATIONS
AS 
select
  ycan.ycan_ybom_id,
  ycan.ycan_date_conf,
  ycan.ycan_amount_due_conf,
  ycan.ycan_percentage_conf,
  ycan.ycan_date_rms,
  ycan.ycan_amount_due_rms,
  ycan.ycan_percentage_rms
from
  ycan
  order by   ycan.ycan_ybom_id,  ycan.ycan_date_conf, ycan.ycan_percentage_conf, ycan.ycan_date_rms, ycan.ycan_percentage_rms;

CREATE OR REPLACE VIEW V8_CCM_EVENTS_BYDATE
AS 
select
  ybom.ybom_id,
  ybom.ybom_arrival,
  ybom.ybom_departure,
  ybom.ybom_noofattendees, 
  ybom.ybom_xcms_id,
  ybom.ybom_name,
  ybom.ybom_contactonsite,
  ybom.ybom_ind_xcms_id,
  ybom.ybom_infoboard,
  yevm.yevm_id,
  yevm.yevm_name,
  yevm.yevm_noofattendees,
  yevm.yevm_starttime,
  yevm.yevm_endtime,
  yevm.yevm_doorcard,
  yevm.YEVM_DOORCARD_PROF,
  yevm.yevm_displorder,
  xcms.comp_name name,
  xcms_ind.ind_name ind_name,
  yest.yest_id,
  yest.yest_shortdesc,
  yest.yest_cancelstatus,
  yest_event.yest_id event_yest_id,
  yest_event.yest_cancelstatus event_yest_cancelstatus,
  yest_event.yest_shortdesc event_shortdesc,
  (select xced.xced_initials from xced, ybml where YBML.YBML_YBOM_ID=ybom.ybom_id and YBML.YBML_TYPE=2 and YBML.YBML_XCMS_ID=xced.xced_id) XCED_INITIALS,
  yfsc.yfsc_longdesc,
  yebs.yebs_shareable,
  yebs.yebs_specialrate,
  yebs.yebs_costprice,
  yebs.yebs_yfsr_id,
  yfsc.yfsc_id,
  yssc.yssc_id,
  yssc.yssc_longdesc,
  yety_id,
  yety_longdesc,
  nvl((select YBML.YBML_XCMS_ID from ybml where YBML.YBML_YBOM_ID=ybom.ybom_id and YBML.YBML_TYPE=2),0) xced_id,
  nvl((SELECT max(noofrooms) FROM (SELECT sum(noofrooms) noofrooms, ydet_date, yres_ybom_id FROM ( SELECT sum(ybld.ybld_actualnoofrooms) noofrooms, ybld.ybld_date ydet_date, yblh.yblh_ybom_id yres_ybom_id FROM ybld,yblh,ybst WHERE yblh.yblh_ybst_id = ybst.ybst_id AND ybst.ybst_resstatus = 1 AND ybld.ybld_yblh_id = yblh.yblh_id GROUP BY ybld.ybld_date, yblh.yblh_ybom_id UNION ALL SELECT sum(ydet.ydet_noofrooms) noofrooms, ydet.ydet_date ydet_date, yres.yres_ybom_id yres_ybom_id FROM ydet,yres WHERE ydet.ydet_yres_id = yres.yres_id AND nvl(ydet.ydet_ybld_id,0) = 0 AND yres.yres_resstatus = 1 AND (nvl(yres.yres_hasshare,0) = 0 OR ydet.ydet_id = (SELECT min(ydet2.ydet_id) FROM ydet ydet2 WHERE ydet2.ydet_sharenum = ydet.ydet_sharenum)) AND nvl(ydet.ydet_ygrp_id,0) = 0 AND nvl(yres.yres_ygrp_id,0) = 0 GROUP BY ydet.ydet_date,yres.yres_ybom_id UNION ALL SELECT sum(ydet.ydet_noofrooms) noofrooms, ydet.ydet_date ydet_date, ygrp.ygrp_ybom_id yres_ybom_id FROM ydet,yres,ygrp WHERE ydet.ydet_yres_id = yres.yres_id AND (  ydet.ydet_ygrp_id = ygrp.ygrp_id OR yres.yres_ygrp_id = ygrp.ygrp_id) AND nvl(ydet.ydet_ybld_id,0) = 0 AND yres.yres_resstatus = 1 AND (nvl(yres.yres_hasshare,0) = 0 OR ydet.ydet_id = (SELECT min(ydet2.ydet_id) FROM ydet ydet2 WHERE ydet2.ydet_sharenum = ydet.ydet_sharenum)) GROUP BY ydet.ydet_date, ygrp.ygrp_ybom_id) GROUP BY ydet_date, yres_ybom_id) WHERE yres_ybom_id = ybom.ybom_id),0) noofrooms,
  nvl(YBOM.YBOM_YCAD_ID,0) YBOM_YCAD_ID,
  nvl(YFSC.YFSC_YCAD_ID,0) YFSC_YCAD_ID
from
  ybom,
  yevm,
  v8_ccm_contract_prof xcms ,
  v8_ccm_contract_prof xcms_ind,
  yest,
  yest yest_event,
  yebs,
  yfsc,
  yssl,
  yssc,
  yety
where
  ybom.ybom_id(+) = yevm.yevm_ybom_id and
  ybom.ybom_xcms_id = xcms.xcms_id(+) and
  ybom.ybom_ind_xcms_id = xcms_ind.xcms_id(+) and
  ybom.ybom_con_yest_id = yest.yest_id(+) and
  yevm.yevm_yest_id = yest_event.yest_id(+) and
  yevm.yevm_id = yebs.yebs_yevm_id(+) and
  yebs.yebs_yfsc_id = yfsc.yfsc_id(+) and
  yebs.yebs_yssl_id = yssl.yssl_id(+) and
  yssl.yssl_yssc_id = yssc.yssc_id(+) and
  yevm.yevm_yety_id = yety.yety_id(+) and
  nvl(yevm.yevm_mergedto_yevm_id, 0)=0;

CREATE OR REPLACE VIEW V8_CCM_BOOKING_LIST
AS 
select
  ybom.ybom_id ybom_id,
  ybom.ybom_xcms_id ybom_xcms_id,
  ybom.ybom_name ybom_name,
  ybom.ybom_contactonsite ybom_contactonsite,
  ybom.ybom_arrival ybom_arrival,
  ybom.ybom_departure ybom_departure,
  ybom.ybom_catdecisiondate ybom_catdecisiondate,
  ybom.ybom_catfollowupdate ybom_catfollowupdate,
  ybom.ybom_catcutoffdate ybom_catcutoffdate,
  ybom.ybom_noofattendees ybom_noofattendees,
  (select YBLH.YBLH_ROOMINGLISTDATE from yblh where yblh.yblh_id=ybom.ybom_id) yblh_roominglistdate,
  ybom.ybom_rmsfollowupdate ybom_rmsfollowupdate,
  ybom.ybom_detailsok ybom_detailsok,
  ybom.ybom_distributed ybom_distributed,
  ybom.ybom_xcms_id xcms_id,
  (select xcms.xcms_name1||' '||xcms.xcms_name2||' '||xcms.xcms_name3 from xcms where xcms.xcms_id=ybom.ybom_xcms_id) name,
  ybom.ybom_ind_xcms_id xcms_ind_id,
  (select xcms.xcms_name1||' '||xcms.xcms_name2||' '||xcms.xcms_name3 from xcms where xcms.xcms_id=ybom.ybom_ind_xcms_id) ind_name,
  (select xcms.xcms_name1||', '||xcms.xcms_name3||' '||xcms.xcms_name2 from xcms where xcms.xcms_id=ybom.ybom_ind_xcms_id) ind_name2,
  ybom.ybom_con_yest_id yest_id,
  (select yest.yest_shortdesc from yest where yest.yest_id=ybom.ybom_con_yest_id) yest_shortdesc,
  (select yest.yest_cancelstatus from yest where yest.yest_id=ybom.ybom_con_yest_id) yest_cancelstatus,
  (select XCED.XCED_INITIALS from xced, ybml where YBML.YBML_YBOM_ID=ybom.ybom_id and YBML.YBML_TYPE=2 and YBML.YBML_XCMS_ID=xced.xced_id) xced_initials,
  (select YBML.YBML_XCMS_ID from ybml where YBML.YBML_YBOM_ID=ybom.ybom_id and YBML.YBML_TYPE=2) xced_id,
  (select decode(comm.ind_tel_xcom_value,'',comm.comp_tel_xcmt_id ,comm.ind_tel_xcmt_id) from v8_ccm_booking_comm comm where COMM.YBOM_ID=YBOM.YBOM_ID) tel_xcmt_id,
  (select decode(comm.ind_tel_xcom_value,'',comm.comp_tel_xcmt_longdesc ,comm.ind_tel_xcmt_longdesc) from v8_ccm_booking_comm comm where COMM.YBOM_ID=YBOM.YBOM_ID) tel_xcmt_longdesc,
  (select decode(comm.ind_tel_xcom_value,'',comm.comp_tel_xcom_value    ,comm.ind_tel_xcom_value   ) from v8_ccm_booking_comm comm where COMM.YBOM_ID=YBOM.YBOM_ID) tel_xcom_value,
  (select decode(comm.ind_fax_xcom_value,'',comm.comp_fax_xcmt_id ,comm.ind_fax_xcmt_id) from v8_ccm_booking_comm comm where COMM.YBOM_ID=YBOM.YBOM_ID) fax_xcmt_id,
  (select decode(comm.ind_fax_xcom_value,'',comm.comp_fax_xcmt_longdesc ,comm.ind_fax_xcmt_longdesc) from v8_ccm_booking_comm comm where COMM.YBOM_ID=YBOM.YBOM_ID) fax_xcmt_longdesc,
  (select decode(comm.ind_fax_xcom_value,'',comm.comp_fax_xcom_value    ,comm.ind_fax_xcom_value  ) from v8_ccm_booking_comm comm where COMM.YBOM_ID=YBOM.YBOM_ID) fax_xcom_value,
  (select wmlg.wmlg_time_creation from wmlg where WMLG.WMLG_TABLE_ID=ybom.ybom_id and WMLG.WMLG_TABLENAME='YBOM' ) creation_date,
  nvl(YBOM.YBOM_CON_YEST_ID,0) conf_status_id,
  (select yest.yest_shortdesc from yest where yest.yest_id=YBOM.YBOM_CON_YEST_ID) conf_status_shortdesc,
  nvl((select YBML.YBML_XCMS_ID from ybml where YBML.YBML_YBOM_ID=ybom.ybom_id and YBML.YBML_TYPE=2),0) conf_manager_id,
  (select XCED.XCED_INITIALS from xced, ybml where YBML.YBML_YBOM_ID=ybom.ybom_id and YBML.YBML_TYPE=2 and YBML.YBML_XCMS_ID=xced.xced_id) conf_manager_initials,
  nvl(YBOM.YBOM_RMS_YEST_ID,0) rooms_status_id,
  (select yest.yest_shortdesc from yest where yest.yest_id=YBOM.YBOM_RMS_YEST_ID) rooms_status_shortdesc,
  nvl((select YBML.YBML_XCMS_ID from ybml where YBML.YBML_YBOM_ID=ybom.ybom_id and YBML.YBML_TYPE=3),0) rooms_manager_id,
  (select XCED.XCED_INITIALS from xced, ybml where YBML.YBML_YBOM_ID=ybom.ybom_id and YBML.YBML_TYPE=3 and YBML.YBML_XCMS_ID=xced.xced_id) rooms_manager_initials,
  nvl((select YBML.YBML_XCMS_ID from ybml where YBML.YBML_YBOM_ID=ybom.ybom_id and YBML.YBML_TYPE=1),0) account_manager_id,
  (select XCED.XCED_INITIALS from xced, ybml where YBML.YBML_YBOM_ID=ybom.ybom_id and YBML.YBML_TYPE=1 and YBML.YBML_XCMS_ID=xced.xced_id) account_manager_initials,
  (select YBLH.YBLH_ID from yblh where YBLH.YBLH_YBOM_ID=YBOM.YBOM_ID) yblh_id,
  (select YBLH.YBLH_SHORTDESC from yblh where YBLH.YBLH_YBOM_ID=YBOM.YBOM_ID) yblh_shortdesc,
  (select YBLH.YBLH_YBST_ID from yblh where YBLH.YBLH_YBOM_ID=YBOM.YBOM_ID) yblh_ybst_id,
  (select YBLH.YBLH_STARTDATE from yblh where YBLH.YBLH_YBOM_ID=YBOM.YBOM_ID) yblh_startdate,
  (select YBLH.YBLH_ENDDATE from yblh where YBLH.YBLH_YBOM_ID=YBOM.YBOM_ID) yblh_enddate,
  YBOM.YBOM_RMSDECISIONDATE ybom_RMSdecisiondate,
  (select YBLH.YBLH_CUTOFFDATE from yblh where YBLH.YBLH_YBOM_ID=YBOM.YBOM_ID) yblh_cutoffdate,
  ybom.ybom_id ybis_ybom_id,
  (select YEST.YEST_CANCELSTATUS from yest where YEST.YEST_ID=YBOM.YBOM_RMS_YEST_ID) rooms_yest_cancelstatus,
  ybom.ybom_ygrt_id ybom_ygrt_id,
  ybom.ybom_ygrt_id ygrt_id,
  (select YGRT.YGRT_NAME from ygrt where ygrt.ygrt_id=YBOM.YBOM_YGRT_ID) ygrt_name,
  YBOM.YBOM_XCMA_ID xcma_id,
  (select XCMA.XCMA_LONGDESC from xcma where XCMA.XCMA_ID=YBOM.YBOM_XCMA_ID) xcma_longdesc,
  YBOM.YBOM_YRAS_ID yras_id,
  (select YRAS.YRAS_LONGDESC from yras where YRAS.YRAS_ID=YBOM.YBOM_YRAS_ID) yras_longdesc,
   YBOM.YBOM_YFTY_ID ybom_yfty_id,  -- 18.06.2012 new field MHoffmann 
  (select YFTY.YFTY_LONGDESC from yfty where YFTY.YFTY_ID=YBOM.YBOM_YFTY_ID) yfty_longdesc,
  nvl(YBOM.YBOM_YCAD_ID,0) YBOM_YCAD_ID
from
  ybom;

create or replace view v8_ccm_yevm_mergedattendees
as
select v.yevm_id,
(select sum(v1.yevm_noofattendees) 
 from yevm v1 
 where NVL(v1.yevm_mergedto_yevm_id, 0) <> 0 AND v1.yevm_mergedto_yevm_id = v.yevm_id ) merged_yevm_noofattendees
from yevm v;

CREATE OR REPLACE FORCE VIEW V8_CCM_FORECAST_REVENUE
AS
   SELECT ybef_yevm_id,
          ybef_per_person,
          ybef_flat_amount,
          (yevm_noofattendees - nvl(ym.merged_yevm_noofattendees, 0)  ) * ybef_per_person + ybef_flat_amount TotalForecast,
          DECODE (NVL (ybef_ypkd_id, 0), 0, ybef_rev_amount, ybef_rev_amount * (yevm_noofattendees - nvl(ym.merged_yevm_noofattendees, 0)  )) TotalRevenue,
          DECODE (NVL (ybef_ypkd_id, 0), 0, ybef_price_net, ybef_price_net * (yevm_noofattendees - nvl(ym.merged_yevm_noofattendees, 0)  )) TotalNetRevenue,
          DECODE (NVL (ybef_ypkd_id, 0), 0, ybef_price_vat, ybef_price_vat * (yevm_noofattendees - nvl(ym.merged_yevm_noofattendees, 0)  )) TotalVat,
          DECODE (NVL (ybef_ypkd_id, 0), 0, ybef_rev_amount) RevenueExclPKG,
          zdco_longdesc,
          zdco_id,
          yest_id,
          yest_longdesc,
          yest_postforecast,
          yest_postrevenue,
          yest_cancelstatus,
          yety_id,
          yety_longdesc,
          TRUNC (yevm_starttime) start_date,
          ybef_zdco_id,
          yevm_yest_id,
          yevm_ybom_id,
          zcur_shortdesc,
          yfty_id,
          yfty_shortdesc,
          yfty_longdesc,
          ybom_detailsok,
          ybef_alternative AlternativeFlag
     FROM ybef,
          zdco,
          yevm,
          yest,
          yety,
          zcur,
          yfty,
          ybom,
          v8_ccm_yevm_mergedattendees ym
    WHERE     ybef_zdco_id = zdco_id
          AND ybef_yevm_id = yevm.yevm_id
          AND yevm_yest_id = yest_id
          AND yevm_yety_id = yety_id
          AND ybef_zcur_id = zcur.zcur_id(+)
          AND yevm_ybom_id = ybom_id
          AND ybom_yfty_id = yfty_id(+)
          AND yevm.yevm_id = ym.yevm_id;

CREATE OR REPLACE VIEW V8_STAT_CCM_RES_FORREV_PK
AS 
SELECT TRUNC (yevm_starttime) wdat_date,
          ybom_id,
          yevm.yevm_id,
          zdco_id,
          ZDCO.ZDCO_ZDSG_ID,
          zdco_stats_type,
          5 ccm_type,
          yevm_noofattendees - NVL (ym.merged_yevm_noofattendees, 0)
             yevm_noofattendees,
            (yevm_noofattendees - NVL (ym.merged_yevm_noofattendees, 0))
          * ybef_rev_amount
             grossrev,
            (yevm_noofattendees - NVL (ym.merged_yevm_noofattendees, 0))
          * ybef_org_rev_amount
             orgrev,
          (SELECT SUM (zpos_quantity * zpos_grossunitprice)
             FROM zpos,
                  (SELECT yblp_id,
                          yblp_resource_id,
                          yblp_resource_type,
                          zpos_id
                     FROM zpos, yblp
                    WHERE zpos_id = yblp_zpos_id) v
            WHERE     zpos.zpos_id = v.zpos_id
                  AND v.yblp_resource_id = ybef.ybef_id
                  AND v.yblp_resource_type = 1)
             posted_grossrev,
          (SELECT SUM (zpos_quantity * zpos_unitprice)
             FROM zpos,
                  (SELECT yblp_id,
                          yblp_resource_id,
                          yblp_resource_type,
                          zpos_id
                     FROM zpos, yblp
                    WHERE zpos_id = yblp_zpos_id) v
            WHERE     zpos.zpos_id = v.zpos_id
                  AND v.yblp_resource_id = ybef.ybef_id
                  AND v.yblp_resource_type = 1)
             posted_netrev,
          (SELECT SUM (zpos_quantity * zpos_unitprice)
             FROM zpos,
                  (SELECT yblp_id,
                          yblp_resource_id,
                          yblp_resource_type,
                          z2.zpos_id
                     FROM zpos z1, zpos z2, yblp
                    WHERE     z1.zpos_taxlink_id = z2.zpos_taxlink_id
                          AND z1.zpos_id = yblp_zpos_id
                          AND z2.zpos_cdt = 2) v
            WHERE     zpos.zpos_id = v.zpos_id
                  AND v.yblp_resource_id = ybef.ybef_id
                  AND v.yblp_resource_type = 1)
             posted_tax,
          ybomyest.yest_cancelstatus ybom_yest_cancelstatus,
          yest.yest_cancelstatus yevm_yest_cancelstatus,
          YBOM.YBOM_CON_YEST_ID,
          YEVM.YEVM_YEST_ID,
          (SELECT TRUNC (wmlg_time_creation)
             FROM wmlg
            WHERE     wmlg_tablename = 'YBEF'
                  AND wmlg_table_id = ybef_id
                  AND ROWNUM = 1)
             production_date,
          ybef_alternative alternative,
            (yevm_noofattendees - NVL (ym.merged_yevm_noofattendees, 0))
          * ybef_price_net
             Net,
            (yevm_noofattendees - NVL (ym.merged_yevm_noofattendees, 0))
          * ybef_price_Vat
             Vat
     FROM yevm,
          ybef,
          zdco,
          yest,
          ybom,
          yest ybomyest,
          v8_ccm_yevm_mergedattendees ym
    WHERE     NVL (yevm_pkgincl, 0) = 1
          AND ybef_yevm_id = yevm.yevm_id
          AND NVL (ybef_ypkd_id, 0) > 0
          AND NVL (ybef_rev_amount, 0) <> 0
          AND ybef_zdco_id = zdco_id
          AND yevm_yest_id = yest.yest_id
          AND yest.yest_postrevenue = 1
          AND yevm_ybom_id = ybom_id
          AND ybom_con_yest_id = ybomyest.yest_id
          AND (       NVL (ybomyest.yest_cancelstatus, 0) = 0
                  AND NVL (yest.yest_cancelstatus, 0) <> 1
               OR NVL (ybomyest.yest_cancelstatus, 0) = 1)
          AND yevm.yevm_id = ym.yevm_id;

CREATE OR REPLACE VIEW V8_STAT_CCM_RES_FORREV_EXP
AS 
SELECT TRUNC (yevm_starttime) wdat_date,
          ybom_id,
          yevm.yevm_id,
          zdco_id,
          ZDCO_ZDSG_ID,
          0 zdco_stats_type,
          6 ccm_type,
          yevm_noofattendees - NVL (ym.merged_yevm_noofattendees, 0)
             yevm_noofattendees,
              (yevm_noofattendees - NVL (ym.merged_yevm_noofattendees, 0))
            * ybef_per_person
          + ybef_flat_amount
             grossrev,
          0 orgrev,
          0 posted_grossrev,
          0 posted_netrev,
          0 posted_tax,
          ybomyest.yest_cancelstatus ybom_yest_cancelstatus,
          yest.yest_cancelstatus yevm_yest_cancelstatus,
          YBOM.YBOM_CON_YEST_ID,
          YEVM.YEVM_YEST_ID,
          (SELECT TRUNC (wmlg_time_creation)
             FROM wmlg
            WHERE     wmlg_tablename = 'YBEF'
                  AND wmlg_table_id = ybef_id
                  AND ROWNUM = 1)
             production_date,
          ybef_alternative alternative,
              (yevm_noofattendees - NVL (ym.merged_yevm_noofattendees, 0))
            * ybef_per_person
          + ybef_flat_amount
          - evaluateVAT (
               NVL (zdco_id, 0),
               TRUNC (yevm_starttime),
                   (  yevm_noofattendees
                    - NVL (ym.merged_yevm_noofattendees, 0))
                 * ybef_per_person
               + ybef_flat_amount)
             Net,
          0 Vat
     FROM yevm,
          ybef,
          zdco,
          yest,
          ybom,
          yest ybomyest,
          v8_ccm_yevm_mergedattendees ym
    WHERE     ybef_yevm_id = YEVM.yevm_id
          AND (   NVL (ybef_per_person, 0) <> 0
               OR NVL (ybef_flat_amount, 0) <> 0)
          AND ybef_zdco_id = zdco_id
          AND yevm_yest_id = yest.yest_id
          AND yest.yest_postrevenue = 1
          AND yevm_ybom_id = ybom_id
          AND ybom_con_yest_id = ybomyest.yest_id
          AND (       NVL (ybomyest.yest_cancelstatus, 0) = 0
                  AND NVL (yest.yest_cancelstatus, 0) <> 1
               OR NVL (ybomyest.yest_cancelstatus, 0) = 1)
          --and nvl(yevm_mergedto_yevm_id, 0)=0;
          AND yevm.yevm_id = ym.yevm_id;

CREATE OR REPLACE VIEW V8_CCM_FS_PACKAGE
AS 
SELECT ypkd.YPKD_ID YPKD_ID,
          ypkd.ypkd_ybom_id YPKD_YBOM_ID,
          ypkd.ypkd_name YPKD_NAME,
          ypkd.ypkd_noofattendees YPKD_NOOFATTENDEES,
          ypkd.ypkd_starttime YPKD_STARTTIME,
          ypkd.YPKD_ENDTIME YPKD_ENDTIME,
          NVL (ypkd.YPKD_ALTERNATIVE, 0) YPKD_ALTERNATIVE,
          (SELECT ybom.YBOM_BASE_ZCUR_ID
             FROM ybom
            WHERE ybom.ybom_id = ypkd.YPKD_YBOM_ID)
             ybom_base_zcur_id,
          (SELECT zcur.zcur_shortdesc
             FROM ybom, zcur
            WHERE     ybom.ybom_id = ypkd.YPKD_YBOM_ID
                  AND ybom.ybom_base_zcur_id = zcur.zcur_id)
             ZCUR_SHORTDESC,
          ypkd.YPKD_YPKG_ID YPKG_ID,
          (SELECT ypkg.YPKG_NAME
             FROM ypkg
            WHERE ypkg.ypkg_id = ypkd.YPKD_YPKG_ID)
             YPKG_NAME,
          ypkd_displorder YPKD_DISPLORDER,
          ybef.ybef_yevm_id YEVM_ID,
          (SELECT yevm.yevm_name
             FROM yevm
            WHERE yevm.yevm_id = ybef.ybef_yevm_id)
             yevm_name,
          (SELECT yevm.YEVM_STARTTIME
             FROM yevm
            WHERE yevm.YEVM_ID = ybef.YBEF_YEVM_ID)
             YEVM_STARTTIME,
          (SELECT yevm.YEVM_ENDTIME
             FROM yevm
            WHERE yevm.YEVM_ID = ybef.YBEF_YEVM_ID)
             YEVM_ENDTIME,
          (SELECT yevm.YEVM_NOOFATTENDEES - NVL (ym.merged_yevm_noofattendees, 0)
             FROM yevm,
              v8_ccm_yevm_mergedattendees ym
            WHERE yevm.YEVM_ID = ybef.YBEF_YEVM_ID and yevm.YEVM_ID=ym.yevm_id)
             YEVM_NOOFATTENDEES,
          ybef.YBEF_ZDCO_ID YBEF_ZDCO_ID,
          (SELECT zdco.ZDCO_LONGDESC
             FROM zdco
            WHERE zdco.zdco_id = ybef.YBEF_ZDCO_ID)
             ZDCO_LONGDESC,
          ybef.YBEF_REV_AMOUNT YBEF_REV_AMOUNT,
          ypkd.ypkd_price YPKD_PRICE
     FROM ypkd, ybef      
    WHERE ypkd.YPKD_ID = ybef.YBEF_YPKD_ID;

CREATE OR REPLACE VIEW V8_CCM_FS_MAIN
AS 
select
ybom.ybom_id YBOM_ID,
ybom.ybom_arrival YBOM_ARRIVAL,
ybom.ybom_departure YBOM_DEPARTURE,
ybom.ybom_name YBOM_NAME,
ybom.ybom_noofattendees YBOM_NOOFATTENDEES,
ybom.ybom_contractnumber YBOM_CONTRACTNUMBER,
ybom.ybom_contactonsite YBOM_CONTACTONSITE,
ybom.ybom_catdecisiondate YBOM_CATDECISIONDATE,
ybom.ybom_catfollowupdate YBOM_CATFOLLOWUPDATE,
ybom.ybom_infoboard YBOM_INFOBOARD,
ybom.ybom_cateringonly YBOM_CATERINGONLY,
ybom.YBOM_DETAILSOK YBOM_DETAILSOK,
ybom.YBOM_DISTRIBUTED YBOM_DISTRIBUTED,
ybom.YBOM_BASE_ZCUR_ID ZCUR_ID,
ybom.YBOM_YFTY_ID YFTY_ID,
ybom.YBOM_ZFAC_ID ybom_zfac_id,
decode((select nvl(zfac_zfan_id,0) from zfac where zfac_id=ybom_zfac_id),0,(select zfac.zfac_autonumber from zfac where zfac.zfac_id=ybom.ybom_zfac_id),-1,(select zfac.zfac_autonumber from zfac where zfac.zfac_id=ybom.ybom_zfac_id),(select zfan.zfan_longdesc from zfac,zfan where zfan.zfan_id=zfac.zfac_zfan_id and zfac.zfac_id=ybom.ybom_zfac_id)) ZFAC_NUMBER,
(select yfty.yfty_longdesc from yfty where ybom.ybom_yfty_id=yfty.yfty_id) YFTY_LONGDESC,
ybom.ybom_con_yest_id YBOM_CON_YEST_ID,
(select yest.yest_shortdesc from yest where yest.yest_id=ybom.ybom_con_yest_id) YBOM_CON_YEST_SHORTDESC,
(select yest.yest_longdesc from yest where yest.yest_id=ybom.ybom_con_yest_id) YBOM_CON_YEST_LONGDESC,
(select yest.yest_cancelstatus from yest where yest.yest_id=ybom.ybom_con_yest_id) YBOM_CON_YEST_CANCELSTATUS,
ybom.ybom_rms_yest_id YBOM_RMS_YEST_ID,
(select yest.yest_shortdesc from yest where yest.yest_id=ybom.ybom_rms_yest_id) YBOM_RMS_YEST_SHORTDESC,
(select yest.yest_longdesc from yest where yest.yest_id=ybom.ybom_rms_yest_id) YBOM_RMS_YEST_LONGDESC,
(select yest.yest_cancelstatus from yest where yest.yest_id=ybom.ybom_rms_yest_id) YBOM_RMS_YEST_CANCELSTATUS,
(select zcur.zcur_shortdesc from zcur where ybom.ybom_base_zcur_id=zcur.zcur_id) ZCUR_SHORTDESC,
(select zcur.zcur_longdesc from zcur where ybom.ybom_base_zcur_id=zcur.zcur_id) ZCUR_LONGDESC,
yevm.yevm_id YEVM_ID,
yevm.YEVM_ALTERNATIVE YEVM_ALTERNATIVE,
yevm.YEVM_DISPLORDER yevm_displorder,
yevm.yevm_name YEVM_NAME,
yevm.yevm_noofattendees YEVM_NOOFATTENDEES,
TO_NUMBER(TO_CHAR(TRUNC(YEVM.YEVM_STARTTIME),'J')) DATE_NUMBER,
decode(trunc(yevm.yevm_starttime),(select min(trunc(yevm_s.yevm_starttime)) from yevm yevm_s where yevm_s.yevm_ybom_id=ybom.ybom_id),1,0) FIRST_DAY_CHECK,
decode(trunc(yevm.yevm_starttime),(select max(trunc(yevm_s.yevm_starttime)) from yevm yevm_s where yevm_s.yevm_ybom_id=ybom.ybom_id),1,0) LAST_DAY_CHECK,
yevm.yevm_starttime YEVM_STARTTIME,
yevm.yevm_endtime YEVM_ENDTIME,
yevm.YEVM_DOORCARD YEVMDOORCARD,
yevm.YEVM_YETY_ID yevm_yety_id,
yevm.YEVM_YEST_ID YEVM_YEST_ID,
(select yest.yest_shortdesc from yest where yest.yest_id=yevm.yevm_yest_id) yevm_yest_shortdesc,
(select yest.yest_longdesc from yest where yest.yest_id=yevm.yevm_yest_id) yevm_yest_longdesc,
nvl((select yest.yest_cancelstatus from yest where yest.yest_id=yevm.yevm_yest_id),0) yevm_yest_cancelstatus,
(select yety.YETY_SHORTDESC from yety where yety.yety_id=yevm.yevm_yety_id) YETY_SHORTDESC,
(select yety.YETY_LONGDESC from yety where yety.yety_id=yevm.yevm_yety_id) YETY_LONGDESC,
yebs.yebs_id YEBS_ID,
yebs.YEBS_ALTERNATIVE YEBS_ALTERNATIVE,
yebs.yebs_specialrate YEBS_SPECIALRATE,
round(YEBS_SPECIALRATE*100/(100+
  (select (to_number(substr(ztcd.ZTCD_UDF,5,length(ztcd.ZTCD_UDF)-10))) from ztcd,ztxc,zdtl where ztcd.ZTCD_ZTXC_ID=ztxc.ZTXC_ID
  and ztxc.ZTXC_ID=zdtl.ZDTL_ZTXC_ID and zdtl.ZDTL_ZDCO_ID=YEBS_ZDCO_ID and zdtl.ZDTL_FROMTIME<=trunc(yebs.YEBS_STARTTIME) and zdtl.ZDTL_TOTIME>=trunc(YEBS.YEBS_ENDTIME))),2) YEBS_SPECIALRATE_NET,
decode(nvl(yebs.yebs_perhour,0), 0, yebs.yebs_specialrate, 1, (yebs_endtime-yebs_starttime)*24*yebs.yebs_specialrate) ybes_tot_rate,
round((decode(nvl(yebs.yebs_perhour,0), 0, yebs.yebs_specialrate, 1, (yebs_endtime-yebs_starttime)*24*yebs.yebs_specialrate))*100/(100+
  (select (to_number(substr(ztcd.ZTCD_UDF,5,length(ztcd.ZTCD_UDF)-10))) from ztcd,ztxc,zdtl where ztcd.ZTCD_ZTXC_ID=ztxc.ZTXC_ID
  and ztxc.ZTXC_ID=zdtl.ZDTL_ZTXC_ID and zdtl.ZDTL_ZDCO_ID=YEBS_ZDCO_ID and zdtl.ZDTL_FROMTIME<=trunc(yebs.YEBS_STARTTIME) and zdtl.ZDTL_TOTIME>=trunc(YEBS.YEBS_ENDTIME))),2) YEBS_TOT_RATE_NET,
yebs.yebs_pkgincl YEBS_PKGINCL,
yebs.YEBS_YFSC_ID YEBS_YFSC_ID,
(select yfsc.YFSC_LONGDESC from yfsc where yebs.yebs_yfsc_id=yfsc.yfsc_id) yfsc_longdesc,
(select yfsc.YFSC_GENERIC_NAME from yfsc where yebs.yebs_yfsc_id=yfsc.yfsc_id) yfsc_generic_name,
nvl(decode(yebs_yssl_id, 0, (select YFSC.YFSC_MINIMUMCAPACITY from yfsc where yfsc_id=yebs_yfsc_id), (select decode(yssl_mincapacity, null, YFSC_MINIMUMCAPACITY, yssl_mincapacity) mincapacity from yfsc, yssl where yfsc_id=yssl_yfsc_id(+) and yssl_id=yebs.yebs_yssl_id)),0) YFSC_MINIMUMCAPACITY,
nvl(decode(yebs_yssl_id, 0, (select yfsc_maximumcapacity from yfsc where yfsc_id=yebs_yfsc_id), (select decode(yssl_maxcapacity, null, yfsc_maximumcapacity, yssl_maxcapacity) maxcapacity from yfsc, yssl where yfsc_id=yssl_yfsc_id(+) and yssl_id=yebs.yebs_yssl_id)),0) YFSC_MAXIMUMCAPACITY,
nvl((select YFSC.YFSC_COMBINED from yfsc where yebs.yebs_yfsc_id=yfsc.yfsc_id),0) YFSC_COMBINED,
nvl(YEBS.YEBS_SHAREABLE,0) YEBS_SHAREABLE,
nvl(YEBS.YEBS_DONTMOVE,0) YEBS_DONTMOVE,
YEBS.YEBS_SETUPTIME,
YEBS.YEBS_SETDOWNTIME,
(select yssl.YSSL_YSSC_ID from yssl where yssl.YSSL_ID=yebs.YEBS_YSSL_ID) yssc_id,
(select yssc.yssc_longdesc from yssl,yssc where yssl.yssl_yssc_id=yssc.yssc_id and yssl.YSSL_ID=yebs.YEBS_YSSL_ID) yssc_longdesc,
(select yssc.yssc_shortdesc from yssl,yssc where yssl.yssl_yssc_id=yssc.yssc_id and yssl.YSSL_ID=yebs.YEBS_YSSL_ID) yssc_shortdesc,
(select yfsr.YFSR_YFRD_ID from yfsr where yfsr.YFSR_ID=yebs.YEBS_YFSR_ID) YFRD_ID,
(select yfrd.yfrd_longdesc from yfsr,yfrd where yfsr.yfsr_yfrd_id=yfrd.yfrd_id and yfsr.YFSR_ID=yebs.YEBS_YFSR_ID) YFRD_LONGDESC,
nvl(ybom.YBOM_IND_XCMS_ID,0) YBOM_IND_XCMS_ID,
(select v8_rep_name.Name3 from V8_rep_name where v8_rep_name.xcms_id=ybom.ybom_ind_xcms_id) IND_NAME,
(select V8_REP_FULLADDRESS.Address from V8_REP_FULLADDRESS where V8_REP_FULLADDRESS.XCMS_ID=ybom.ybom_ind_xcms_id) IND_ADDRESS,
(select xcom.XCOM_VALUE from xcom,xcmt where xcmt.xcmt_id=xcom.xcom_xcmt_id and xcom.xcom_xcms_id=ybom.ybom_ind_xcms_id and xcom_primary=1 and xcmt.xcmt_type=1 and xcom_xcmt_id=xcmt.xcmt_id and rownum=1) IND_MAIL,
(select xcom.XCOM_VALUE from xcom,xcmt where xcmt.xcmt_id=xcom.xcom_xcmt_id and xcom.xcom_xcms_id=ybom.ybom_ind_xcms_id and xcom_primary=1 and xcmt.xcmt_type=2 and xcom_xcmt_id=xcmt.xcmt_id and rownum=1) IND_WEB,
(select xcom.XCOM_VALUE from xcom,xcmt where xcmt.xcmt_id=xcom.xcom_xcmt_id and xcom.xcom_xcms_id=ybom.ybom_ind_xcms_id and xcom_primary=1 and xcmt.xcmt_type=3 and xcom_xcmt_id=xcmt.xcmt_id and rownum=1) IND_FAX,
(select xcom.XCOM_VALUE from xcom,xcmt where xcmt.xcmt_id=xcom.xcom_xcmt_id and xcom.xcom_xcms_id=ybom.ybom_ind_xcms_id and xcom_primary=1 and xcmt.xcmt_type=4 and xcom_xcmt_id=xcmt.xcmt_id and rownum=1) IND_TEL,
nvl(ybom.YBOM_XCMS_ID,0) YBOM_XCMS_ID,
(select v8_rep_name.Name3 from V8_rep_name where v8_rep_name.xcms_id=ybom.ybom_xcms_id) COMP_NAME,
(select V8_REP_FULLADDRESS.Address from V8_REP_FULLADDRESS where V8_REP_FULLADDRESS.XCMS_ID=ybom.ybom_xcms_id) COMP_ADDRESS,
(select xcom.XCOM_VALUE from xcom,xcmt where xcmt.xcmt_id=xcom.xcom_xcmt_id and xcom.xcom_xcms_id=ybom.ybom_xcms_id and xcom_primary=1 and xcmt.xcmt_type=1 and xcom_xcmt_id=xcmt.xcmt_id and rownum=1) COMP_MAIL,
(select xcom.XCOM_VALUE from xcom,xcmt where xcmt.xcmt_id=xcom.xcom_xcmt_id and xcom.xcom_xcms_id=ybom.ybom_xcms_id and xcom_primary=1 and xcmt.xcmt_type=2 and xcom_xcmt_id=xcmt.xcmt_id and rownum=1) COMP_WEB,
(select xcom.XCOM_VALUE from xcom,xcmt where xcmt.xcmt_id=xcom.xcom_xcmt_id and xcom.xcom_xcms_id=ybom.ybom_xcms_id and xcom_primary=1 and xcmt.xcmt_type=3 and xcom_xcmt_id=xcmt.xcmt_id and rownum=1) COMP_FAX,
(select xcom.XCOM_VALUE from xcom,xcmt where xcmt.xcmt_id=xcom.xcom_xcmt_id and xcom.xcom_xcms_id=ybom.ybom_xcms_id and xcom_primary=1 and xcmt.xcmt_type=4 and xcom_xcmt_id=xcmt.xcmt_id and rownum=1) COMP_TEL,
(select ybml.YBML_XCMS_ID from ybml where ybml.YBML_YBOM_ID=ybom.ybom_id and ybml.ybml_type=2 and rownum=1) CONF_XCED_ID,
(select v8_rep_name.Name3 from ybml,v8_rep_name where v8_rep_name.xcms_id=ybml.ybml_xcms_id and ybml.YBML_YBOM_ID=ybom.ybom_id and ybml.ybml_type=2 and rownum=1) CONF_XCED_NAME,
(select ybml.YBML_XCMS_ID from ybml where ybml.YBML_YBOM_ID=ybom.ybom_id and ybml.ybml_type=3 and rownum=1) RMS_XCED_ID,
(select v8_rep_name.Name3 from ybml,v8_rep_name where v8_rep_name.xcms_id=ybml.ybml_xcms_id and ybml.YBML_YBOM_ID=ybom.ybom_id and ybml.ybml_type=3 and rownum=1) RMS_XCED_NAME,
V8_FNC_CCM_NOTES(ybom.ybom_id,'YBOM',0,1) ybom_notes_all,
V8_FNC_CCM_NOTES(ybom.ybom_id,'YBOM',1,1) ybom_notes_viewable,
V8_FNC_CCM_NOTES(ybom.ybom_id,'YBOM',0,0) ybom_notes_notviewable,
V8_FNC_CCM_NOTES(yevm.yevm_id,'YEVM',0,1) yevm_notes_all,
V8_FNC_CCM_NOTES(yevm.yevm_id,'YEVM',1,1) yevm_notes_viewable,
V8_FNC_CCM_NOTES(yevm.yevm_id,'YEVM',0,0) yevm_notes_notviewable,
V8_FNC_CCM_NOTES(yebs.yebs_id,'YEBS',0,1) yebs_notes_all,
V8_FNC_CCM_NOTES(yebs.yebs_id,'YEBS',1,1) yebs_notes_viewable,
V8_FNC_CCM_NOTES(yebs.yebs_id,'YEBS',0,0) yebs_notes_notviewable,
nvl((select 1 from xcno where xcno.XCNO_TABLE_ID=ybom.ybom_id and xcno.XCNO_TABLELINKEDTO='YBOM' and rownum=1),0) ybom_notes_check,
nvl((select 1 from xcno where xcno.XCNO_TABLE_ID=yevm.yevm_id and xcno.XCNO_TABLELINKEDTO='YEVM'and rownum=1),0) yevm_notes_check,
nvl((select 1 from xcno where xcno.XCNO_TABLE_ID=yebs.yebs_id and xcno.XCNO_TABLELINKEDTO='YEBS'and rownum=1),0) yebs_notes_check,
nvl((select 1 from ycan where ycan.YCAN_YBOM_ID=ybom.ybom_id and rownum=1),0) YCAN_CHECK,
nvl((select 1 from ydep where ydep.YDEP_YBOM_ID=ybom.ybom_id and rownum=1),0) YDEP_CHECK,
nvl((select 1 from ybis where ybis.YBIS_YBOM_ID=ybom.ybom_id and rownum=1),0) YBIS_CHECK,
nvl((select 1 from v8_ccm_fs_rooms where v8_ccm_fs_rooms.YBOM_ID=ybom.ybom_id and rownum=1),0) YRES_CHECK,
nvl((select 1 from v8_ccm_fs_block_det where v8_ccm_fs_block_det.YBOM_ID=ybom.ybom_id and rownum=1),0) YBLH_CHECK,
nvl((select 1 from v8_ccm_booking_attributes v where v.ybom_id=ybom.ybom_id and rownum=1),0) BOOKING_ATTRIBUT_CHECK,
nvl((select 1 from v8_ccm_event_attributes v where v.yevm_id=yevm.yevm_id and rownum=1),0) EVENT_ATTRIBUT_CHECK,
nvl((select 1 from ypkd where trunc(yevm.YEVM_STARTTIME) between ypkd.YPKD_STARTTIME and ypkd.YPKD_ENDTIME and ypkd.ypkd_ybom_id=yevm.yevm_ybom_id and rownum=1),0) YPKD_CKECK,
nvl((select 1 from yevm yevm_s where nvl(yevm_s.yevm_doorcard,'keine')<>'keine' and trunc(yevm_s.yevm_starttime)=trunc(yevm.yevm_starttime) and yevm_s.yevm_ybom_id=yevm.yevm_ybom_id and rownum=1),0) YEVM_DOORCARD_CHECK,
nvl((select 1 from yebr,yest where YEBR.YEBR_YEVM_ID=yevm.yevm_id and yevm.yevm_ybom_id=ybom.ybom_id and yevm.yevm_yest_id=yest.yest_id and yest.yest_cancelstatus<>1 and rownum=1),0) NonFood_Check,
nvl((select 1 from yemn,yest where YEMN.YEMN_YEVM_ID=yevm.yevm_id and yevm.yevm_ybom_id=ybom.ybom_id and yevm.yevm_yest_id=yest.yest_id and yest.yest_cancelstatus<>1 and rownum=1),0) Food_Check,
nvl(YBOM.YBOM_YCAD_ID,0) YBOM_YCAD_ID, -- 11.08.2014 new field MHoffmann
nvl((select YFSC.YFSC_YCAD_ID from yfsc where YFSC.YFSC_ID=YEBS.YEBS_YFSC_ID),0) YFSC_YCAD_ID -- 11.08.2014 new field MHoffmann
from
ybom,yevm, yebs
where
ybom.ybom_id=yevm.yevm_ybom_id (+) and
yevm.yevm_id=yebs.yebs_yevm_id (+) and
nvl(yevm.yevm_mergedto_yevm_id, 0)=0;

CREATE OR REPLACE VIEW V8_CCM_REV_FC_ZPOS
AS 
select
ybom_id_s ybom_id,
TYP TYP,
TYP_TXT TYP_TXT,
ybom_arrival_s ybom_arrival,
YBOM_NOOFATTENDEES_S YBOM_NOOFATTENDEES,
zfac_id_s zfac_id,
Date_s yevm_zpos_ybss_ypos_date,
zdco_id_s zdco_id,
zdco_longdesc_s zdco_longdesc,
sum(Forecast_s) forecast,
sum(Revenue_s) revenue,
sum(revenue_pm_s) revenue_pm,
sum(TENT_NOOFROOMS_S) TENT_NOOFROOMS,
sum(DEF_NOOFROOMS_S) DEF_NOOFROOMS
from
-- CCM Forecast Revenue per Date and Departmentcode
(select
v8_ccm_forecast_revenue.yevm_ybom_id ybom_id_s,
1 TYP,
'CCM Revenue' TYP_TXT,
(select ybom.ybom_arrival from YBOM where ybom.ybom_id=V8_CCM_FORECAST_REVENUE.yevm_ybom_id) ybom_arrival_s,
(select YBOM.YBOM_NOOFATTENDEES from YBOM where ybom.ybom_id=V8_CCM_FORECAST_REVENUE.yevm_ybom_id) ybom_noofattendees_s,
(select ybom.ybom_zfac_id from ybom where ybom.ybom_id=v8_ccm_forecast_revenue.yevm_ybom_id) Zfac_id_s,
trunc((select YEVM.YEVM_STARTTIME from yevm where yevm.yevm_id=V8_CCM_FORECAST_REVENUE.YBEF_YEVM_ID)) Date_s,
v8_ccm_forecast_revenue.zdco_id zdco_id_s,
(select zdco.zdco_longdesc from zdco where zdco.zdco_id=v8_ccm_forecast_revenue.zdco_id) zdco_longdesc_s,
v8_ccm_forecast_revenue.totalforecast forecast_s,
v8_ccm_forecast_revenue.totalrevenue revenue_s,
0 revenue_pm_s,
0 TENT_NOOFROOMS_s,
0 DEF_NOOFROOMS_S
from
v8_ccm_forecast_revenue
where
nvl(v8_ccm_forecast_revenue.alternativeflag,0)=0
and
v8_ccm_forecast_revenue.yest_cancelstatus=0
union ALL
-- PM Account Revenue per Date and Departmentcode
select
(select zfac.zfac_ybom_id from zfac where zfac.zfac_id=zpos.zpos_zfac_id) YBOM_ID_s,
2 TYP,
'PM Revenue' TYP_TXT,
(select ybom.ybom_arrival from zfac,ybom where ybom.ybom_id=zfac.zfac_ybom_id and zfac.zfac_id=zpos.zpos_zfac_id) YBOM_Arrival_s,
(select YBOM.YBOM_NOOFATTENDEES from zfac,ybom where ybom.ybom_id=zfac.zfac_ybom_id and zfac.zfac_id=zpos.zpos_zfac_id) YBOM_NOOFATTENDEES_s,
zpos.zpos_zfac_id zfac_id_s,
zpos.zpos_postdate Date_s,
zpos.zpos_zdco_id zdco_id_s,
(select zdco.zdco_longdesc from zdco where zdco.zdco_id=zpos.zpos_zdco_id) zdco_longdesc_s,
0 forecast_s,
0 revenue_s,
(zpos.zpos_grossunitprice*zpos.zpos_quantity) revenue_pm_s,
0 TENT_NOOFROOMS_S,
0 DEF_NOOFROOMS_S
from
zpos
where
zpos_cdt=1
UNION ALL
select
(select YBLH.YBLH_YBOM_ID from yblh where YBLH.YBLH_ID=ybss.ybss_YBLH_ID) YBOM_ID_s,
3 TYP,
'Blockrevenue' TYP_TXT,
(select YBOM.YBOM_ARRIVAL from yblh,ybom where YBLH.YBLH_YBOM_ID=YBOM.YBOM_ID and YBLH.YBLH_ID=ybss.ybss_YBLH_ID) YBOM_ARRIVAL_s,
(select YBOM.YBOM_NOOFATTENDEES from yblh,ybom where YBLH.YBLH_YBOM_ID=YBOM.YBOM_ID and YBLH.YBLH_ID=YBSS.YBSS_YBLH_ID) YBOM_NOOFATTENDEES_s,
(select YBOM.YBOM_ZFAC_ID from ybom,yblh where YBLH.YBLH_YBOM_ID=YBOM.YBOM_ID and YBLH.YBLH_ID=ybss.ybss_YBLH_ID) ZFAC_ID_s,
ybss.ybss_DATE Date_s,
9999999999 zdco_id_s,
'Blocks' zdco_longdesc_s,
0 forecast_s,
(ybss_unitprice*decode(ybld_actualnoofrooms, 0, ybld_initialnoofrooms, ybld_actualnoofrooms)) revenue_s,
0 revenue_pm_s,
0 TENT_NOOFROOMS_S,
0 DEF_NOOFROOMS_S
from
yblh, ybss, ybst, ybld
where
yblh_id=ybss_yblh_id and 
yblh_ybst_id=ybst_id and 
nvl(ybst_cancelstatus, 0)<>1 and 
yblh_id=ybld_yblh_id and 
ybld_ycat_id=ybss_ycat_id 
and ybld_date=ybss_date
UNION ALL
select
yres_ybom_id YBOM_ID_s,
4 TYP,
'Reservationrevenue' TYP_TXT,
(select YBOM.YBOM_ARRIVAL from ybom where yres_ybom_id=ybom.ybom_id) YBOM_ARRIVAL_s,
(select YBOM.YBOM_NOOFATTENDEES from ybom where yres_ybom_id=ybom.ybom_id) YBOM_NOOFATTENDEES_s,
(select YBOM.YBOM_ZFAC_ID from ybom where yres_ybom_id=ybom.ybom_id) ZFAC_ID_s,
YPOS.YPOS_DATE Date_s,
8888888888 zdco_id_s,
'Reservations' zdco_longdesc_s,
0 forecast_s,
(ypos_unitprice*ypos_quantity) revenue_s,
0 revenue_pm_s,
0 TENT_NOOFROOMS_S,
0 DEF_NOOFROOMS_S
from
(select yres_id, yres_ybom_id,(select count(ydet_id) from ydet where ydet_yres_id=yres_id and nvl(ydet_ybld_id, 0)>0 ) linkedblocks,(select count(ydet_id) from ydet where ydet_yres_id=yres_id and nvl(ydet_ygrp_id, 0)>0 ) linkedgroups from yres    where yres_ybom_id>0 and nvl(yres_resstatus, 0)<>3   ) yresindividual, ypos 
where 
yresindividual.linkedblocks=0 and 
yresindividual.yres_id=ypos.ypos_yres_id
union all
select
ybom_id YBOM_ID_s,
5 TYP,
'Roomnights' TYP_TXT,
YBOM.YBOM_ARRIVAL YBOM_ARRIVAL_s,
YBOM.YBOM_NOOFATTENDEES YBOM_NOOFATTENDEES_S,
YBOM.YBOM_ZFAC_ID ZFAC_ID_s,
wdat.WDAT_DATE Date_s,
9999999999 zdco_id_s,
'Roomnights' zdco_longdesc_s,
0 forecast_s,
0 revenue_s,
0 revenue_pm_s,
nvl((select sum(V8_REP_YDET_INFOS.YDET_NOOFROOMS) from v8_rep_ydet_infos where V8_REP_YDET_INFOS.YRES_YBOM_ID=ybom.ybom_id and V8_REP_YDET_INFOS.YRES_RESSTATUS=0 and wdat.WDAT_DATE=V8_REP_YDET_INFOS.YDET_DATE),0)
+ nvl((select sum(nvl(V8_REP_BLOCK_DETAILED.YBLD_ACTUALNOOFROOMS,0)-(nvl(V8_REP_BLOCK_DETAILED.YDET_NOOFROOMS,0)-nvl(V8_REP_BLOCK_DETAILED.CANCELROOM,0))) from V8_rep_BLOCK_DETailed where V8_REP_BLOCK_DETAILED.YBLH_YBOM_ID=ybom.ybom_id and V8_rep_BLOCK_DETailed.ybld_date=wdat.wdat_date and V8_rep_BLOCK_DETailed.ybst_resstatus=0),0) TENT_NOOFROOMS_S,
nvl((select sum(V8_REP_YDET_INFOS.YDET_NOOFROOMS) from v8_rep_ydet_infos where V8_REP_YDET_INFOS.YRES_YBOM_ID=ybom.ybom_id and V8_REP_YDET_INFOS.YRES_RESSTATUS=1 and wdat.WDAT_DATE=V8_REP_YDET_INFOS.YDET_DATE),0)
+nvl((select sum(nvl(V8_REP_BLOCK_DETAILED.YBLD_ACTUALNOOFROOMS,0)-(nvl(V8_REP_BLOCK_DETAILED.YDET_NOOFROOMS,0)-nvl(V8_REP_BLOCK_DETAILED.CANCELROOM,0))) from V8_rep_BLOCK_DETailed where V8_REP_BLOCK_DETAILED.YBLH_YBOM_ID=ybom.ybom_id and V8_rep_BLOCK_DETailed.ybld_date=wdat.wdat_date and V8_rep_BLOCK_DETailed.ybst_resstatus=1),0) DEF_NOOFROOMS_S
from
ybom, wdat
where
wdat.wdat_date between YBOM.YBOM_ARRIVAL and YBOM.YBOM_DEPARTURE
)
group by
ybom_id_s,TYP,TYP_TXT,ybom_arrival_s,YBOM_NOOFATTENDEES_S,zfac_id_s,Date_s,zdco_id_s,zdco_longdesc_s;

CREATE OR REPLACE VIEW V8_CCM_ROOMS_BLOCK_NPU
as
SELECT                            -- Blockreservation not picked up
  1 TYP,
  V8_ED_BLOCK_DETAILS.YBLH_YBOM_ID YBOM_ID,
  V8_ED_BLOCK_DETAILS.YBLD_YBLH_ID YBLH_ID,
  V8_ED_BLOCK_DETAILS.YBLD_ID YBLD_ID,
  V8_ED_BLOCK_DETAILS.YBLD_DATE YBLD_DATE,
  NVL (DECODE (ybst_openforpickup, 1, V8_ED_BLOCK_DETAILS.AVAILABLE, V8_ED_BLOCK_DETAILS.ybld_initialnoofrooms), 0) NOOFROOMS,
  NVL (V8_ED_BLOCK_DETAILS.PICKUP, 0) NOOFROOMS_PICKUP,
  NVL (V8_ED_BLOCK_DETAILS.YBLD_NOOFADULTS, 0) ADULTNO,
  V8_ED_BLOCK_DETAILS.YBLD_YCAT_ID ROOMTYPE_ID,
  V8_ED_BLOCK_DETAILS.YCAT_LONGDESC ROOMTYPE_DESC,
  (SELECT SUM (YBSS.YBSS_QUANTITY * YBSS.YBSS_UNITPRICE) FROM YBSS WHERE YBSS.YBSS_YBLD_ID = V8_ED_BLOCK_DETAILS.YBLD_ID) Conf_rate,
  (SELECT SUM (YBSS.YBSS_QUANTITY * YBSS.YBSS_UNITPRICE) FROM YBSS WHERE     YBSS.YBSS_YBLD_ID = V8_ED_BLOCK_DETAILS.YBLD_ID AND YBSS.YBSS_dt = 1) Conf_rate_net,
  (SELECT zcur_shortdesc FROM zcur, ybom y WHERE     zcur_id = y.ybom_base_zcur_id AND y.ybom_id = V8_ED_BLOCK_DETAILS.YBLH_ybom_id) currency
FROM V8_ED_BLOCK_DETAILS
WHERE      NVL (V8_ED_BLOCK_DETAILS.ybst_cancelstatus, 0) <> 1
  AND (  (ybst_openforpickup = 1)
            AND (NVL (available, 0) <> 0) OR  (ybst_openforpickup = 0)
                    AND (NVL ( V8_ED_BLOCK_DETAILS.YBLD_INITIALNOOFROOMS, 0) <> 0));

CREATE OR REPLACE VIEW V8_CCM_ROOMS_BLOCK_PU_DET
as
SELECT                                 -- Blockreservation Pickup
  2 TYP,
  YBLH_YBOM_ID YBOM_ID,
  YBLH_ID,
  YBLD_ID,
  YDET_DATE YBLD_DATE,
  YDET_NOOFROOMS NOOFROOMS,
  NULL noofrooms_pickup,
  YDET_ADULTNO ADULTNO,
  YDET_YCAT_ID ROOMTYPE_ID,
  (SELECT ycat.YCAT_LONGDESC FROM ycat WHERE ycat.YCAT_ID = ydet.YDET_YCAT_ID) ROOMTYPE_DESC,
  getYDetDiscountedAmountForeign (ydet_rateamount, ydet_ratemanual, ydet_ratediff, ydet_ratediscountperc, ydet_nodiscountpackage, ydet_foreignrateamount, ydet_foreignratemanual, ydet_foreignratediff, ydet_foreignnodiscountpackage, ydet_finalAmount, ydet_finalForeignAmount) CONF_RATE,
  (SELECT SUM (YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID = YDET.YDET_YRES_ID AND ydet.YDET_DATE = YPOS.YPOS_DATE AND YPOS.YPOS_DT <> 2) CONF_RATE_NET,
  (SELECT zcur.zcur_shortdesc FROM zcur WHERE YRES_CONTRACT_ZCUR_ID = zcur_id) CURRENCY
FROM wdat, ydet, ybld, yblh, yres
WHERE wdat.WDAT_DATE = ydet.YDET_DATE(+)
  and ydet_ybld_id=ybld_id and ybld_yblh_id=yblh_id
  and ydet_yres_id=yres_id
  and YRES_RESSTATUS <> 3;

CREATE OR REPLACE VIEW V8_CCM_ROOMS_BLOCK_PU
as
SELECT                                 -- Blockreservation Pickup
  2 TYP,
  v.YBOM_ID,
  v.YBLH_ID,
  v.YBLD_ID,
  V.YBLD_DATE,
  SUM (v.NOOFROOMS) NOOFROOMS,
  null noofrooms_pickup,
  V.ADULTNO,
  V.ROOMTYPE_ID,
  V.ROOMTYPE_DESC,
  V.CONF_RATE,
  V.CONF_RATE_NET,
  V.CURRENCY
FROM V8_CCM_ROOMS_BLOCK_PU_DET v
GROUP BY V.YBOM_ID, v.YBLH_ID, v.YBLD_ID, V.YBLD_DATE, V.ADULTNO, V.ROOMTYPE_ID, V.ROOMTYPE_DESC, v.conf_rate, V.CONF_RATE_NET, V.currency;
                 
CREATE OR REPLACE VIEW V8_CCM_ROOMS_BLOCK
AS 
SELECT                            -- Blockreservation not picked up
  TYP,
  YBOM_ID,
  YBLH_ID,
  YBLD_ID,
  YBLD_DATE,
  NOOFROOMS,
  NOOFROOMS_PICKUP,
  ADULTNO,
  ROOMTYPE_ID,
  ROOMTYPE_DESC,
  Conf_rate,
  Conf_rate_net,
  currency
FROM V8_CCM_ROOMS_BLOCK_NPU
UNION ALL
SELECT                            -- Blockreservation picked up
  TYP,
  YBOM_ID,
  YBLH_ID,
  YBLD_ID,
  YBLD_DATE,
  NOOFROOMS,
  NOOFROOMS_PICKUP,
  ADULTNO,
  ROOMTYPE_ID,
  ROOMTYPE_DESC,
  Conf_rate,
  Conf_rate_net,
  currency
FROM V8_CCM_ROOMS_BLOCK_PU
ORDER BY YBLD_DATE;   
                    
CREATE OR REPLACE VIEW V8_CCM_ROOMS_BLOCK_PERDAY
as
select typ, ybom_id, ybld_date, conf_rate*noofrooms rate,  conf_rate_net*noofrooms ratenet, noofrooms*(conf_rate - conf_rate_net) ratetax, 0 posted 
from v8_ccm_rooms_block_npu 
union all 
select typ, ybom_id, ybld_date, conf_rate*noofrooms rate,  conf_rate_net*noofrooms ratenet, noofrooms*(conf_rate - conf_rate_net) ratetax, 0 posted 
from v8_ccm_rooms_block_pu_det
union all 
select 3 typ, shis_ybom_id, shis_wdat_date, 0 rate, 0 ratenet, 0 ratetax, shis_grossrev posted 
from shis, wgbs 
where shis_yres_id>0 and shis_wdat_date<wgbs.wgbs_datevalue and nvl(shis_yblh_id, 0)>0 and nvl(shis_yres_resstatus, 0)<>3 
union all 
select 4 typ, yres_ybom_id, zpos_postdate, 0 rate, 0 ratenet, 0 ratetax, zpos_unitprice*zpos_quantity posted 
from zpos,     (select yres_id, yres_ybom_id,     (select count(ydet_id) from ydet where ydet_yres_id=yres_id and nvl(ydet_ybld_id, 0)>0 ) linkedblocks    from yres    where yres_ybom_id>0 and nvl(yres_resstatus, 0)<>3 ) yresindividual, wgbs 
where zpos.zpos_postdate=wgbs.wgbs_datevalue and zpos.zpos_yres_id=yresindividual.yres_id and yresindividual.linkedblocks>0 and zpos.zpos_cdt<>5;

CREATE OR REPLACE VIEW V8_AR_AINV_DETAILED1
AS 
select ainv.AINV_ID,
       AINV_POSTDATE, 
       AINV_PCDATE, 
       AINV_TRANSFER_DATE, 
       AINV_CLOSEDTIME, 
       ainv_db DebitAmount, 
       ainv_cr CreditAmount, 
       ainv_db-ainv_cr OpenBalance, 
       AINV_ADJUST_AINV_ID, 
       AINV_REALPOSTDATE, 
       AINV_MANUALPOST, 
       AINV_DESCRIPT, 
       AINV_TRANSFER_INFO, 
       AACC_ID, 
       AACC_NUMBER, 
       AACC_ATYP_ID, 
       AACC_OBSOLETE, 
       (select Decode(xcms1.XCMS_TYPE, 
                      1, xcms1.XCMS_NAME1||' '||xcms1.XCMS_NAME2||' '||xcms1.XCMS_NAME3, 
                      Trim( 
                           Decode(nvl(xcms1.XCMS_NAME1,''),'','',xcms1.XCMS_NAME1||Decode(nvl(xcms1.XCMS_NAME3,''),'','',',')||' ')|| 
                           Decode(nvl(xcms1.XCMS_NAME3,''),'','',xcms1.XCMS_NAME3||' ')|| 
                           xcms1.XCMS_NAME2 
                          ) 
                     ) 
          from XCMS xcms1 
         where xcms1.XCMS_ID (+) = AACC_XCMS_ID) AccountName ,
         nvl((select count(ACPL.ACPL_ACMP_ID) from acpl where ACPL.ACPL_AINV_ID=AINV.AINV_ID),0) COUNT_ACMP,
         nvl((select max(ACPL.ACPL_ACMP_ID) from acpl where ACPL.ACPL_AINV_ID=AINV.AINV_ID),0) MAX_ACMP_ID,
                nvl((select sum(ainv_s.ainv_amount) from ainv ainv_s, acpl where ainv_s.ainv_id=acpl.acpl_ainv_id and exists (select 1 from acpl where ACPL.ACPL_AINV_ID=AINV.AINV_ID)),1) ACMP_AMOUNT,
         nvl((select sum(ainv_s.ainv_amount) from ainv ainv_s where ainv_s.ainv_adjust_ainv_id=ainv.ainv_id),0) AINV_ADJUST_AMOUNT,
          nvl( (select sum(ainv_s.ainv_db) from ainv ainv_s, acpl where ainv_s.ainv_id=acpl.acpl_ainv_id and acpl.acpl_acmp_id=NVL(
    (SELECT MAX(ACPL.ACPL_ACMP_ID) FROM acpl WHERE ACPL.ACPL_AINV_ID=AINV.AINV_ID
    ),0)),0)-
      nvl((select sum(ainv_s.ainv_cr) from ainv ainv_s, acpl where ainv_s.ainv_id=acpl.acpl_ainv_id and acpl.acpl_acmp_id=NVL(
    (SELECT MAX(ACPL.ACPL_ACMP_ID) FROM acpl WHERE ACPL.ACPL_AINV_ID=AINV.AINV_ID
    ),0)),0) TOT_BALANCE         
  from AACC,
       ainv 
 where AACC_ID (+) = AINV_AACC_ID
 and (nvl(ainv_db,0)-nvl(ainv_cr,0)<>0
      or not exists (select 1 from aipl where aipl_credit_ainv_id=ainv.ainv_id)
     );

create or replace view V8_SYS_BLOCK_SUMUP as
select
  YBLD_DATE, 
  nvl(sum(BlockTent),0) BlockTent, 
  nvl(sum(BlockDef),0) BlockDef, 
  nvl(sum(TentPickedUp),0) TentPickedUp, 
  nvl(sum(ActPickedUp),0) ActPickedUp,
  nvl(sum(YBLD_NOOFADULTS),0) NoOfAdults
  from (  
SELECT 
  YBLD_DATE, 
  decode(YBST_RESSTATUS, 0, YBLD_ACTUALNOOFROOMS, 0) BlockTent, 
  decode(YBST_RESSTATUS, 1, YBLD_ACTUALNOOFROOMS, 0) BlockDef,
  (select sum(YDET_NOOFROOMS) from ydet, yres where yres_id=ydet_yres_id and ydet_ybld_id=ybld_id and YRES_RESSTATUS=0) TentPickedUp, 
  (select sum(YDET_NOOFROOMS) from ydet, yres where yres_id=ydet_yres_id and ydet_ybld_id=ybld_id and YRES_RESSTATUS=1) ActPickedUp, 
  YBLD_NOOFADULTS 
FROM YBLD, yblh, ybst
where 
     ybld_yblh_id=yblh_id
and  yblh_ybst_id=ybst_id     
and  YBST_RESSTATUS<>3
)
group by 
  ybld_date
order by 
  ybld_date;

CREATE OR REPLACE VIEW V8_REP_XCMM_HISTFOR 
AS 
SELECT 
V8_REP_WDAT_DATE_XCMM.WDAT_DATE WDAT_DATE, 
V8_REP_WDAT_DATE_XCMM.WDAT_DATE WDAT_DATE2, 
V8_REP_WDAT_DATE_XCMM.WDAT_DATE WDAT_DATE3, 
V8_REP_WDAT_DATE_XCMM.XCMM_ID XCMM_ID, 
V8_REP_WDAT_DATE_XCMM.XCMM_SHORTDESC XCMM_SHORTDESC,
V8_REP_WDAT_DATE_XCMM.XCMM_LONGDESC XCMM_LONGDESC, 
V8_REP_WDAT_DATE_XCMM.XCMM_INACTIVE XCMM_INACTIVE, 
(DECODE(V8_REP_WDAT_DATE_XCMM.FUTURE,1, 
(NVL((SELECT V8_REP_YBLD_OCC_XCMM.DEFNOOFROOMS FROM V8_REP_YBLD_OCC_XCMM WHERE V8_REP_YBLD_OCC_XCMM.XCMA_XCMM_ID=V8_REP_WDAT_DATE_XCMM.XCMM_ID AND V8_REP_YBLD_OCC_XCMM.YBLD_DATE=V8_REP_WDAT_DATE_XCMM.WDAT_DATE),0)+NVL((SELECT V8_REP_YDET_OCC_WB_XCMM.DEFNOOFROOMS FROM V8_REP_YDET_OCC_WB_XCMM WHERE V8_REP_YDET_OCC_WB_XCMM.XCMA_XCMM_ID=V8_REP_WDAT_DATE_XCMM.XCMM_ID AND V8_REP_YDET_OCC_WB_XCMM.YDET_DATE=V8_REP_WDAT_DATE_XCMM.WDAT_DATE),0)), 
(NVL((SELECT SUM(DECODE(SHIS.SHIS_YRES_RESSTATUS,3,0,DECODE(SHIS.SHIS_DEP_ID,1,0,SHIS.SHIS_YDET_NOOFROOMS))) FROM SHIS,XCMA WHERE SHIS.SHIS_YDET_XCMA_ID=XCMA.XCMA_ID AND XCMA.XCMA_XCMM_ID=V8_REP_WDAT_DATE_XCMM.XCMM_ID AND SHIS.SHIS_WDAT_DATE=V8_REP_WDAT_DATE_XCMM.WDAT_DATE),0)))) DEF_NOOFRROMS,
(DECODE(V8_REP_WDAT_DATE_XCMM.FUTURE,1, 
(NVL((SELECT V8_REP_YBLD_OCC_XCMM.TENTNOOFROOMS FROM V8_REP_YBLD_OCC_XCMM WHERE V8_REP_YBLD_OCC_XCMM.XCMA_XCMM_ID=V8_REP_WDAT_DATE_XCMM.XCMM_ID AND V8_REP_YBLD_OCC_XCMM.YBLD_DATE=V8_REP_WDAT_DATE_XCMM.WDAT_DATE),0)+(NVL((SELECT V8_REP_YDET_OCC_WB_XCMM.TENTNOOFROOMS FROM V8_REP_YDET_OCC_WB_XCMM WHERE V8_REP_YDET_OCC_WB_XCMM.XCMA_XCMM_ID=V8_REP_WDAT_DATE_XCMM.XCMM_ID AND V8_REP_YDET_OCC_WB_XCMM.YDET_DATE=V8_REP_WDAT_DATE_XCMM.WDAT_DATE),0)-NVL((SELECT V8_REP_YDET_OCC_WB_XCMM.TENTBLOCKDEFRESROOM FROM V8_REP_YDET_OCC_WB_XCMM WHERE V8_REP_YDET_OCC_WB_XCMM.XCMA_XCMM_ID=V8_REP_WDAT_DATE_XCMM.XCMM_ID AND V8_REP_YDET_OCC_WB_XCMM.YDET_DATE=V8_REP_WDAT_DATE_XCMM.WDAT_DATE),0))), 
 0)) TENT_NOOFRROMS, 
NVL((SELECT SUM(SBUD.SBUD_VALUE) FROM SBUD,XCMA WHERE SBUD.SBUD_DATE=V8_REP_WDAT_DATE_XCMM.WDAT_DATE AND UPPER(SBUD.SBUD_TABLENAME)='XCMA' AND SBUD.SBUD_TABLE_ID=XCMA.XCMA_ID AND XCMA.XCMA_XCMM_ID=V8_REP_WDAT_DATE_XCMM.XCMM_ID AND SBUD.SBUD_TYPE=3),0) BUD_NOOFROOMS,
(DECODE(V8_REP_WDAT_DATE_XCMM.FUTURE,1, 
(NVL((SELECT V8_REP_YBLD_OCC_XCMM.DEFNOOFADULTS FROM V8_REP_YBLD_OCC_XCMM WHERE V8_REP_YBLD_OCC_XCMM.XCMA_XCMM_ID=V8_REP_WDAT_DATE_XCMM.XCMM_ID AND V8_REP_YBLD_OCC_XCMM.YBLD_DATE=V8_REP_WDAT_DATE_XCMM.WDAT_DATE),0)+NVL((SELECT V8_REP_YDET_ADULT_WB_XCMM.DEFADULTNO FROM V8_REP_YDET_ADULT_WB_XCMM WHERE V8_REP_YDET_ADULT_WB_XCMM.XCMA_XCMM_ID=V8_REP_WDAT_DATE_XCMM.XCMM_ID AND V8_REP_YDET_ADULT_WB_XCMM.YDET_DATE=V8_REP_WDAT_DATE_XCMM.WDAT_DATE),0)), 
(NVL((SELECT SUM(DECODE(SHIS.SHIS_YRES_RESSTATUS,3,0,DECODE(SHIS.SHIS_DEP_ID,1,0,SHIS.SHIS_YDET_ADULTNO))) FROM SHIS,XCMA WHERE SHIS.SHIS_YDET_XCMA_ID=XCMA.XCMA_ID AND XCMA.XCMA_XCMM_ID=V8_REP_WDAT_DATE_XCMM.XCMM_ID AND SHIS.SHIS_WDAT_DATE=V8_REP_WDAT_DATE_XCMM.WDAT_DATE),0)))) DEF_NOOFADULTS,
(DECODE(V8_REP_WDAT_DATE_XCMM.FUTURE,1, 
(NVL((SELECT V8_REP_YBLD_OCC_XCMM.TENTNOOFADULTS FROM V8_REP_YBLD_OCC_XCMM WHERE V8_REP_YBLD_OCC_XCMM.XCMA_XCMM_ID=V8_REP_WDAT_DATE_XCMM.XCMM_ID AND V8_REP_YBLD_OCC_XCMM.YBLD_DATE=V8_REP_WDAT_DATE_XCMM.WDAT_DATE),0)+NVL((SELECT V8_REP_YDET_ADULT_WB_XCMM.TENTADULTNO FROM V8_REP_YDET_ADULT_WB_XCMM WHERE V8_REP_YDET_ADULT_WB_XCMM.XCMA_XCMM_ID=V8_REP_WDAT_DATE_XCMM.XCMM_ID AND V8_REP_YDET_ADULT_WB_XCMM.YDET_DATE=V8_REP_WDAT_DATE_XCMM.WDAT_DATE),0)), 
 0)) TENT_NOOFADULTS, 
 -- MH 17.06.2014 : new field
NVL((DECODE(V8_REP_WDAT_DATE_XCMM.FUTURE,1, 
(SELECT SUM(yrgl_number*ydet_noofrooms) FROM YDET,YRGL,YRES,XCAC, XCMA WHERE YRGL.yrgl_ydet_id=YDET.ydet_id  AND YRES.yres_id=YDET.ydet_yres_id AND XCAC.xcac_id=yrgl_xcac_id AND YRES.YRES_RESSTATUS=1 AND YDET.YDET_DATE=V8_REP_WDAT_DATE_XCMM.WDAT_DATE AND YDET.YDET_XCMA_ID=XCMA.XCMA_ID and XCMA.XCMA_XCMM_ID=V8_REP_WDAT_DATE_XCMM.XCMM_ID), 
(NVL((SELECT SUM(DECODE(SHIS.SHIS_YRES_RESSTATUS,3,0,DECODE(SHIS.SHIS_DEP_ID,1,0,SHIS.SHIS_TOTALYRGL_NUMBER ))) FROM SHIS, xcma WHERE SHIS.SHIS_YDET_XCMA_ID=XCMA.XCMA_ID and XCMA.XCMA_XCMM_ID=V8_REP_WDAT_DATE_XCMM.XCMM_ID AND SHIS.SHIS_WDAT_DATE=V8_REP_WDAT_DATE_XCMM.WDAT_DATE),0)))),0) 
DEF_NOOFKIDS, 
 -- MH 17.06.2014 : new field
NVL((DECODE(V8_REP_WDAT_DATE_XCMM.FUTURE,1, 
(SELECT SUM(yrgl_number*ydet_noofrooms) FROM YDET,YRGL,YRES,XCAC, XCMA WHERE YRGL.yrgl_ydet_id=YDET.ydet_id  AND YRES.yres_id=YDET.ydet_yres_id AND XCAC.xcac_id=yrgl_xcac_id AND YRES.YRES_RESSTATUS=0 AND YDET.YDET_DATE=V8_REP_WDAT_DATE_XCMM.WDAT_DATE AND YDET.YDET_XCMA_ID=XCMA.XCMA_ID and XCMA.XCMA_XCMM_ID=V8_REP_WDAT_DATE_XCMM.XCMM_ID), 
0)),0) 
TENT_NOOFKIDS, 
NVL((SELECT SUM(SBUD.SBUD_VALUE) FROM SBUD,XCMA WHERE SBUD.SBUD_DATE=V8_REP_WDAT_DATE_XCMM.WDAT_DATE AND UPPER(SBUD.SBUD_TABLENAME)='XCMA' AND SBUD.SBUD_TABLE_ID=XCMA.XCMA_ID AND XCMA.XCMA_XCMM_ID=V8_REP_WDAT_DATE_XCMM.XCMM_ID AND SBUD.SBUD_TYPE=4),0)  BUD_NOOFADULTS,
(DECODE(V8_REP_WDAT_DATE_XCMM.FUTURE,1,
NVL(V8_REP_YPOS_FC_XCMM.DEF_REV_GROSS,0),
(NVL((SELECT SUM(SHIS.SHIS_GROSSREV) FROM SHIS,XCMA WHERE SHIS.SHIS_YDET_XCMA_ID=XCMA.XCMA_ID AND XCMA.XCMA_XCMM_ID=V8_REP_WDAT_DATE_XCMM.XCMM_ID AND SHIS.SHIS_WDAT_DATE=V8_REP_WDAT_DATE_XCMM.WDAT_DATE),0)))) DEF_REV_GROSS,
(DECODE(V8_REP_WDAT_DATE_XCMM.FUTURE,1,
NVL(V8_REP_YPOS_FC_XCMM.TENT_REV_GROSS,0),
 0)) TENT_REV_GROSS,
NVL((SELECT SUM(SBUD.SBUD_VALUE) FROM SBUD,XCMA WHERE SBUD.SBUD_DATE=V8_REP_WDAT_DATE_XCMM.WDAT_DATE AND UPPER(SBUD.SBUD_TABLENAME)='XCMA' AND SBUD.SBUD_TABLE_ID=XCMA.XCMA_ID AND XCMA.XCMA_XCMM_ID=V8_REP_WDAT_DATE_XCMM.XCMM_ID AND SBUD.SBUD_TYPE=2),0) BUD_REV_GROSS,
-- --
(DECODE(V8_REP_WDAT_DATE_XCMM.FUTURE,1,
NVL(V8_REP_YPOS_FC_XCMM.DEF_REV_NET,0),
(NVL((SELECT SUM(SHIS.SHIS_NETREV) FROM SHIS,XCMA WHERE SHIS.SHIS_YDET_XCMA_ID=XCMA.XCMA_ID AND XCMA.XCMA_XCMM_ID=V8_REP_WDAT_DATE_XCMM.XCMM_ID AND SHIS.SHIS_WDAT_DATE=V8_REP_WDAT_DATE_XCMM.WDAT_DATE),0)))) DEF_REV_NET,
(DECODE(V8_REP_WDAT_DATE_XCMM.FUTURE,1,
NVL(V8_REP_YPOS_FC_XCMM.TENT_REV_NET,0),
0)) TENT_REV_NET,
NVL((SELECT SUM(SBUD.SBUD_VALUE) FROM SBUD,XCMA WHERE SBUD.SBUD_DATE=V8_REP_WDAT_DATE_XCMM.WDAT_DATE AND UPPER(SBUD.SBUD_TABLENAME)='XCMA' AND SBUD.SBUD_TABLE_ID=XCMA.XCMA_ID AND XCMA.XCMA_XCMM_ID=V8_REP_WDAT_DATE_XCMM.XCMM_ID AND SBUD.SBUD_TYPE=1),0) BUD_REV_NET,
V8_REP_WDAT_DATE_XCMM.FUTURE FUTURE
 FROM 
V8_REP_WDAT_DATE_XCMM,V8_REP_YPOS_FC_XCMM
WHERE 
V8_REP_WDAT_DATE_XCMM.WDAT_DATE=V8_REP_YPOS_FC_XCMM.YPOS_DATE(+) AND
V8_REP_WDAT_DATE_XCMM.XCMM_ID=V8_REP_YPOS_FC_XCMM.XCMM_ID(+) 
ORDER BY V8_REP_WDAT_DATE_XCMM.WDAT_DATE;

CREATE OR REPLACE VIEW V8_REP_MAINTENANCE_INFOS
AS 
SELECT 
wact.WACT_ID WACT_ID, 
WACT.WACT_PLACE_ID,
wact.WACT_WATY_ID WACT_WATY_ID, 
waty.WATY_LONGDESC WATY_LONGDESC, 
wact.WACT_DESCRIPTION WACT_DESCRIPTION,
want.WANT_NOTES NOTES,
trunc(wact.WACT_STARTTIME) WACT_STARTTIME, 
--(SELECT wlog.WLOG_TIME FROM wlog WHERE wlog.WLOG_TYPE=163 AND wlog.WLOG_TABLE_ID=wact.WACT_ID AND UPPER(wlog.WLOG_TABLENAME)='WACT') CREATED_DATE_TIME, 
wact.WACT_STARTTIME CREATED_DATE_TIME,
--(SELECT TO_CHAR(wlog.WLOG_TIME,'HH24:MI') FROM wlog WHERE wlog.WLOG_TYPE=163 AND wlog.WLOG_TABLE_ID=wact.WACT_ID AND UPPER(wlog.WLOG_TABLENAME)='WACT') CREATED_TIME, 
TO_CHAR(wact.WACT_STARTTIME,'HH24:MI') CREATED_TIME,
wact.WACT_ENDTIME DUE_DATE,
trunc(wact.WACT_COMPLTIME) WACT_COMPLTIME, 
--(SELECT wlog.WLOG_TIME FROM wlog WHERE wlog.WLOG_TYPE=179 AND wlog.WLOG_TABLE_ID=wact.WACT_ID AND UPPER(wlog.WLOG_TABLENAME)='WACT') RESOLVED_DATE_TIME,
wact.WACT_COMPLTIME RESOLVED_DATE_TIME,
--(SELECT TO_CHAR(wlog.WLOG_TIME,'HH24:MI') FROM wlog WHERE wlog.WLOG_TYPE=179 AND wlog.WLOG_TABLE_ID=wact.WACT_ID AND UPPER(wlog.WLOG_TABLENAME)='WACT') RESOLVED_TIME,
TO_CHAR(wact.WACT_COMPLTIME,'HH24:MI') RESOLVED_TIME,
nvl2(wact.WACT_COMPLTIME,1,0) IS_RESOLVED,
nvl2(wact.WACT_COMPLTIME,(SELECT v8_rep_name.NAME2 FROM v8_rep_name, wlog WHERE v8_rep_name.XCMS_ID=wlog.WLOG_XCED_ID and wlog.WLOG_TYPE=370 AND wlog.WLOG_TABLE_ID=wact.WACT_ID AND UPPER(wlog.WLOG_TABLENAME)='WACT' and trunc(wact.WACT_COMPLTIME)=trunc(wlog.WLOG_TIME) and rownum=1),null) SET_RESOLVED_BY,
wact.WACT_PLACE_TYPE WACT_PLACE_TYPE,
DECODE(wact.WACT_PLACE_TYPE,1,'YRMS',2,'YFSC',3,'WATY',4,'YPUB') WACT_LINKED_TABLE, 
DECODE(wact.WACT_PLACE_TYPE,1,(SELECT yrms.YRMS_SHORTDESC FROM yrms WHERE yrms.YRMS_ID=wact.WACT_PLACE_ID),2,(SELECT yfsc.YFSC_LONGDESC FROM yfsc WHERE yfsc.YFSC_ID=wact.WACT_PLACE_ID),3,(SELECT waty.WATY_LONGDESC FROM waty WHERE waty.WATY_ID=wact.WACT_PLACE_ID),4,(SELECT ypub.YPUB_LONGDESC FROM ypub WHERE ypub.YPUB_ID=wact.WACT_PLACE_ID)) PLACE,
wala.WALA_XCMS_ID WALA_XCMS_ID,
NVL((SELECT v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=WALA_XCMS_ID),'Not assigned') EMP_NAME,
(SELECT xced.XCED_TITLE FROM xced WHERE xced.XCED_ID=WALA_XCMS_ID) EMP_TITLE,
(SELECT xced.XCED_WEGR_ID FROM xced WHERE xced.XCED_ID=WALA_XCMS_ID) XCED_WEGR_ID,
(SELECT wegr.WEGR_LONGDESC FROM wegr,xced WHERE wegr.WEGR_ID=xced.XCED_WEGR_ID AND xced.XCED_ID=WALA_XCMS_ID) WEGR_LONGDESC,
DECODE(wact.WACT_PLACE_TYPE,1,(SELECT yrst.YRST_ID FROM yrst WHERE wact.WACT_STARTTIME BETWEEN yrst.YRST_FROMTIME AND yrst.YRST_UNTILTIME AND yrst.YRST_YRMS_ID=wact.WACT_PLACE_ID AND ROWNUM=1),2,(SELECT yscp.YSCP_ID FROM yscp WHERE wact.WACT_STARTTIME BETWEEN TRUNC(yscp.YSCP_FROM) AND TRUNC(yscp.YSCP_TO) AND yscp.YSCP_YFSC_ID=wact.WACT_PLACE_ID AND ROWNUM=1),TO_NUMBER(NULL)) YRST_YSCP_ID,
nvl2(DECODE(wact.WACT_PLACE_TYPE,1,(SELECT yrst.YRST_ID FROM yrst WHERE wact.WACT_STARTTIME BETWEEN yrst.YRST_FROMTIME AND yrst.YRST_UNTILTIME AND yrst.YRST_YRMS_ID=wact.WACT_PLACE_ID AND ROWNUM=1),2,(SELECT yscp.YSCP_ID FROM yscp WHERE wact.WACT_STARTTIME BETWEEN TRUNC(yscp.YSCP_FROM) AND TRUNC(yscp.YSCP_TO) AND yscp.YSCP_YFSC_ID=wact.WACT_PLACE_ID AND ROWNUM=1),TO_NUMBER(NULL)),1,0) IS_BLOCKED,
DECODE(wact.WACT_PLACE_TYPE,1,(SELECT yrsc.YRSC_LONGDESC||': '||yrst.YRST_REMARK FROM yrst,yrsc WHERE yrst.YRST_YRSC_ID=yrsc.YRSC_ID AND wact.WACT_STARTTIME BETWEEN yrst.YRST_FROMTIME AND yrst.YRST_UNTILTIME AND yrst.YRST_YRMS_ID=wact.WACT_PLACE_ID AND ROWNUM=1),2,(SELECT yscp.YSCP_REASON FROM yscp WHERE wact.WACT_STARTTIME BETWEEN TRUNC(yscp.YSCP_FROM) AND TRUNC(yscp.YSCP_TO) AND yscp.YSCP_YFSC_ID=wact.WACT_PLACE_ID AND ROWNUM=1),TO_NUMBER(NULL)) BLOCKED_REASON, 
DECODE(wact.WACT_PLACE_TYPE,1,(SELECT yrst.YRST_FROMTIME FROM yrst WHERE wact.WACT_STARTTIME BETWEEN yrst.YRST_FROMTIME AND yrst.YRST_UNTILTIME AND yrst.YRST_YRMS_ID=wact.WACT_PLACE_ID AND ROWNUM=1),2,(SELECT TRUNC(yscp.YSCP_FROM) FROM yscp WHERE wact.WACT_STARTTIME BETWEEN TRUNC(yscp.YSCP_FROM) AND TRUNC(yscp.YSCP_TO) AND yscp.YSCP_YFSC_ID=wact.WACT_PLACE_ID AND ROWNUM=1),TO_NUMBER(NULL)) BLOCKED_FROM,
DECODE(wact.WACT_PLACE_TYPE,1,(SELECT yrst.YRST_UNTILTIME FROM yrst WHERE wact.WACT_STARTTIME BETWEEN yrst.YRST_FROMTIME AND yrst.YRST_UNTILTIME AND yrst.YRST_YRMS_ID=wact.WACT_PLACE_ID AND ROWNUM=1),2,(SELECT TRUNC(yscp.YSCP_TO) FROM yscp WHERE wact.WACT_STARTTIME BETWEEN TRUNC(yscp.YSCP_FROM) AND TRUNC(yscp.YSCP_TO) AND yscp.YSCP_YFSC_ID=wact.WACT_PLACE_ID AND ROWNUM=1),TO_NUMBER(NULL)) BLOCKED_UNTIL,
EXTRACT(DAY FROM(sysdate - wact.WACT_STARTTIME) DAY TO SECOND) || ' day ' || EXTRACT(HOUR FROM(sysdate - wact.WACT_STARTTIME) DAY TO SECOND) || ' hour ' || EXTRACT(MINUTE FROM(sysdate - wact.WACT_STARTTIME) DAY TO SECOND) || ' minute ' || EXTRACT(SECOND FROM(sysdate - wact.WACT_STARTTIME) DAY TO SECOND) || ' second ' Age,
EXTRACT(DAY FROM(sysdate - wact.WACT_STARTTIME) DAY TO SECOND) || ' days ' Age_D_Text,
EXTRACT(DAY FROM(sysdate - wact.WACT_STARTTIME) DAY TO SECOND) Age_D,
EXTRACT(HOUR FROM(sysdate - wact.WACT_STARTTIME) DAY TO SECOND) || ' hours ' Age_H_Text,
EXTRACT(HOUR FROM(sysdate - wact.WACT_STARTTIME) DAY TO SECOND) Age_H,
EXTRACT(MINUTE FROM(sysdate - wact.WACT_STARTTIME) DAY TO SECOND) || ' minutes ' Age_M_Text,
EXTRACT(MINUTE FROM(sysdate - wact.WACT_STARTTIME) DAY TO SECOND) Age_M,
EXTRACT(DAY FROM(sysdate - wact.WACT_STARTTIME) DAY TO SECOND) || '-' || EXTRACT(HOUR FROM(sysdate - wact.WACT_STARTTIME) DAY TO SECOND) || '-' || EXTRACT(MINUTE FROM(sysdate - wact.WACT_STARTTIME) DAY TO SECOND) Age_String,
 decode(wact_compltime, null, case when wact.WACT_ENDTIME>sysdate then (EXTRACT(DAY FROM(wact.WACT_ENDTIME-sysdate) DAY TO SECOND) || ' days ') else 'Overdue' end, null) Due_D_Text,
 decode(wact_compltime, null, case when wact.WACT_ENDTIME>sysdate then (EXTRACT(DAY FROM(wact.WACT_ENDTIME-sysdate) DAY TO SECOND) || ' day ' || EXTRACT(HOUR FROM(wact.WACT_ENDTIME-sysdate) DAY TO SECOND) || ' hour ' || EXTRACT(MINUTE FROM(wact.WACT_ENDTIME-sysdate) DAY TO SECOND) || ' minute ' || EXTRACT(SECOND FROM(wact.WACT_ENDTIME-sysdate) DAY TO SECOND) || ' second ') else 'Overdue' end, null) Due,
 decode(wact_compltime, null, case when wact.WACT_ENDTIME>sysdate then (EXTRACT(DAY FROM(wact.WACT_ENDTIME-sysdate) DAY TO SECOND) || ' d ' || EXTRACT(HOUR FROM(wact.WACT_ENDTIME-sysdate) DAY TO SECOND) || ' h ' || EXTRACT(MINUTE FROM(wact.WACT_ENDTIME-sysdate) DAY TO SECOND) || ' m ' ) else 'Overdue' end, null) DueShort, 
 decode(wact_compltime, null, case when wact.WACT_ENDTIME>sysdate then (EXTRACT(DAY FROM(wact.WACT_ENDTIME-sysdate) DAY TO SECOND)) else null end, null) Due_D,
 decode(wact_compltime, null, case when wact.WACT_ENDTIME>sysdate then (EXTRACT(HOUR FROM(wact.WACT_ENDTIME-sysdate) DAY TO SECOND) || ' hours ' ) else null end, null) Due_H_Text,
 decode(wact_compltime, null, case when wact.WACT_ENDTIME>sysdate then (EXTRACT(HOUR FROM(wact.WACT_ENDTIME-sysdate) DAY TO SECOND)) else null end, null) Due_H,
 decode(wact_compltime, null, case when wact.WACT_ENDTIME>sysdate then (EXTRACT(MINUTE FROM(wact.WACT_ENDTIME-sysdate) DAY TO SECOND) || ' minutes ') else null end, null) Due_M_Text,
 decode(wact_compltime, null, case when wact.WACT_ENDTIME>sysdate then (EXTRACT(MINUTE FROM(wact.WACT_ENDTIME-sysdate) DAY TO SECOND)) else null end, null) Due_M,
trunc((sysdate - wact.WACT_STARTTIME)*24*60) age_in_minutes
FROM wact, waty, wala, watg, want
   WHERE wact_id = wala_wact_id(+)
     AND wact_waty_id = waty_id(+)
     AND waty_watg_id = watg_id(+)
     AND wact_want_id = want_id(+)
     AND watg_role = 6;

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_INFO
AS 
SELECT
--  distinct
  ZPOS.ZPOS_POSTDATE DAT,
  ZPOS.ZPOS_CDT CDT,
  ZPOS.ZPOS_ID ZPOS_ID,
  DECODE (ZDCO.ZDCO_PAYM_TYPE,2,1,0) IS_CL,
  nvl2((to_char((SELECT trunc(YRES.YRES_EXPDEPTIME) FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),'J')),'-',NULL) MINUSI,
  (DECODE(ZPOS.ZPOS_PAIDOUT,1,-1,1)*(ZPOS.ZPOS_QUANTITY))                                                                                                                                                                                                   QTY,
  (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount)))                                       GROSS,
  (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL)))                                                                                                  PAY,
  (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount)))                                                                          REV,
  (DECODE(ZPOS.ZPOS_PAIDOUT,1,-1,1)*((DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount)))/ZPOS.ZPOS_QUANTITY))                                                                                                                         UNIT,
  (DECODE(ZPOS.ZPOS_PAIDOUT,1,-1,1)*((DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_foreignprice*ZPOS.zpos_quantity),(ZPOS.zpos_foreignprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_foreignsumupamount)))/ZPOS.ZPOS_QUANTITY))                                                                                                         FOREIGN_UNIT,
  zpos_exchangerate EXCHANGERATE, 
  (SELECT zcur_shortdesc FROM ZCUR WHERE zcur_id = NVL(zpos_zcur_id,1)) CURRENCY,      
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)                                              SPAY,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)     SPAY_CL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)<>2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)     SPAY_NOCL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)                     SREV,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) OPENA,
decode(nvl(ZORG.ZORG_ROLE,0),0, ZPOS_DESCRIPT,
                                      1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                      2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                      3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                      4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_longdesc from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       )DDESC,
  RPAD(decode(nvl(ZORG.ZORG_ROLE,0),0, ZPOS_DESCRIPT,
                                    1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_longdesc from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       ) ,30,' ')||(DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) 
                                       DDESC_LF_ORG_NAME,
  RPAD(decode(nvl(ZORG.ZORG_ROLE,0),0, ZPOS_DESCRIPT,
                                    1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                      3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_longdesc from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       ) ,30,' ')||nvl2(DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),CHR(10)||'     ' || decode(nvl((select yblp_id from yblp where yblp_zpos_id=zpos_id), 0 ), 0, DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),  getccmzposcommenttranslated(ZPOS.ZPOS_ID) ),TO_CHAR(NULL))||(DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) 
                                       DDESC_LF_COMMENT_ORG,
  RPAD(decode(nvl(ZORG.ZORG_ROLE,0),0, ZPOS_DESCRIPT,
                                    1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_longdesc from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                      ),30,' ')||nvl2(DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),CHR(10)||'     ' ||DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),TO_CHAR(NULL))||(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))) 
                                      DDESC_LF_COM_ORG_ALL,
  ZPOS.ZPOS_PHONENR                                                                   PHONE,
  (case when nvl(ZPOS.ZPOS_PHONENR,'0')<>'0' then SUBSTR(ZPOS.ZPOS_PHONENR,1,LENGTH(ZPOS.ZPOS_PHONENR)-3)||'XXX' else ZPOS.ZPOS_PHONENR END) PHONE_XXX,
  ZDCO.ZDCO_ID                                                                        ZDCO_ID,
  ZDCO.ZDCO_NUMERICDESC                                                            NDESC,
  ZPOS.ZPOS_TAXLINK_ID                                                                   TAXLINK_ID,
   decode(ZORG.ZORG_ROLE,0, ZPOS_DESCRIPT,
                                    1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                      3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_longdesc from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       )LDESC,
  (DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))) ORG_NAME,
  (DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YRES.YRES_CURRENT_YDET_ID=YDET.YDET_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(NVL((SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID),(SELECT ZFAC.ZFAC_AUTONUMBER FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))))),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))) ORG_NAME_ROOM,
  (DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YRES.YRES_CURRENT_YDET_ID=YDET.YDET_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(NVL((SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID),(SELECT ZFAC.ZFAC_AUTONUMBER FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))))),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) ORG_NAME_ROOM_IF,
  (DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YRES.YRES_CURRENT_YDET_ID=YDET.YDET_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),(NVL((SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID),(SELECT ZFAC.ZFAC_AUTONUMBER FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))) ORG_ROOM,
  DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YRES.YRES_CURRENT_YDET_ID=YDET.YDET_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),(NVL((SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID),(SELECT ZFAC.ZFAC_AUTONUMBER FROM ZFAC WHERE ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID)))))) ORG_ROOM_IF,
  TO_CHAR((CASE WHEN (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)=0 AND NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0)<>0 THEN NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0) ELSE (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 
  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) END)*100,'999999990,09') FSPAY_CL,
  ZWIN.ZWIN_ID                                                                        ZWIN_ID,
  (SELECT XCCS.XCCS_NUMBER FROM YRCL, XCCS, ZCPL WHERE YRCL.YRCL_XCCS_ID = XCCS.XCCS_ID AND YRCL.YRCL_ID = ZCPL.ZCPL_YRCL_ID AND ZCPL.ZCPL_ZPOS_ID = ZPOS.ZPOS_ID) CreditCardNumber,
  (SELECT 'XXXX XXXX XXXX '||SUBSTR(XCCS.XCCS_NUMBER,LENGTH(XCCS.XCCS_NUMBER)-3,4) FROM YRCL, XCCS, ZCPL WHERE YRCL.YRCL_XCCS_ID = XCCS.XCCS_ID AND YRCL.YRCL_ID = ZCPL.ZCPL_YRCL_ID AND ZCPL.ZCPL_ZPOS_ID = ZPOS.ZPOS_ID) HiddenCreditCardNumber,
  (SELECT YRES.YRES_CRSRESNR FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID) CRSRESNR,
  (SELECT TRUNC(YRES.YRES_EXPARRTIME) FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID) ARRIVAL,
  (SELECT trunc(YRES.YRES_EXPDEPTIME) FROM YRES WHERE YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID) DEPARTURE,
  (SELECT max(ztxc.ztxc_displorder) FROM ztxc INNER JOIN zdtl ON zdtl.zdtl_ztxc_id = ztxc.ztxc_id WHERE zdtl.zdtl_zdco_id = zpos.zpos_zdco_id AND zpos.zpos_postdate BETWEEN zdtl.zdtl_fromtime AND zdtl.zdtl_totime) taxcode,
  (case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZPOS.ZPOS_ID and Z2.ZPOS_ZTCD_ID is not null)>1 then ' ' else (select ZTXC.ZTXC_LONGDESC from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z2.ZPOS_ZTCD_ID from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZPOS.ZPOS_ID and Z2.ZPOS_ZTCD_ID is not null)) end ) TAXCODE_DESC,
  (case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZPOS.ZPOS_ID and Z2.ZPOS_ZTCD_ID is not null)>1 then 1 else 0 end ) MULTI_TAXCODE_EXISTS,
  decode(nvl(ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS_ORIGINATED_YRES_ID,ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID,
  decode(ZPOS.ZPOS_CDT,5,CHR(10)||'     ' || DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),to_char(null)) PAY_LF_COMMENT,
  decode(nvl(zpos_originated_yres_id,0),0,1,0) ZFAC_OR_YRES,
   decode(nvl(ZORG.ZORG_ROLE,0),0, ZPOS_DESCRIPT,
                                      1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                      3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_longdesc from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       )ZDCO_LONGDESC,
  RPAD(decode(nvl(ZORG.ZORG_ROLE,0),0, ZPOS_DESCRIPT,
                                      1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_longdesc from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       ) ,30,' ')||(DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) 
                                      ZDCO_LONGDESC_LF_ORG_NAME,
  RPAD(decode(nvl(ZORG.ZORG_ROLE,0),0, ZPOS_DESCRIPT,
                                      1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_longdesc from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       ) ,30,' ')||nvl2(DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),CHR(10)||'     ' || decode(nvl((select yblp_id from yblp where yblp_zpos_id=zpos_id), 0 ), 0, DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),  getccmzposcommenttranslated(ZPOS.ZPOS_ID) ),TO_CHAR(NULL))||(DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) 
                                     ZDCO_LONGDESC_LF_COMMENT_ORG,
  RPAD(decode(nvl(ZORG.ZORG_ROLE,0),0, ZPOS_DESCRIPT,
                                      1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_longdesc from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                      ),30,' ')||nvl2(DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),CHR(10)||'     ' ||DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),TO_CHAR(NULL))||(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))) 
                                     ZDCO_LONGDESC_LF_COM_ORG_ALL
FROM
  ZPOS,
  ZORG,
  ZWIN,
  ZDCO
WHERE
ZORG.ZORG_ZPOS_ID(+)=ZPOS.ZPOS_ID and
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND
  zdco_id=ZPOS.zpos_zdco_id AND
  ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ZPOS.ZPOS_HIDDEN,0)=0
  AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1
  AND
  ABS((DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount))))<>(DECODE((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_XCMS_ID=0 AND WUSS.WUSS_NAME='Show 0 Postings on Billing Screen'),1,-1,0));

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_INFO_ARR_PD
AS 
SELECT 
  ZPOS_POSTDATE_S DAT, 
  to_number(to_char(ZPOS_POSTDATE_S,'J')) DAT_J, 
  (SELECT ZPOS.ZPOS_CDT FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S) CDT, 
  ZPOS_ID_S ZPOS_ID, 
  ZDCO_ID_S ZDCO_ID, 
  REC_COUNT_S QTY, 
  GROSS_S GROSS, 
  PAY_S PAY, 
  REV_S REV, 
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY, 
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY_CL, 
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)<>2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY_NOCL, 
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SREV, 
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) OPENA, 
  TO_CHAR((CASE WHEN (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)=0 AND NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 
  AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0)<>0 THEN NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0) ELSE (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) 
  AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) END)*100,'999999990,09') FSPAY_CL, 
decode(INDICATE_ZPOS_ID,
                                   0, NVL( WTRL_TRANSLATE('ZARR',(select ZARR_ID from ZARR where ZARR_ID=(select ZPOS_ZARR_ID from ZPOS where ZPOS.ZPOS_ZARR_ID=ZARR_ID_S and ZPOS.ZPOS_ID=ZPOS_ID_S)),'ZARR_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZPOS_id_S)),
                                   1, decode(nvl((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),0),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),
                                                         1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=zarr_or_zpos_s) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=zarr_or_zpos_S)  and Zarr_or_zpos_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=zarr_or_zpos_s)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=zarr_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=zarr_or_zpos_s)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=zarr_or_zpos_s),(select yrch_foliotext from yrch where YRCH_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s)),wtrl_translate('YRCH',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s) ,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=zarr_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=zarr_or_zpos_S))),
                                                         2,decode((select ZPOS.zpos_descript from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=(select ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('ZART',(SELECT ZORG.ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=zarr_or_zpos_s) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=zarr_or_zpos_S)  and Zarr_or_zpos_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=zarr_or_zpos_s)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=zarr_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=zarr_or_zpos_s)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=zarr_or_zpos_s),(select yPAC_foliotext from YPAC where YPAC_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s)),wtrl_translate('YPAC',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s) ,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=zarr_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=zarr_or_zpos_S))),
                                                         4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('ZDCO',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))))
                                   )DDESC,   
                                   ZARR_OR_ZPOS_S ZARR_OR_ZPOS, 
  INDICATE_ZPOS_ID, 
  decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then ' ' else (wtrl_translate('ZTXC',(select ZTXC.ZTXC_ID from ZTXC, ZTCD where ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z2.ZPOS_ZTCD_ID from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)), 'ZTXC_LONGDESC', (SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))) end),(case when (select count(distinct Z3.ZPOS_ZTCD_ID) from zpos Z3 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S)>1 then ' '  else (wtrl_translate('ZTXC',(select ZTXC.ZTXC_ID from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select Z4.ZPOS_ZTCD_ID from zpos Z3, zpos Z4 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S and Z3.ZPOS_TAXLINK_ID=Z4.ZPOS_TAXLINK_ID and Z3.ZPOS_POSTDATE=zpos_postdate_s and Z3.ZPOS_ZWIN_ID=Zwin_id_s and Z4.ZPOS_ZTCD_ID is not null and rownum=1)), 'ZTXC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))) end)) TAXCODE_DESC,
  decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then 1 else 0 end ),(case when (select count( distinct Z3.ZPOS_ZTCD_ID) from zpos Z3 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S)>1 then 1 else 0 end)) MULTI_TAXCODE_EXISTS, 
  ZWIN_ID_S ZWIN_ID 
FROM 
( 
SELECT 
--  distinct 
  ntd.ZPOS_POSTDATE ZPOS_POSTDATE_S, 
  MIN(ntd.ZPOS_ID) ZPOS_ID_S, 
  MIN(ntd.ZPOS_ZDCO_ID) ZDCO_ID_S,
  NTD.ZPOS_ZARR_ID ZARR_ID_S, 
  COUNT(1) REC_COUNT_S, 
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,(-ntd.zpos_unitprice*ntd.zpos_quantity),-(ntd.zpos_unitprice*ntd.zpos_quantity)),DECODE(ntd.ZPOS_CDT,5,0,ntd.zpos_sumupamount))) GROSS_S, 
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,NULL,(ntd.zpos_unitprice*ntd.zpos_quantity)), NULL)) PAY_S, 
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1, -ntd.zpos_unitprice*ntd.zpos_quantity, NULL),DECODE(ntd.ZPOS_CDT,5,0, ntd.zpos_sumupamount))) REV_S, 
  ZWIN.ZWIN_ID ZWIN_ID_S, 
   ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0) INDICATE_ZPOS_ID, 
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID) ZARR_OR_ZPOS_S 
FROM 
  ZPOS ntd, 
  ZWIN ZWIN, 
  ZDCO,
  ZORG
WHERE 
ZORG.ZORG_ZPOS_ID(+)=NTD.ZPOS_ID AND
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND 
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND 
  zdco_id=ntd.zpos_zdco_id AND 
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND 
  NVL(ntd.ZPOS_HIDDEN,0)=0 
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1 
GROUP BY ZWIN.ZWIN_ID,  ZWIN.ZWIN_INVOICE_TO_XCMS_ID, NTD.ZPOS_ZARR_ID, ntd.ZPOS_POSTDATE, DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID), DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0)--,decode(nvl(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ID)-- ntd.ZPOS_ID--,zdco.ZDCO_ID--, ntd.ZPOS_ID, ntd.ZPOS_ZARR_ID 
) WHERE ABS(nvl(REV_S,PAY_S))<>(DECODE((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_XCMS_ID=0 AND WUSS.WUSS_NAME='Show 0 Postings on Billing Screen'),1,-1,0)) 
-- and ZWIN_ID_S=:zwin_id 
 ORDER BY CDT ASC;

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_INFO_ARR_PS
AS 
SELECT 
  ZPOS_POSTDATE_S DAT, 
  to_number(to_char(ZPOS_POSTDATE_S,'J')) DAT_J, 
  (SELECT ZPOS.ZPOS_CDT FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S) CDT, 
  ZPOS_ID_S ZPOS_ID, 
  ZDCO_ID_S ZDCO_ID, 
  REC_COUNT_S QTY, 
  GROSS_S GROSS, 
  PAY_S PAY, 
  REV_S REV, 
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY, 
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY_CL, 
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)<>2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY_NOCL, 
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SREV, 
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) OPENA, 
  TO_CHAR((CASE WHEN (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)=0 AND NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2 
  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0)<>0 THEN NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0) ELSE (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) 
  AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) END)*100,'999999990,09') FSPAY_CL, 
 decode(INDICATE_ZPOS_ID,
                                  0, NVL( WTRL_TRANSLATE('ZARR',(select ZARR_ID from ZARR where ZARR_ID=(select ZPOS_ZARR_ID from ZPOS where ZPOS.ZPOS_ZARR_ID=ZARR_or_zpos_S and ZPOS.ZPOS_ID=ZPOS_ID_S)),'ZARR_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZPOS_id_S)),
                                   1, decode(nvl((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),0),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),
                                                          1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=zarr_or_zpos_s) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=zarr_or_zpos_S)  and Zarr_or_zpos_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=zarr_or_zpos_s)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=zarr_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=zarr_or_zpos_s)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=zarr_or_zpos_s),(select yrch_foliotext from yrch where YRCH_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s)),wtrl_translate('YRCH',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s) ,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=zarr_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=zarr_or_zpos_S))),
                                                         2,decode((select ZPOS.zpos_descript from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=(select ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('ZART',(SELECT ZORG.ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                        3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=zarr_or_zpos_s) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=zarr_or_zpos_S)  and Zarr_or_zpos_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=zarr_or_zpos_s)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=zarr_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=zarr_or_zpos_s)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=zarr_or_zpos_s),(select yPAC_foliotext from YPAC where YPAC_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s)),wtrl_translate('YPAC',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=zarr_or_zpos_s) ,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=zarr_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=zarr_or_zpos_S))),
                                                         4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),
                                                         wtrl_translate('ZDCO',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZARR_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZARR_OR_ZPOS_S),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))))
                                   ) DDESC,   
 ZARR_OR_ZPOS_S ZARR_OR_ZPOS, 
 INDICATE_ZPOS_ID, 
  decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then ' ' else (wtrl_translate('ZTXC',(select ZTXC.ZTXC_ID from ZTXC, ZTCD where ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z2.ZPOS_ZTCD_ID from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)), 'ZTXC_LONGDESC', (SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))) end),(case when (select count(distinct Z3.ZPOS_ZTCD_ID) from zpos Z3 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S)>1 then ' '  else (wtrl_translate('ZTXC',(select ZTXC.ZTXC_ID from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select Z4.ZPOS_ZTCD_ID from zpos Z3, zpos Z4 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S and Z3.ZPOS_TAXLINK_ID=Z4.ZPOS_TAXLINK_ID and Z3.ZPOS_POSTDATE=zpos_postdate_s and Z3.ZPOS_ZWIN_ID=Zwin_id_s and Z4.ZPOS_ZTCD_ID is not null and rownum=1)), 'ZTXC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))) end)) TAXCODE_DESC, 
  decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then 1 else 0 end ),(case when (select count( distinct Z3.ZPOS_ZTCD_ID) from zpos Z3 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S)>1 then 1 else 0 end)) MULTI_TAXCODE_EXISTS, 
  ZWIN_ID_S ZWIN_ID 
FROM 
( 
SELECT 
--  distinct 
  MAX(ntd.ZPOS_POSTDATE) ZPOS_POSTDATE_S, 
  MIN(ntd.ZPOS_ID) ZPOS_ID_S, 
  MIN(ntd.ZPOS_ZDCO_ID) ZDCO_ID_S, 
  COUNT(1) REC_COUNT_S, 
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,(-ntd.zpos_unitprice*ntd.zpos_quantity),-(ntd.zpos_unitprice*ntd.zpos_quantity)),DECODE(ntd.ZPOS_CDT,5,0,ntd.zpos_sumupamount))) GROSS_S, 
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,NULL,(ntd.zpos_unitprice*ntd.zpos_quantity)), NULL)) PAY_S, 
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1, -ntd.zpos_unitprice*ntd.zpos_quantity, NULL),DECODE(ntd.ZPOS_CDT,5,0, ntd.zpos_sumupamount))) REV_S, 
  ZWIN.ZWIN_ID ZWIN_ID_S,
  ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S, 
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0) INDICATE_ZPOS_ID, 
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID) ZARR_OR_ZPOS_S 
FROM 
  ZPOS ntd, 
  ZWIN ZWIN, 
  ZDCO,
  ZORG
WHERE 
ZORG.ZORG_ZPOS_ID(+)=NTD.ZPOS_ID AND
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND 
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND 
  zdco_id=ntd.zpos_zdco_id AND 
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND 
  NVL(ntd.ZPOS_HIDDEN,0)=0 
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1 
   --and ZWIN_ID=:zwin_id 
GROUP BY ZWIN.ZWIN_ID,  ZWIN.ZWIN_INVOICE_TO_XCMS_ID, DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID), DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0)--,ntd.ZPOS_POSTDATE 
) WHERE ABS(nvl(REV_S,PAY_S))<>(DECODE((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_XCMS_ID=0 AND WUSS.WUSS_NAME='Show 0 Postings on Billing Screen'),1,-1,0)) 
ORDER BY ZPOS_POSTDATE_S, CDT ASC;

CREATE OR REPLACE VIEW V8_ED_FOLIO_TAX_INFO
AS 
SELECT
 ZWIN_ID_S ZWIN_ID,
 ZTCD_ID_S ZTCD_ID,
 (SELECT ZTCD.ZTCD_POSTTO_ZDCO_ID FROM ZTCD WHERE ZTCD.ZTCD_ID=ZTCD_ID_S) ZDCO_ID,
 (SELECT ZTCD.ZTCD_ZTXC_ID FROM ZTCD WHERE ZTCD.ZTCD_ID=ZTCD_ID_S) ZTCD_ZTXC_ID,
 Wtrl_Translate('ZTXC',(SELECT ZTCD.ZTCD_ZTXC_ID FROM ZTCD WHERE ZTCD.ZTCD_ID=ZTCD_ID_S),'ZTXC_LONGDESC',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_CONTACT_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))) ZTCD_NAME,
 (SELECT ZTXC.ZTXC_SHORTDESC FROM ZTXC WHERE ZTXC.ZTXC_ID=(SELECT ZTCD.ZTCD_ZTXC_ID FROM ZTCD WHERE ZTCD.ZTCD_ID=ZTCD_ID_S)) ZTXC_SHORTDESC,
 (SELECT ZTXC.ZTXC_LONGDESC FROM ZTXC WHERE ZTXC.ZTXC_ID=(SELECT ZTCD.ZTCD_ZTXC_ID FROM ZTCD WHERE ZTCD.ZTCD_ID=ZTCD_ID_S)) ZTXC_LONGDESC,
 GROSS_AMOUNT                        GROSS,
 round( GROSS_AMOUNT,2)                        GROSS2,
 NET_AMOUNT                 NET,
 ROUND(NET_AMOUNT,2)        NET2,
 TAX_AMOUNT                 TAX,
 ROUND(TAX_AMOUNT,2)        TAX2,
 MULTI_TAX_AMOUNT           MULTITAX,
 ROUND(MULTI_TAX_AMOUNT,2)  MULTITAX2,
 (SELECT ztxc.ztxc_displorder FROM ztxc WHERE ztxc.ztxc_id = (SELECT ztcd.ztcd_ztxc_id FROM ztcd WHERE ztcd.ztcd_id = ztcd_id_s)) tax_code
 FROM
(
SELECT
 ZWIN_ID_S ZWIN_ID_S,
 ZTCD_ID_S ZTCD_ID_S,
 SUM(S_GROSS) GROSS_AMOUNT,
 SUM(S_NET) NET_AMOUNT,
 SUM(S_GROSS-S_NET) TAX_AMOUNT,
 SUM(S_TAX) MULTI_TAX_AMOUNT
FROM
(
SELECT
  DISTINCT
  z.ZPOS_ZWIN_ID ZWIN_ID_S,
  z.ZPOS_ZTCD_ID ZTCD_ID_S,
  z.ZPOS_TAXLINK_ID,
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_GROSSUNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_GROSS,
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET,
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX
FROM
  ZPOS z
WHERE
  NVL(z.ZPOS_ZTCD_ID,0)<>0 AND NVL(z.ZPOS_HIDDEN,0)=0 AND NVL(z.ZPOS_NOREPRINT,0)<>1
  AND NOT EXISTS (SELECT 1 FROM ZALL WHERE zall.ZALL_ID=z.ZPOS_ZALL_ID AND NVL(zall.ZALL_INTERNALSPLIT_ZPOS_ID,0)=0)
  AND NOT EXISTS (SELECT 1 FROM ZPOS WHERE zpos.ZPOS_ID=z.ZPOS_INTERNALSPLIT_ZPOS_ID AND NVL(zpos.ZPOS_HIDDEN,0)=3)
)
GROUP BY ZWIN_ID_S,ZTCD_ID_S
)
UNION ALL
SELECT
 S_ZWIN_ID ZWIN_ID,
 TO_NUMBER(-1) ZTCD_ID,
 TO_NUMBER(-1) ZDCO_ID,
 TO_NUMBER(-1) ZTCD_ZTXC_ID,
 'Paid Out'   ZTCD_NAME,
 'P/O'   ZTXC_SHORTDESC,
 'Paid Out' ZTXC_LONGDESC,
 GROSS_AMOUNT             GROSS,
 round( GROSS_AMOUNT,2)                        GROSS2,
 NET_AMOUNT                 NET,
 ROUND(NET_AMOUNT,2)        NET2,
 TAX_AMOUNT                 TAX,
 ROUND(TAX_AMOUNT,2)        TAX2,
 0 MULTITAX,
 0 MULTITAX2,
 0 tax_code
FROM
(
SELECT
 S_ZWIN_ID S_ZWIN_ID,
 SUM(S_GROSS) GROSS_AMOUNT,
 SUM(S_NET) NET_AMOUNT,
 SUM(S_TAX) TAX_AMOUNT
FROM
(
SELECT
  z.ZPOS_ZWIN_ID S_ZWIN_ID,
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,-ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_GROSS,
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,-ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET,
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,-ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX
  FROM
  ZPOS z
WHERE
  z.ZPOS_ZDCO_ID IN (SELECT ZDCO.ZDCO_ID FROM ZDCO WHERE ZDCO.ZDCO_PAIDOUT=1) AND NVL(z.ZPOS_HIDDEN,0)=0 AND NVL(z.ZPOS_NOREPRINT,0)<>1
  AND NOT EXISTS (SELECT 1 FROM ZALL WHERE zall.ZALL_ID=z.ZPOS_ZALL_ID AND NVL(zall.ZALL_INTERNALSPLIT_ZPOS_ID,0)=0)
  AND NOT EXISTS (SELECT 1 FROM ZPOS WHERE zpos.ZPOS_ID=z.ZPOS_INTERNALSPLIT_ZPOS_ID AND NVL(zpos.ZPOS_HIDDEN,0)=3)
)
GROUP BY S_ZWIN_ID
)
UNION ALL
SELECT
 S_ZWIN_ID ZWIN_ID,
 TO_NUMBER(-2) ZTCD_ID,
 TO_NUMBER(-2) ZDCO_ID,
 TO_NUMBER(-2) ZTCD_ZTXC_ID,
 'No Tax Linked'   ZTCD_NAME,
 'No Link'   ZTXC_SHORDESC,
 'No Tax Linked' ZTXC_LONGDESC,
 SUM(GROSS_AMOUNT)          GROSS,
 round(SUM( GROSS_AMOUNT),2)                        GROSS2,
 SUM(NET_AMOUNT)              NET,
 ROUND(SUM(NET_AMOUNT),2)     NET2,
 SUM(TAX_AMOUNT)              TAX,
 ROUND(SUM(TAX_AMOUNT),2)     TAX2,
 0 MULTITAX,
 0 MULTITAX2,
 0 tax_code
FROM
(
SELECT
 S_ZWIN_ID S_ZWIN_ID,
 S_ZPOS_POSTDATE S_ZPOS_POSTDATE,
 SUM(S_GROSS) GROSS_AMOUNT,
 SUM(S_NET) NET_AMOUNT,
 SUM(S_TAX) TAX_AMOUNT
FROM
(
SELECT
  z.ZPOS_ZWIN_ID S_ZWIN_ID,
  z.ZPOS_POSTDATE S_ZPOS_POSTDATE,
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,-ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_GROSS,
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,-ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT<>2 AND ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID),0) S_NET,
  NVL((SELECT SUM(DECODE(ZPOS.ZPOS_CDT,5,(DECODE(ZPOS.ZPOS_PAIDOUT,1,-ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))),ROUND((ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY),(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))),ROUND(ZPOS.ZPOS_UNITPRICE*ZPOS.ZPOS_QUANTITY,(DECODE(NVL(NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0),4),0,4,NVL((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_NAME='RoundingValueForTaxPostings' AND WUSS.WUSS_XCMS_ID=0),0)))))) FROM ZPOS WHERE ZPOS.ZPOS_CDT=2 AND ZPOS.ZPOS_TAXLINK_ID=z.ZPOS_TAXLINK_ID  AND ZPOS.ZPOS_ZTCD_ID=z.ZPOS_ZTCD_ID GROUP BY ZPOS.ZPOS_TAXLINK_ID, ZPOS.ZPOS_ZTCD_ID),0) S_TAX
  FROM
  ZPOS z
WHERE
  NVL(z.ZPOS_HIDDEN,0)=0 AND NVL(z.ZPOS_NOREPRINT,0)<>1 AND z.ZPOS_CDT=1
  AND NOT EXISTS (SELECT 1 FROM ZDTL WHERE ZDTL.ZDTL_ZDCO_ID=z.ZPOS_ZDCO_ID AND z.ZPOS_POSTDATE BETWEEN ZDTL.ZDTL_FROMTIME AND ZDTL.ZDTL_TOTIME)
  AND NOT EXISTS (SELECT 1 FROM ZALL WHERE zall.ZALL_ID=z.ZPOS_ZALL_ID AND NVL(zall.ZALL_INTERNALSPLIT_ZPOS_ID,0)=0)
  AND NOT EXISTS (SELECT 1 FROM ZPOS WHERE zpos.ZPOS_ID=z.ZPOS_INTERNALSPLIT_ZPOS_ID AND NVL(zpos.ZPOS_HIDDEN,0)=3)
)
GROUP BY S_ZWIN_ID,S_ZPOS_POSTDATE
) GROUP BY S_ZWIN_ID order by tax_code;

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_INFO_REV_GCHK
AS 
SELECT
  ZPOS_POSTDATE_S DAT,
  (DECODE(NVL((select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where ZPOS.ZPOS_ID=ZPOS_ID_S),0),0,(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET,YRES WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_ID=NVL(YRES.YRES_CURRENT_YDET_ID,YRES.YRES_LAST_YDET_ID) AND YRES.YRES_ID=(select ZPOS.ZPOS_ORIGINATED_YRES_ID from zpos where ZPOS.ZPOS_ID=ZPOS_ID_S)),(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=(select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where ZPOS.ZPOS_ID=ZPOS_ID_S)))) ORG_ROOM,
  (SELECT ZPOS.ZPOS_CDT FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S) CDT,
  ZPOS_ID_S ZPOS_ID,
  DECODE((SELECT ZPOS.ZPOS_PAIDOUT FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),1,-1,1)*(SELECT ZPOS.ZPOS_QUANTITY FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S) QTY,
  GROSS_S GROSS,
  TAXLINK_ID_S TAXLINK_ID,
  NVL(PAY_S,0) PAY,
  NVL(REV_S,0) REV,
  TO_CHAR(NVL(PAY_S,0)*100,'999999990,09') FPAY,
  TO_CHAR(NVL(REV_S,0)*100,'999999990,09') FREV,
  NVL(REV_S,0)/(DECODE((SELECT ZPOS.ZPOS_PAIDOUT FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),1,-1,1)*(SELECT ZPOS.ZPOS_QUANTITY FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S)) UNIT,
  NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0) SPAY,
  NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0) SPAY_CL,
  NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)<>2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0) SPAY_NOCL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SREV,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) OPENA,
  TO_CHAR((CASE WHEN (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)=0 AND NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  
  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0)<>0 THEN NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0) ELSE (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0)
  AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) END)*100,'999999990,09') FSPAY_CL,
  decode(INDICATE_ZPOS_ID,
                                   0, NVL( WTRL_TRANSLATE('ZDCO',(select ZDCO_ID from ZDCO where ZDCO_ID=(select ZPOS_ZDCO_ID from ZPOS where ZPOS_ID=ZPOS_ID_S)),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZPOS_id_S)),
                                   1, decode(nvl((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),0),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S),
                                                         1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S)  and ZCHK_OR_ZPOS_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=ZCHK_OR_ZPOS_S)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=ZCHK_OR_ZPOS_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=ZCHK_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_OR_ZPOS_S),(select yrch_foliotext from yrch where YRCH_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('YRCH',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S) ,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=ZCHK_OR_ZPOS_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_OR_ZPOS_S))),
                                                        2,decode((select ZPOS.zpos_descript from ZPOS where ZPOS_ID=ZCHK_OR_ZPOS_S),(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=(select ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('ZART',(SELECT ZORG.ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S),'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZCHK_OR_ZPOS_S)),
                                                    3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZCHK_or_zpos_s) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=ZCHK_or_zpos_S)  and ZCHK_or_zpos_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=ZCHK_or_zpos_s)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=ZCHK_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=ZCHK_or_zpos_s)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_or_zpos_s),(select yPAC_foliotext from YPAC where YPAC_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s)),wtrl_translate('YPAC',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s) ,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=ZCHK_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_or_zpos_S))),
                                         4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),
                                                         wtrl_translate('ZDCO',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZCHK_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZCHK_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S))))
                                   )DDESC,                       
RPAD(
decode(INDICATE_ZPOS_ID,
                                     0, NVL( WTRL_TRANSLATE('ZDCO',(select ZDCO_ID from ZDCO where ZDCO_ID=(select ZPOS_ZDCO_ID from ZPOS where ZPOS_ID=ZPOS_ID_S)),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZPOS_id_S)),
                                     1, decode(nvl((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),0),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S),
                                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S)  and ZCHK_OR_ZPOS_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=ZCHK_OR_ZPOS_S)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=ZCHK_OR_ZPOS_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=ZCHK_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_OR_ZPOS_S),(select yrch_foliotext from yrch where YRCH_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('YRCH',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S) ,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=ZCHK_OR_ZPOS_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_OR_ZPOS_S))),
                                                         2,decode((select ZPOS.zpos_descript from ZPOS where ZPOS_ID=ZCHK_OR_ZPOS_S),(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=(select ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('ZART',(SELECT ZORG.ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S),'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZCHK_OR_ZPOS_S)),
                                                        3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZCHK_or_zpos_s) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=ZCHK_or_zpos_S)  and ZCHK_or_zpos_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=ZCHK_or_zpos_s)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=ZCHK_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=ZCHK_or_zpos_s)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_or_zpos_s),(select yPAC_foliotext from YPAC where YPAC_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s)),wtrl_translate('YPAC',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s) ,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=ZCHK_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_or_zpos_S))),
                                                         4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),
                                                         wtrl_translate('ZDCO',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZCHK_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZCHK_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S)))))
            ,30,' ')||nvl2(DECODE(NVL((select ZDCO.ZDCO_COMMENTDISABLED from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=zpos_id_s)),0),0,(select ZPOS_COMMENT from zpos where zpos_id=zpos_id_s),TO_CHAR(NULL)),CHR(10)||'     ' || decode(nvl((select yblp_id from yblp where yblp_zpos_id=zpos_id_S), 0 ), 0, DECODE(NVL((select ZDCO.ZDCO_COMMENTDISABLED from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=zpos_id_s)),0),0,(select ZPOS_COMMENT from zpos where zpos_id=zpos_id_s),TO_CHAR(NULL)),  getccmzposcommenttranslated((select ZPOS.ZPOS_ID from zpos where zpos_id=zpos_id_s)) ),TO_CHAR(NULL))||(DECODE((select ZPOS.ZPOS_XCMS_ID from zpos where zpos_id=zpos_id_s),(select ZPOS.ZPOS_ORIGINATED_XCMS_ID from zpos where zpos_id=zpos_id_s),' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=(select ZPOS.ZPOS_ORIGINATED_XCMS_ID from zpos where zpos_id=zpos_id_s))||(DECODE((DECODE(NVL((select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_id=zpos_id_s),0),0,(select ZPOS.ZPOS_ORIGINATED_YRES_ID from zpos where zpos_id=zpos_id_s),(select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_id=zpos_id_s))),(DECODE(NVL((select ZPOS.ZPOS_ZFAC_ID from zpos where zpos_id=zpos_id_s),0),0,(select ZPOS.ZPOS_YRES_ID from zpos where zpos_id=zpos_id_s),(select ZPOS.ZPOS_ZFAC_ID from zpos where zpos_id=zpos_id_s))),'',(DECODE(NVL((select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_ID=ZPOS_ID_S),0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=(select ZPOS.ZPOS_ORIGINATED_YRES_ID from zpos where zpos_id=zpos_id_s)),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=(select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_id=zpos_id_s)))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S))))) 
           DDESC_LF_ORG_NAME,
RPAD(
decode(INDICATE_ZPOS_ID,
                                  0, NVL( WTRL_TRANSLATE('ZDCO',(select ZDCO_ID from ZDCO where ZDCO_ID=(select ZPOS_ZDCO_ID from ZPOS where ZPOS_ID=ZPOS_ID_S)),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZPOS_id_S)),
                                  1, decode(nvl((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),0),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S),
                                                         1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S)  and ZCHK_OR_ZPOS_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=ZCHK_OR_ZPOS_S)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=ZCHK_OR_ZPOS_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=ZCHK_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_OR_ZPOS_S),(select yrch_foliotext from yrch where YRCH_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('YRCH',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S) ,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=ZCHK_OR_ZPOS_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_OR_ZPOS_S))),
                                                         2,decode((select ZPOS.zpos_descript from ZPOS where ZPOS_ID=ZCHK_OR_ZPOS_S),(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=(select ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('ZART',(SELECT ZORG.ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S),'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZCHK_OR_ZPOS_S)),
                                                        3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZCHK_or_zpos_s) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=ZCHK_or_zpos_S)  and ZCHK_or_zpos_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=ZCHK_or_zpos_s)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=ZCHK_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=ZCHK_or_zpos_s)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_or_zpos_s),(select yPAC_foliotext from YPAC where YPAC_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s)),wtrl_translate('YPAC',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s) ,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=ZCHK_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_or_zpos_S))),
                                                          4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),
                                                         wtrl_translate('ZDCO',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZCHK_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZCHK_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S)))))
            ,30,' ')||nvl2(DECODE(NVL((select ZDCO.ZDCO_COMMENTDISABLED from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=zpos_id_s)),0),0,(select ZPOS_COMMENT from zpos where zpos_id=zpos_id_s),TO_CHAR(NULL)),CHR(10)||'     ' || decode(nvl((select yblp_id from yblp where yblp_zpos_id=zpos_id_S), 0 ), 0, DECODE(NVL((select ZDCO.ZDCO_COMMENTDISABLED from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=zpos_id_s)),0),0,(select ZPOS_COMMENT from zpos where zpos_id=zpos_id_s),TO_CHAR(NULL)),  getccmzposcommenttranslated((select ZPOS.ZPOS_ID from zpos where zpos_id=zpos_id_s)) ),TO_CHAR(NULL))||(DECODE((select ZPOS.ZPOS_XCMS_ID from zpos where zpos_id=zpos_id_s),(select ZPOS.ZPOS_ORIGINATED_XCMS_ID from zpos where zpos_id=zpos_id_s),' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=(select ZPOS.ZPOS_ORIGINATED_XCMS_ID from zpos where zpos_id=zpos_id_s))||(DECODE((DECODE(NVL((select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_id=zpos_id_s),0),0,(select ZPOS.ZPOS_ORIGINATED_YRES_ID from zpos where zpos_id=zpos_id_s),(select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_id=zpos_id_s))),(DECODE(NVL((select ZPOS.ZPOS_ZFAC_ID from zpos where zpos_id=zpos_id_s),0),0,(select ZPOS.ZPOS_YRES_ID from zpos where zpos_id=zpos_id_s),(select ZPOS.ZPOS_ZFAC_ID from zpos where zpos_id=zpos_id_s))),'',(DECODE(NVL((select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_ID=ZPOS_ID_S),0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=(select ZPOS.ZPOS_ORIGINATED_YRES_ID from zpos where zpos_id=zpos_id_s)),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=(select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_id=zpos_id_s)))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S))))) 
          DDESC_LF_COMMENT_ORG,
RPAD(
decode(INDICATE_ZPOS_ID,
                                  0, NVL( WTRL_TRANSLATE('ZDCO',(select ZDCO_ID from ZDCO where ZDCO_ID=(select ZPOS_ZDCO_ID from ZPOS where ZPOS_ID=ZPOS_ID_S)),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZPOS_id_S)),
                                  1, decode(nvl((select ZORG.ZORG_ROLE from zorg where ZORG.ZORG_ZPOS_ID=ZPOS_ID_S),0),
                                                         0,(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S),
                                                         1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S)  and ZCHK_OR_ZPOS_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=ZCHK_OR_ZPOS_S)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=ZCHK_OR_ZPOS_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=ZCHK_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_OR_ZPOS_S),(select yrch_foliotext from yrch where YRCH_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('YRCH',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S) ,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=ZCHK_OR_ZPOS_S)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_OR_ZPOS_S))),
                                                         2,decode((select ZPOS.zpos_descript from ZPOS where ZPOS_ID=ZCHK_OR_ZPOS_S),(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=(select ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('ZART',(SELECT ZORG.ZORG_TABLE_ID from ZORG where ZORG.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S),'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZCHK_OR_ZPOS_S)),
                                                        3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZCHK_or_zpos_s) ,(select zdco_longdesc from zdco where ZDCO_ID=(select ZPOS_ZDCO_ID from zpos where ZPOS.ZPOS_ID=ZCHK_or_zpos_S)  and ZCHK_or_zpos_S=(select ZORG.ZORG_ZPOS_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s)),
                                                          wtrl_translate('ZDCO', (select ZDCO_ID from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=ZCHK_or_zpos_s)),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos.zpos_zwin_id from zpos where zpos_id=ZCHK_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos.zpos_id=ZCHK_or_zpos_s)),decode((select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_or_zpos_s),(select yPAC_foliotext from YPAC where YPAC_ID=(select zorg.zorg_table_id from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s)),wtrl_translate('YPAC',(select ZORG.ZORG_TABLE_ID from zorg where ZORG.ZORG_ZPOS_ID=ZCHK_or_zpos_s) ,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(select ZWIN.ZWIN_INVOICE_TO_XCMS_ID from zwin where zwin.zwin_id=(select zpos_zwin_id from zpos where zpos_id=ZCHK_or_zpos_s)))),(select ZPOS.ZPOS_DESCRIPT from zpos where zpos_id=ZCHK_or_zpos_S))),
                                                          4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=(select S.ZORG_TABLE_ID from ZORG s where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),0),'0',decode((select ZPOS.ZPOS_ID from ZPOS where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(SELECT S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),
                                                         wtrl_translate('ZDCO',(select S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S),'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZCHK_OR_ZPOS_S)),decode((select ZPOS.ZPOS_DESCRIPT from ZPOS where ZPOS_ID=ZCHK_OR_ZPOS_S),(select ZDCO_LONGDESC from ZDCO where ZDCO_ID=(select S.zorg_table_id from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S)),wtrl_translate('ZDCO',(SELECT S.ZORG_TABLE_ID from ZORG S where S.ZORG_ZPOS_ID=ZCHK_OR_ZPOS_S),'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=INVOICE_S)),(SELECT ZPOS.ZPOS_DESCRIPT from zpos where ZPOS.ZPOS_ID=ZCHK_OR_ZPOS_S)))))
              ,30,' ')||nvl2(DECODE(NVL((select ZDCO.ZDCO_COMMENTDISABLED from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=zpos_id_s)),0),0,(select ZPOS_COMMENT from zpos where zpos_id=zpos_id_s),TO_CHAR(NULL)),CHR(10)||'     ' ||DECODE(NVL((select ZDCO.ZDCO_COMMENTDISABLED from zdco where zdco_id=(select zpos_zdco_id from zpos where zpos_id=zpos_id_s)),0),0,(select ZPOS_COMMENT from zpos where zpos_id=zpos_id_s),TO_CHAR(NULL)),TO_CHAR(NULL))||(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=(select ZPOS.ZPOS_ORIGINATED_XCMS_ID from zpos where zpos_id=zpos_id_s))||(DECODE((DECODE(NVL((select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_id=zpos_id_s),0),0,(select ZPOS.ZPOS_ORIGINATED_YRES_ID from zpos where zpos_id=zpos_id_s),(select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_id=zpos_id_s))),(DECODE(NVL((select ZPOS.ZPOS_ZFAC_ID from zpos where zpos_id=zpos_id_s),0),0,(select ZPOS.ZPOS_YRES_ID from zpos where zpos_id=zpos_id_s),(select ZPOS.ZPOS_ZFAC_ID from zpos where zpos_id=zpos_id_s))),'',(DECODE(NVL((select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_id=zpos_id_s),0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=(select ZPOS.ZPOS_ORIGINATED_YRES_ID from zpos where zpos_id=zpos_id_s)),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=(select ZPOS.ZPOS_ORIGINATED_ZFAC_ID from zpos where zpos_id=zpos_id_s)))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S))) 
             DDESC_LF_COM_ORG_ALL,
  (SELECT ZPOS.ZPOS_PHONENR FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S) PHONE,
  (SELECT ZPOS.ZPOS_ZDCO_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S) ZDCO_ID,
  (SELECT ZDCO.ZDCO_NUMERICDESC FROM ZDCO WHERE ZDCO.ZDCO_ID=(SELECT ZPOS.ZPOS_ZDCO_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S)) NDESC,
  (DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name,ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S AND v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S))) ORG_NAME,
  (DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name,ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S AND v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL((SELECT ZPOS.ZPOS_ORIGINATED_ZFAC_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),0),0,(SELECT ZPOS.ZPOS_ORIGINATED_YRES_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),(SELECT ZPOS.ZPOS_ORIGINATED_ZFAC_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S))),(DECODE(NVL((SELECT ZPOS.ZPOS_ZFAC_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),0),0,(SELECT ZPOS.ZPOS_YRES_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),(SELECT ZPOS.ZPOS_ZFAC_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S))),'',(DECODE(NVL((SELECT ZPOS.ZPOS_ORIGINATED_ZFAC_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES,ZPOS WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ZPOS.ZPOS_ID=ZPOS_ID_S),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC,ZPOS WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID AND ZPOS.ZPOS_ID=ZPOS_ID_S))))),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S))) ORG_NAME_ROOM,
  (DECODE((SELECT ZPOS.ZPOS_XCMS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),(SELECT ZPOS.ZPOS_ORIGINATED_XCMS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),TO_CHAR(NULL),
   (DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name,ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S AND v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL((SELECT ZPOS.ZPOS_ORIGINATED_ZFAC_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),0),0,(SELECT ZPOS.ZPOS_ORIGINATED_YRES_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),(SELECT ZPOS.ZPOS_ORIGINATED_ZFAC_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S))),(DECODE(NVL((SELECT ZPOS.ZPOS_ZFAC_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),0),0,(SELECT ZPOS.ZPOS_YRES_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),(SELECT ZPOS.ZPOS_ZFAC_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S))),'',(DECODE(NVL((SELECT ZPOS.ZPOS_ORIGINATED_ZFAC_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES,ZPOS WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID AND ZPOS.ZPOS_ID=ZPOS_ID_S),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC,ZPOS WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID AND ZPOS.ZPOS_ID=ZPOS_ID_S))))),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS_ID_S))))) ORG_ROOM_IF,
  DECODE(INDICATE_ZPOS_ID,0,(DECODE(NVL((SELECT ZDCO.ZDCO_COMMENTDISABLED FROM ZDCO WHERE ZDCO.ZDCO_ID=(SELECT ZPOS.ZPOS_ZDCO_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S)),0),0,(SELECT ZPOS.ZPOS_COMMENT FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S),TO_CHAR(NULL))),TO_CHAR(NULL)) ZCHK_COMMENT,
 ZCHK_OR_ZPOS_S ZCHK_OR_ZPOS,
 INDICATE_ZPOS_ID,
 (DECODE(INDICATE_ZPOS_ID,0,ZCHK_OR_ZPOS_S,to_char(null))) GUEST_CHECK_NUM,
 --(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZPOS_ID_S and Z2.ZPOS_ZTCD_ID is not null)>1 then ' ' else (select ZTXC.ZTXC_LONGDESC from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z2.ZPOS_ZTCD_ID from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZPOS_ID_S and Z2.ZPOS_ZTCD_ID is not null)) end ) TAXCODE_DESC,
  (case when (select count (distinct Z2.ZPOS_ZTCD_ID) from zpos z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=zpos_id_s and Z2.ZPOS_ZTCD_ID is not null)>1 then ' ' else  (wtrl_translate('ZTXC',( select ZTXC.ZTXC_ID from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select Z2.ZPOS_ZTCD_ID from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZPOS_ID_S and Z2.ZPOS_ZTCD_ID is not null and rownum=1) ),'ZTXC_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))) end) TAXCODE_DESC, --changed, because of a single row, changed to rownum=1 
 (case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZPOS_ID_S and Z2.ZPOS_ZTCD_ID is not null)>1 then 1 else 0 end ) MULTI_TAXCODE_EXISTS,
 ZWIN_ID_S ZWIN_ID
FROM
(
SELECT
--  distinct
  ntd.ZPOS_POSTDATE ZPOS_POSTDATE_S,
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
  MIN(ntd.ZPOS_TAXLINK_ID) TAXLINK_ID_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,(-ntd.zpos_unitprice*ntd.zpos_quantity),-(ntd.zpos_unitprice*ntd.zpos_quantity)),DECODE(ntd.ZPOS_CDT,5,0,ntd.zpos_sumupamount))) GROSS_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,NULL,(ntd.zpos_unitprice*ntd.zpos_quantity)), NULL)) PAY_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1, -ntd.zpos_unitprice*ntd.zpos_quantity, NULL),DECODE(ntd.ZPOS_CDT,5,0, ntd.zpos_sumupamount))) REV_S,
  ZWIN.ZWIN_ID ZWIN_ID_S,
  ZWIN.ZWIN_INVOICE_TO_XCMS_ID INVOICE_S,
  DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',1,0) INDICATE_ZPOS_ID,
  DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',to_char(ntd.ZPOS_ID),ntd.ZPOS_POSCHECK_NR) ZCHK_OR_ZPOS_S
FROM
  ZPOS ntd,
  ZWIN ZWIN,
  ZDCO,
  ZORG
WHERE
--ZWIN.ZWIN_ID=:zwin_id and
ZORG.ZORG_ZPOS_ID(+)=NTD.ZPOS_ID AND
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND
  zdco_id=ntd.zpos_zdco_id AND
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ntd.ZPOS_HIDDEN,0)=0
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1
   GROUP BY ZWIN.ZWIN_ID, ZWIN.ZWIN_INVOICE_TO_XCMS_ID, ntd.ZPOS_POSTDATE, DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',to_char(ntd.ZPOS_ID),ntd.ZPOS_POSCHECK_NR), DECODE(NVL(ntd.ZPOS_POSCHECK_NR,' '),' ',1,0)
) WHERE ABS(nvl(REV_S,PAY_S))<>(DECODE((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_XCMS_ID=0 AND WUSS.WUSS_NAME='Show 0 Postings on Billing Screen'),1,-1,0))
order by CDT ASC;

CREATE OR REPLACE VIEW V8_ED_FOLIO_REV_INFO_LB
AS 
SELECT
--  distinct
  ZPOS.ZPOS_POSTDATE DAT,
  ZPOS.ZPOS_CDT CDT,
  ZPOS.ZPOS_ID ZPOS_ID,
  (DECODE(ZPOS.ZPOS_PAIDOUT,1,-1,1)*(ZPOS.ZPOS_QUANTITY))                                                                                                                                                                                                   QTY,
  (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),-(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount)))                                       GROSS,
  (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL)))                                                                                                  PAY,
  TO_CHAR((DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL)))*100,'999999990,09')                                                                      FPAY,
  (DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount)))                                                                          REV,
  TO_CHAR((DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount)))*100,'999999990,09')                                                                           FREV,
  (DECODE(ZPOS.ZPOS_PAIDOUT,1,-1,1)*((DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,(-ZPOS.zpos_unitprice*ZPOS.zpos_quantity),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),DECODE(ZPOS.ZPOS_CDT,5,0,ZPOS.zpos_sumupamount)))/ZPOS.ZPOS_QUANTITY))                                                                                                                         UNIT,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)                                              SPAY,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)     SPAY_CL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)<>2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)     SPAY_NOCL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)                     SREV,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) OPENA,
  TO_CHAR((CASE WHEN (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)=0 AND NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0)<>0 THEN NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID),0) ELSE (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 
  AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID) END)*100,'999999990,09') FSPAY_CL,
  decode(nvl(ZORG.ZORG_ROLE,0),0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                      3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_longdesc from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       )DDESC,
  RPAD(decode(nvl(ZORG.ZORG_ROLE,0),0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_longdesc from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       ) ,30,' ')||(DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) 
                                       DDESC_LF_ORG_NAME,
  RPAD(decode(nvl(ZORG.ZORG_ROLE,0),0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_longdesc from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                       ) ,30,' ')||nvl2(DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),CHR(10)||'     ' || decode(nvl((select yblp_id from yblp where yblp_zpos_id=zpos_id), 0 ), 0, DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),  getccmzposcommenttranslated(ZPOS.ZPOS_ID) ),TO_CHAR(NULL))||(DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) 
                                       DDESC_LF_COMMENT_ORG,
  RPAD(decode(nvl(ZORG.ZORG_ROLE,0),0, ZPOS_DESCRIPT,
                                       1,decode(nvl((select YRCH.YRCH_FOLIOTEXT from YRCH where YRCH.YRCH_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select yrch_foliotext from yrch where YRCH_ID=zorg_table_id),wtrl_translate('YRCH',ZORG.ZORG_TABLE_ID,'YRCH_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       2,decode(zpos_descript,(select ZART.ZART_LONGDESC from ZART where ZART.ZART_ID=ZORG_TABLE_ID),wtrl_translate('ZART',ZORG.ZORG_TABLE_ID,'ZART_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT),
                                       3,decode(nvl((select YPAC.YPAC_FOLIOTEXT from YPAC where YPAC.YPAC_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select zdco_longdesc from zdco where ZDCO_ID=ZPOS_ZDCO_ID and ZPOS_ID=ZORG.ZORG_ZPOS_ID),wtrl_translate('ZDCO', ZDCO_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select YPAC_foliotext from YPAC where YPAC_ID=zorg_table_id),wtrl_translate('YPAC',ZORG.ZORG_TABLE_ID,'YPAC_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT)),
                                       4,decode(nvl((select ZDCO.ZDCO_FOLIOTEXT from ZDCO where ZDCO.ZDCO_ID=ZORG.ZORG_TABLE_ID),0),'0',decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_LONGDESC from ZDCO where ZDCO_id=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS.ZPOS_DESCRIPT),decode(ZPOS.ZPOS_DESCRIPT,(select ZDCO_longdesc from ZDCO where ZDCO_ID=zorg_table_id),wtrl_translate('ZDCO',ZORG.ZORG_TABLE_ID,'ZDCO_FOLIOTEXT',(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),ZPOS_DESCRIPT))
                                      ),30,' ')||nvl2(DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),CHR(10)||'     ' ||DECODE(NVL(ZDCO.ZDCO_COMMENTDISABLED,0),0,ZPOS_COMMENT,TO_CHAR(NULL)),TO_CHAR(NULL))||(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,CHR(10)||(SELECT '     ->'||v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),CHR(10)||(SELECT '    ->'||ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))) 
                                      DDESC_LF_COM_ORG_ALL,
  ZPOS.ZPOS_PHONENR                                                                   PHONE,
  (case when nvl(ZPOS.ZPOS_PHONENR,'0')<>'0' then SUBSTR(ZPOS.ZPOS_PHONENR,1,LENGTH(ZPOS.ZPOS_PHONENR)-3)||'XXX' else ZPOS.ZPOS_PHONENR END) PHONE_XXX,
  ZDCO.ZDCO_ID                                                                        ZDCO_ID,
  ZDCO.ZDCO_NUMERICDESC                                                            NDESC,
  (DECODE(NVL(ZDCO.ZDCO_FOLIOTEXT,' '),' ',Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_LONGDESC',NVL(NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID)),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID))),Wtrl_Translate('ZDCO',ZDCO.ZDCO_ID,'ZDCO_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_CONTACT_ID))),/*NEW*/(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=ZWIN.ZWIN_INVOICE_TO_XCMS_ID))))) LDESC,
  (DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))) ORG_NAME,
  (DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))) ORG_NAME_ROOM,
  (DECODE(ZPOS.ZPOS_XCMS_ID,ZPOS.ZPOS_ORIGINATED_XCMS_ID,' ',(DECODE((SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID),NULL,(SELECT v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZPOS.ZPOS_ORIGINATED_XCMS_ID)||(DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,', '||(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),', '||(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))))),(SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE zpcf_zpos_id=ZPOS.ZPOS_ID))))) ORG_NAME_ROOM_IF,
  (DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID))) ORG_ROOM,
  DECODE((DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,ZPOS.ZPOS_ORIGINATED_YRES_ID,ZPOS.ZPOS_ORIGINATED_ZFAC_ID)),(DECODE(NVL(ZPOS.ZPOS_ZFAC_ID,0),0,ZPOS.ZPOS_YRES_ID,ZPOS.ZPOS_ZFAC_ID)),'',(DECODE(NVL(ZPOS.ZPOS_ORIGINATED_ZFAC_ID,0),0,(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YRES WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID AND YRES.YRES_ID=ZPOS.ZPOS_ORIGINATED_YRES_ID),(SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ZPOS.ZPOS_ORIGINATED_ZFAC_ID)))) ORG_ROOM_IF,
  ZWIN.ZWIN_ID                                                                        ZWIN_ID
FROM
  ZPOS,
  ZWIN,
  ZDCO,
  ZORG
WHERE
--ZWIN.ZWIN_ID=:ZWIN_ID and
ZORG.ZORG_ZPOS_ID(+)=ZPOS_ID AND
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND
  zdco_id=ZPOS.zpos_zdco_id AND
  ZPOS.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ZDCO.ZDCO_SUBFUNCTION,0)=0 AND
  NVL(ZPOS.ZPOS_HIDDEN,0)=0
  AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1
UNION ALL
