Ok you asked for it!
What it is a job_table, that every time a job is ran, it either is success or fail. I need to know the max success and the max fail. Then I need other information as well. So later I try a join to gather other info, but the join fails when I try to join on the date,client,policy. If i join on client,policy it works but I get like a million results as every job comes up. All I care about is the job that is equal to the max failure and max success.
WITH temp7 AS
(
SELECT g.CLIENT, g.POLICY as policy_app_s, MAX(TRY_START_DATE) last_app_s
FROM job_detail g
WHERE summary_status IN (0,1)
and g.JOB_TYPE in (101,102,112)
GROUP BY g.CLIENT, g.POLICY
), temp8 AS
(
SELECT h.CLIENT, h.POLICY as policy_app_f, MAX(TRY_START_DATE) last_app_f
FROM job_detail h
WHERE summary_status = 2
and h.JOB_TYPE in (101,102,112)
GROUP BY h.CLIENT, h.POLICY
)
SELECT r.CLIENT,
temp7.policy_app_s,
temp7.last_app_s,
temp8.policy_app_f,
temp8.last_app_f,
r.TRY_START_DATE,
r.MASTER_NAME,
r.VENDOR_STATUS
FROM job_detail r, temp7, temp8
where temp7.client_host_name (+) = r.client_host_name and temp7.policy_app_s (+) = r.policy_name and temp7.last_app_s (+) = r.TRY_START_DATE
and temp8.client_host_name (+) = r.client_host_name and temp8.policy_app_f (+) = r.policy_name and temp8.last_app_f (+) = r.TRY_START_DATE
-- Lets report clients/policy with failtures in the last 7 days
and temp8.last_app_f >= sysdate -7
-- that don't have a successful full or incr in in less than 7 days
and temp7.last_app_s <= sysdate -7