SQL script to find and delete records that are not the
first or the last day of the month. It correctly returns
records with dates prior to 01 Jan 08. However, it is
returning first day of month and last day of month records.
(see attached)
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');
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.