create temporary table ErrorCount
select RT.ProjectName,EHT.Description,count(EHT.Description) as NumberOfOccurances
from ProjectTable PT, RestoreTable RT, ErrorHistoryTable EHT
where PT.Active='Active'
and PT.ProjectName=RT.ProjectName
and RT.RestoreID=EHT.RestoreID
and (RT.ProcessingStatus LIKE "ReadyForDrainToDS"
or RT.ProcessingStatus LIKE 'Completed')
and RT.RestoreStatus NOT LIKE '%Errored%'
and RT.RestoreStatus NOT LIKE '%Cancelled%'
group by RT.ProjectName,EHT.Description;
create temporary table ErrorCount2
select RT.ProjectName,EHT.Description,count(EHT.Description) as NumberOfOccurances
from ProjectTable PT, RestoreTable RT, ErrorHistoryTable EHT
where PT.Active='Active'
and PT.ProjectName=RT.ProjectName
and RT.RestoreID=EHT.RestoreID
and (RT.ProcessingStatus LIKE "ReadyForDrainToDS"
or RT.ProcessingStatus LIKE 'Completed')
and RT.RestoreStatus NOT LIKE '%Errored%'
and RT.RestoreStatus NOT LIKE '%Cancelled%'
group by RT.ProjectName,EHT.Description;
select t1.ProjectName, SUBSTRING(t1.Description,1,50), t1.NumberOfOccurances
from ErrorCount t1
inner join ErrorCount2 t2
on t1.ProjectName=t2.ProjectName
and t1.NumberOfOccurances<=t2.NumberOfOccurances
group by t1.ProjectName, t1.Description, t1.NumberOfOccurances
having count(*) <= 3
order by t1.ProjectName, t1.Description, t1.NumberOfOccurances desc;
drop temporary table ErrorCount;
drop temporary table ErrorCount2;
+----------------------+----------------------------------------------------+--------------------+
| ProjectName | SUBSTRING(t1.Description,1,50) | NumberOfOccurances |
+----------------------+----------------------------------------------------+--------------------+
| A | Initialization failure | 13 |
| A | Initialization failure. | 4 |
| A | Received non-zero return code | 33 |
| B | Completed | 1615 |
| B | Mailbox(es) processing error.TAPEERROR: Processing | 252 |
| B | Received non-zero return code | 847 |
| C | Completed | 2618 |
| C | ERROR-Activation | 473 |
| C | Received non-zero return code | 2268 |
| D | Count validation error.TAPEERROR: Validate count o | 224 |
| D | Mailbox(es) processing error.TAPEERROR: Processing | 125 |
| D | Received non-zero return code | 2331 |
| E | Count validation error | 3 |
| E | Initialization failure | 7 |
| E | Received non-zero return code | 14 |
| F | Initialization failure | 7 |
| F | Received non-zero return code | 4 |
| G | Initialization failure. | 200 |
| G | Mailbox(es) processing error.TAPEERROR: Processing | 148 |
| G | Received non-zero return code | 720 |
| H | Count validation error.TAPEERROR: Validate count o | 1 |
| H | Processing stopped | 3 |
| H | Received non-zero return code | 10 |
| I | Count validation error | 1 |
| I | Initialization failure | 8 |
| I | Received non-zero return code | 9 |
| J | Initialization failure | 28 |
| J | Mailbox(es) processing error | 28 |
| J | Received non-zero return code | 59 |
+----------------------+----------------------------------------------------+--------------------+