
CREATE TABLE IPDATA_BCKP_20251128 AS
SELECT *
FROM ipdata
WHERE nodeid IS NULL
  AND ipstatus IN ('Free', 'UsedByNetwork')
  AND VERSIONID = (SELECT VERSIONID FROM VERSION WHERE NAME = 'Actual_Network');

COMMIT; 

CREATE TABLE NODEINTERFACE_BCKP_20251128 AS
select * from nodeinterface 
where ipdataid in 
(
SELECT ipdataid
FROM ipdata
WHERE nodeid IS NULL
AND ipstatus IN ('Free', 'UsedByNetwork')
)
AND VERSIONID = (SELECT VERSIONID FROM VERSION WHERE NAME = 'Actual_Network');
 
COMMIT;
 
CREATE TABLE SUBINTERFACE_BCKP_20251128 AS
select * from subinterface 
where ipdataid in 
(
SELECT ipdataid
FROM ipdata
WHERE nodeid IS NULL
AND VERSIONID = (SELECT VERSIONID FROM VERSION WHERE NAME = 'Actual_Network')
AND ipstatus IN ('Free', 'UsedByNetwork')
);

COMMIT;

CREATE TABLE HSRP_BCKP_2051128 AS
select * from hsrp 
where hsrp.hsrpipid in 
(
SELECT ipdataid
FROM ipdata
WHERE nodeid IS NULL
  AND VERSIONID = (SELECT VERSIONID FROM VERSION WHERE NAME = 'Actual_Network')
  AND ipstatus IN ('Free', 'UsedByNetwork')
);
 
COMMIT;
 
DELETE FROM ipdata
WHERE nodeid IS NULL
  AND ipstatus IN ('Free', 'UsedByNetwork')
  AND VERSIONID = (SELECT VERSIONID FROM VERSION WHERE NAME = 'Actual_Network');


DELETE from nodeinterface 
where ipdataid in 
(
SELECT ipdataid
FROM ipdata
WHERE nodeid IS NULL
  AND VERSIONID = (SELECT VERSIONID FROM VERSION WHERE NAME = 'Actual_Network')
  AND ipstatus IN ('Free', 'UsedByNetwork')
);


DELETE from subinterface 
where ipdataid in 
(
SELECT ipdataid
FROM ipdata
WHERE nodeid IS NULL
AND VERSIONID = (SELECT VERSIONID FROM VERSION WHERE NAME = 'Actual_Network')
AND ipstatus IN ('Free', 'UsedByNetwork')
);

DELETE from hsrp 
where hsrp.hsrpipid in 
(
SELECT ipdataid
FROM ipdata
WHERE nodeid IS NULL
  AND VERSIONID = (SELECT VERSIONID FROM VERSION WHERE NAME = 'Actual_Network')
  AND ipstatus IN ('Free', 'UsedByNetwork')
);

COMMIT;