Hi!
I'm a VERY junior DBA running 9i.
I need to create a query that loops through a table and based on a condition returns the record. My scenario is I need take an ID and it's date (starting Jan 1 of each year) then goes back through the table again and searches the prior 30 days for other incidents of the same ID. If the count of the ID is 3 or greater I need it to return the count, and the ID. Here is the current query I'm using:
SELECT
a.SYSTEMID,
trunc(a.FAILUREENDDATETIME) as "DAYENDING",
count(b.DAILYTOTAL) as "30DAYTOTAL"
FROM
OUTAGE a,
(
SELECT
trunc(a.FAILUREENDDATE) as "DAYENDING",
a.SYSTEMID,
count(a.SYSTEM) as "DAILYTOTAL"
FROM TROUBLEINSTANCETABLE a
WHERE a.RESPONSIBLEENTITY = 'CENTERID'
AND a.SERVICEAFFECTING = 1
AND a.ACTION <> 'Upgraded'
AND a.FAILUREENDDATE > to_date('DEC-01-' || (to_char(sysdate, 'YYYY')-1), 'MON-DD-YYYY')
GROUP BY trunc(a.FAILUREENDDATE),a.SYSTEMID
) b
WHERE
a.RESPONSIBLEENTITY = 'CENTERID'
AND a.ACTION <> 'Upgraded'
AND a.SERVICEAFFECTING = 1
AND trunc(b.DAYENDING) <= trunc(a.FAILUREENDDATETIME)
AND trunc(b.DAYENDING) >= (trunc(a.FAILUREENDDATETIME) - 30)
AND a.SYSTEMID = b.SYSTEMID
GROUP BY a.SYSTEMID, trunc(a.FAILUREENDDATETIME)
OUTAGE is a restricted view of the data in TROUBLEINSTANCETABLE.
RESPONSIBLEENTITY and SYSTEMID are VARCHAR,
FAILUREENDDATETIME and FAILUREENDDATE are date/time
However, 1)I cannot get the query to return the information based on the count >=3 and 2) the count it returns is incorrect about 50% of the time and I cannot figure it out.
Could someone please help?
I'm a VERY junior DBA running 9i.
I need to create a query that loops through a table and based on a condition returns the record. My scenario is I need take an ID and it's date (starting Jan 1 of each year) then goes back through the table again and searches the prior 30 days for other incidents of the same ID. If the count of the ID is 3 or greater I need it to return the count, and the ID. Here is the current query I'm using:
SELECT
a.SYSTEMID,
trunc(a.FAILUREENDDATETIME) as "DAYENDING",
count(b.DAILYTOTAL) as "30DAYTOTAL"
FROM
OUTAGE a,
(
SELECT
trunc(a.FAILUREENDDATE) as "DAYENDING",
a.SYSTEMID,
count(a.SYSTEM) as "DAILYTOTAL"
FROM TROUBLEINSTANCETABLE a
WHERE a.RESPONSIBLEENTITY = 'CENTERID'
AND a.SERVICEAFFECTING = 1
AND a.ACTION <> 'Upgraded'
AND a.FAILUREENDDATE > to_date('DEC-01-' || (to_char(sysdate, 'YYYY')-1), 'MON-DD-YYYY')
GROUP BY trunc(a.FAILUREENDDATE),a.SYSTEMID
) b
WHERE
a.RESPONSIBLEENTITY = 'CENTERID'
AND a.ACTION <> 'Upgraded'
AND a.SERVICEAFFECTING = 1
AND trunc(b.DAYENDING) <= trunc(a.FAILUREENDDATETIME)
AND trunc(b.DAYENDING) >= (trunc(a.FAILUREENDDATETIME) - 30)
AND a.SYSTEMID = b.SYSTEMID
GROUP BY a.SYSTEMID, trunc(a.FAILUREENDDATETIME)
OUTAGE is a restricted view of the data in TROUBLEINSTANCETABLE.
RESPONSIBLEENTITY and SYSTEMID are VARCHAR,
FAILUREENDDATETIME and FAILUREENDDATE are date/time
However, 1)I cannot get the query to return the information based on the count >=3 and 2) the count it returns is incorrect about 50% of the time and I cannot figure it out.
Could someone please help?