Optimize this. I placed $REGION,$SEVERITY and $ORIGINALSEVERITY as variables to reduce the repeated code:

#SET SQL FOR ENGINEER REPORT

$REGION="case
 when Region = 0 then 'Unclassified'
 when Region = 1 then 'Jamaica'
 when Region = 2 then 'Barbados'
 when Region = 3 then 'Aruba'
 when Region = 4 then 'St. Lucia'
 when Region = 5 then 'Grenada'
 when Region = 6 then 'St. Vincent'
 when Region = 7 then 'Cayman'
 when Region = 8 then 'St. Kitts & Nevis'
 when Region = 9 then 'St. Maartin'
 when Region = 10 then 'Bermuda'
 when Region = 11 then 'Antigua'
 when Region = 12 then 'Curacao'
 when Region = 13 then 'Turks and Caicos'
 when Region = 14 then 'British Virgin Is.'
 when Region = 15 then 'Dominica'
 when Region = 16 then 'Anguilla'
 when Region = 17 then 'Bonaire'
 when Region = 18 then 'Martinique'
 when Region = 19 then 'Monsterrat'
 when Region = 20 then 'Guyana'
 when Region = 21 then 'Trinidad and Tobago'
 when Region = 22 then 'Suriname'
 when Region = 23 then 'El Salvador'
 when Region = 24 then 'Panama'
 when Region = 25 then 'French Guiana'
 when Region = 26 then 'Guadelope'
 when Region = 27 then 'St. Martin and St. Barth'
 when Region = 28 then 'Haiti'
 when Region = 29 then 'Nauru'
 when Region = 30 then 'Papua New Guinea'
 when Region = 31 then 'Samoa'
 when Region = 32 then 'Tonga'
 when Region = 33 then 'Vanuatu'
 when Region = 34 then 'Figi'
 when Region = 35 then 'Miami'
 when Region = 36 then 'BVI'
 when Region = 37 then 'FWI\_GUA'
 when Region = 38 then 'FWI\_FRA'
 when Region = 39 then 'FWI\_FGU'
 when Region = 40 then 'FWI\_MTQ'
 when Region = 41 then 'New York'
 when Region = 42 then 'NOECS' ";

$SEVERITY ="case
 when Severity = 0 then 'Clear'
 when Severity = 2 then 'Info'
 when Severity = 1 then 'Inteterminate'
 when Severity = 3 then 'Minor'
 when Severity = 4 then 'Major'
 when Severity = 5 then 'Critical' ";

$ORIGINALSEVERITY ="case
 when OriginalSeverity = 0 then 'Clear'
 when OriginalSeverity = 2 then 'Info'
 when OriginalSeverity = 1 then 'Inteterminate'
 when OriginalSeverity = 3 then 'Minor'
 when OriginalSeverity = 4 then 'Major'
 when OriginalSeverity = 5 then 'Critical'";

if ($rep == 'engineer\_option') 
{
$engineer\_sql = "select 
$SEVERITY end as Severity ,
$ORIGINALSEVERITY end as OriginalSeverity ,
$REGION end as Region,
Table1.AlarmId,Acktime,fullname,
Table1.Node,
Table1.SiteName,
Table1.SiteCode,
from\_unixtime(Table1.FirstReported) as FirstReported,
Table1.TicketID,
Table1.Summary,
Table1.AdditionalSummary, 
Table1.ClearedTime,
case 
when ClearedTime > 0 then 
time\_format(sec\_to\_time(timestampdiff(second,from\_unixtime(Table1.FirstReported), Table1.ClearedTime)),'%Hh %im %ss')
when ClearedTime is null then time\_format(sec\_to\_time(timestampdiff(second,from\_unixtime(Table1.FirstReported), now())),'%Hh %im %ss') 
end as Duration,
 case when 
 ClearedTime > 0 then
timestampdiff(second,from\_unixtime(Table1.FirstReported), Table1.ClearedTime)/60
 when ClearedTime is null then 
timestampdiff(second,from\_unixtime(Table1.FirstReported), now())/60
 end as DDuration, Class
from 
$table Table1 
inner join 
(select AlarmId, MAX(StateChange) StateChange 
from $table 
where (FirstReported > ".$timestamp1." and FirstReported < ".$timestamp2." and OriginalSeverity > 1 )
GROUP BY AlarmId ) Table2 
ON Table1.AlarmId = Table2.AlarmId 
AND Table1.StateChange=Table2.StateChange ".$filtersql\_sev. 
" inner join Monolith.AuthUsers table3 on OwnerID = table3.userid and UserName = '$engineeid' Order by Table1.FirstReported desc;";
}
이를 최적화합니다. 반복되는 코드를 줄이기 위해 $REGION, $SEVERITY 및 $ORIGINALSEVERITY를 변수로 배치했습니다:

