IF (EXISTS (select Table_name from SysTable where LCase(Table_name)=LCase('v_RegBaseServicesPeriodForOazis'))) THEN Drop View v_RegBaseServicesPeriodForOazis END IF GO CREATE VIEW "DBA"."v_RegBaseServicesPeriodForOazis" as select RegDate=DateRegDt, GuestName=Guests."Name", Account=BK.Guest, RoomNo=BK.Room, Service='Нощувка и застраховка', ServicesNum=NumLodgings, ServiceSngPrice=Currencies.ExchangeRate*BK.Price, AllSum=BK.NumLodgings*ServiceSngPrice, Dogovor=Contracts.ShortName, Plateni=ServiceSngPrice*BK.PayedLodgings, DateEnd=Days(DateRegDt,NumLodgings) from "DBA".Book as BK,"dba".Guests,"DBA".Currencies,"DBA".Contracts where Guests.GuestNum=BK.Guest and Guests.Contract=Contracts.ContractNo and BK.KodCurrency=Currencies.KodCurrency and BK.ContractPays=0 and ( (RegDate>=bvrData1) and (RegDate<=bvrData2)) Union select RegDate=DateRegDt, GuestName=Guests."Name", Account=BK.Guest, RoomNo=BK.Room, Service='Нощувка и застраховка', ServicesNum=NumLodgings, ServiceSngPrice=Currencies.ExchangeRate*BK.Price, AllSum=BK.NumLodgings*CalcPriceWithDiscount(scr.Discount,ServiceSngPrice), Dogovor=Contracts.ShortName, Plateni=BK.PayedLodgings*CalcPriceWithDiscount(scr.Discount,ServiceSngPrice), DateEnd=Days(DateRegDt,NumLodgings) from "DBA".Book as BK,"dba".Guests,"DBA".Currencies,"DBA".Contracts,"dba".ServicesForContract as scr where Guests.GuestNum=BK.Guest and Guests.Contract=Contracts.ContractNo and BK.KodCurrency=Currencies.KodCurrency and BK.ContractPays<>0 and BK.ContractPays=scr.ContractNo and scr.ServiceNo in(-1,-2) and ( (RegDate>=bvrData1) and (RegDate<=bvrData2))