This forum can't upload attachments so here's the SQL;
--Find all Risk valuations that are not the first or last day of the month:
SELECT * FROM valuation
WHERE 1 = 1
AND valuationtime < '01jan08'
AND (To_Char(valuationtime, 'DD') != '01'
OR To_Char((valuationtime + 1), 'DD') != '01' )
AND valuationmode IN ('Gas Valuation All Products', 'Power Valuation All Products'); --16SEP2008 C.H.
COMMIT; --16SEP2008 C.H.
--Report number of rows to be deleted in valuationdetail from above list of valuations:
SELECT Count(*) FROM valuationdetail WHERE valuation IN
(SELECT valuation FROM valuation
WHERE 1 = 1
AND valuationtime < '01jan08' -- only delete before this date
AND (To_Char(valuationtime, 'DD') != '01' -- not the first day of the month
OR To_Char((valuationtime + 1), 'DD') != '01' --not the last day of the month
)
AND valuationmode IN ('Gas Valuation All Products', 'Power Valuation All Products') --valuation mode restiction
);
COMMIT; --16SEP2008 C.H.
--Delete all Risk valuations that are not the first or last day of the month:
-- must run in order as below:
DELETE FROM valuationdetail WHERE valuation IN
(SELECT valuation FROM valuation
WHERE 1 = 1
AND valuationtime < '01jan08' -- only delete before this date
AND (To_Char(valuationtime, 'DD') != '01' -- not the first day of the month
OR To_Char((valuationtime + 1), 'DD') != '01' --not the last day of the month
)
AND valuationmode IN ('Gas Valuation All Products', 'Power Valuation All Products') --valuation mode restiction
);
COMMIT; --16SEP2008 C.H.
DELETE FROM valuation
WHERE 1 = 1
AND valuationtime < '01jan08' -- only delete before this date
AND (To_Char(valuationtime, 'DD') != '01' -- not the first day of the month
OR To_Char((valuationtime + 1), 'DD') != '01' --not the last day of the month
)
AND valuationmode IN ('Gas Valuation All Products', 'Power Valuation All Products');
COMMIT;