#엔지니어 보고서용 SQL 설정

REGION="case
 Region = 0이면 '분류되지 않음'
 when Region = 1이면 '자메이카'
 when Region = 2이면 '바베이도스'
 when Region = 3이면 '아루바'
 지역 = 4이면 '세인트루시아'
 지역 = 5일 때 '그레나다'
 지역 = 6이면 '세인트빈센트'
 지역 = 7이면 '케이맨'
 지역 = 8이면 '세인트키츠네비스'
 지역 = 9일 때 '세인트 마틴'
 지역 = 10이면 '버뮤다'
 지역 = 11이면 '앤티가'
 지역 = 12이면 '큐라소'
 지역 = 13이면 '터크스 케이커스'
 Region = 14이면 '영국령 버진아일랜드'
 지역 = 15일 때 '도미니카'
 지역 = 16이면 '앵귈라'
 지역 = 17이면 '보네르'
 지역 = 18이면 '마르티니크'
 지역 = 19일 때 '몬스터랫'
 Region = 20이면 '가이아나'
 지역 = 21이면 '트리니다드토바고'
 지역 = 22일 때 '수리남'
 Region = 23이면 '엘살바도르'
 Region = 24이면 '파나마'
 지역 = 25일 때 '프랑스 령 기아나'
 지역 = 26이면 '과들루프'
 Region = 27이면 '세인트 마틴 세인트 바스'
 Region = 28이면 '아이티'
 지역 = 29일 때 '나우루'
 지역 = 30이면 '파푸아뉴기니'
 지역 = 31이면 '사모아'
 지역 = 32일 때 '통가'
 Region = 33일 때 '바누아투'
 Region = 34일 때 '피지'
 지역 = 35일 때 '마이애미'
 Region = 36일 때 'BVI'
 Region = 37일 때 'FWI\_GUA'
 Region = 38이면 'FWI\_FRA'
 Region = 39일 때 'FWI\_FGU'
 Region = 40일 때 'FWI\_MTQ'
 Region = 41일 때 '뉴욕'
 Region = 42이면 'NOECS' ";

SEVERITY ="case
 심각도 = 0이면 '지우기'
 when Severity = 2이면 '정보'
 심각도 = 1이면 '미정'
 심각도 = 3이면 '경미'
 심각도 = 4이면 '심각'
 심각도 = 5이면 '심각' ";

ORIGINALSEVERITY ="case
 OriginalSeverity = 0이면 'Clear'
 when OriginalSeverity = 2이면 '정보'
 when OriginalSeverity = 1이면 '미결정'
 when OriginalSeverity = 3이면 '경미'
 OriginalSeverity = 4이면 '중대'
 OriginalSeverity = 5이면 'Critical'";

if ($rep == '엔지니어\_옵션') 
{
engineer\_sql = "select 
SEVERITY 끝은 심각도 ,
ORIGINALSEVERITY 끝을 OriginalSeverity로 선택 ,
REGION 끝을 Region으로 선택합니다,
Table1.AlarmId,Acktime,fullname,
Table1.Node,
Table1.SiteName,
Table1.SiteCode,
from\_unixtime(Table1.FirstReported)를 FirstReported로 끝냅니다,
Table1.TicketID,
Table1.Summary,
Table1.AdditionalSummary, 
Table1.ClearedTime,
case 
ClearedTime > 0이면 
time\_format(sec\_to\_time(timestampdiff(second,from\_unixtime(Table1.FirstReported), Table1.ClearedTime)),'%Hh %im %ss')
ClearedTime이 null이면 time\_format(sec\_to\_time(timestampdiff(second,from\_unixtime(Table1.FirstReported), now())),'%Hh %im %ss') 
Duration으로 끝냅니다,
 경우 
 ClearedTime > 0 then
timestampdiff(second,from\_unixtime(Table1.FirstReported), Table1.ClearedTime)/60
 ClearedTime이 null이면 
timestampdiff(second,from\_unixtime(Table1.FirstReported), now())/60
 DDuration, Class
from 
$table Table1 
내부 조인 
(select AlarmId, MAX(StateChange) StateChange 
from $table 
여기서 (FirstReported > ".$timestamp1." 및 FirstReported < ".$timestamp2." 및 OriginalSeverity > 1 )
GROUP BY AlarmId ) Table2 
ON Table1.AlarmId = Table2.AlarmId 
AND Table1.StateChange=Table2.StateChange ".$filtersql\_sev. 
" 내부 조인 Monolith.AuthUsers table3 on OwnerID = table3.userid 및 UserName = '$engineeid' Order by Table1.FirstReported desc;";
}