
            CREATE TEMP TABLE IF NOT EXISTS actual_packages_1690668003 (
        Lpu_id bigint,
        LPU int,
        ObjectID bigint,
        --ID bigint,
        GUID uuid,
        --TYPE varchar(20),
        PACKAGE_TYPE varchar(100),
        DATE timestamp,
        PACKAGE_STATUS_ID int,
        DATE_UPDATE timestamp
    );
            CREATE UNIQUE INDEX IF NOT EXISTS actual_packages_1690668003_Idx1 ON actual_packages_1690668003(PACKAGE_TYPE, ObjectID, DATE, PACKAGE_STATUS_ID);
            delete from actual_packages_1690668003
            where PACKAGE_TYPE in ('FREE_BEDS_INFORMATION');

            create temp table all_packages_1690668004 (
                Lpu_id bigint,
                LPU int,
                ObjectID bigint,
                TYPE_ID bigint,
                --TYPE varchar(20),
                PACKAGE_TYPE varchar(100),
                DATE timestamp,
                PACKAGE_STATUS_ID int
            );
            CREATE INDEX all_packages_1690668004_Idx2 ON all_packages_1690668004(PACKAGE_TYPE, ObjectID, DATE);
            CREATE INDEX all_packages_1690668004_Idx3 ON all_packages_1690668004(TYPE_ID);

            insert into all_packages_1690668004
            select
                L.Lpu_id,
                nullif(L.Lpu_f003mcod,'')::int as LPU,
                SLP.ServiceListPackage_ObjectID as ObjectID,
                SLPDT.ServiceListProcDataType_id as TYPE_ID,
                --SLPDT.ServiceListProcDataType_Name as TYPE,
                SLPT.ServiceListPackageType_Name as PACKAGE_TYPE,
                max(SLL.ServiceListLog_begDT) as DATE,
                SLP.packagestatus_id as PACKAGE_STATUS_ID
            from
                stg.v_ServiceListPackage SLP
                inner join stg.v_ServiceListLog SLL on SLL.ServiceListLog_id = SLP.ServiceListLog_id
                inner join stg.v_ServiceListPackageType SLPT on SLPT.ServiceListPackageType_id = SLP.ServiceListPackageType_id
                inner join stg.v_ServiceListProcDataType SLPDT on SLPDT.ServiceListProcDataType_id = SLP.ServiceListProcDataType_id
                inner join dbo.v_Lpu L on L.Lpu_id = SLP.Lpu_id
            where
                SLL.ServiceList_id = '25'
                and SLPT.ServiceListPackageType_Name in ('FREE_BEDS_INFORMATION')
                and not exists(
                    select ServiceListDetailLog_id
                    from stg.v_ServiceListDetailLog SLDL
                    where SLDL.ServiceListLog_id = SLP.ServiceListLog_id
                        and SLDL.ServiceListPackage_id = SLP.ServiceListPackage_id
                        and SLDL.ServiceListLogType_id = 2
                    limit 1
                )
                 and not exists(
                select 1 from stg.v_ServiceListPackage SLP2
                inner join stg.v_ServiceListDetailLog SLDL on SLDL.ServiceListPackage_id = SLP2.ServiceListPackage_id
                where SLP2.ServiceListPackage_id = SLP.ServiceListPackage_id and SLDL.ServiceListLogType_id = 2
                limit 1
            )
            group by
                L.Lpu_id,
                L.Lpu_f003mcod,
                SLP.ServiceListPackage_ObjectID,
                SLPDT.ServiceListProcDataType_id,
                --SLPDT.ServiceListProcDataType_Name,
                SLPT.ServiceListPackageType_NAME,
                SLP.packagestatus_id;

            insert into actual_packages_1690668003
            select
                PL.Lpu_id,
                PL.LPU,
                PL.ObjectID,
                --PL.ObjectID AS ID,
                OSL.Object_Guid::uuid as GUID,
                --lastUpdate.TYPE,
                PL.PACKAGE_TYPE,
                lastUpdate.DATE as DATE,
                PL.PACKAGE_STATUS_ID,
                lastDateUpdate.date as DATE_UPDATE
            from
                all_packages_1690668004 as PL
                left join lateral (
                    select DATE --, TYPE
                    from all_packages_1690668004 where PACKAGE_TYPE in ('FREE_BEDS_INFORMATION')
                    and ObjectID = PL.ObjectID and TYPE_ID in (1,3) and DATE >= PL.DATE
                    order by DATE desc
                    limit 1
                ) lastUpdate on true
                left join lateral (
                    select OSL.Object_Guid
                    from v_ObjectSynchronLog OSL
                    inner join dbo.v_ObjectSynchronLogService OSLS on OSLS.ObjectSynchronLogService_id = OSL.ObjectSynchronLogService_id
                    where OSLS.ObjectSynchronLogService_SysNick = 'TFOMSAutoInteract'
                    and OSL.Object_Name in ('FREE_BEDS_INFORMATION') and OSL.Object_id = PL.ObjectID
                    order by OSL.Object_setDT desc
                    limit 1
                ) OSL on true
                left join lateral (
                   select slp2.ServiceListPackage_updDT as date
                       from stg.v_ServiceListPackage slp2
                   where slp2.ServiceListPackage_ObjectID = PL.ObjectID
                   and slp2.PackageStatus_id = 6
                   order by slp2.ServiceListPackage_updDT DESC
                   limit 1
                ) lastDateUpdate on true
                left join lateral (
                    select DATE from all_packages_1690668004
                    where ObjectID = PL.ObjectID and PACKAGE_TYPE in ('FREE_BEDS_INFORMATION')
                    and TYPE_ID = 2 and DATE >= PL.DATE
                    limit 1
                ) p2 on true
            where
                PL.TYPE_ID = 1
                and p2.DATE is null
                ;

            DROP TABLE all_packages_1690668004;
        
