BEGIN
INSERT INTO FlatTableOnAirTracker
SELECT
R.WF_ID
,M.WF_MILESTONE_ID
,(CASE WHEN r.PARENT_WF_ID IS NOT NULL THEN (SELECT WF_NUMBER FROM dbo.WORKFLOW_RECORD WHERE dbo.WORKFLOW_RECORD.WF_ID=r.PARENT_WF_ID)
ELSE r.WF_NUMBER END)AS [WorkflowRecord],
LM.SiteCode,
LM.SiteName,
R.ProjectName,
LM.SiteAddress,
P.PROJECT_NAME AS WRType,
LP.Band,
R.PONo,
M.MILESTONE_NAME AS MilestoneName,
UM.USERFULLNAME AS MilestoneUser,
m.MILESTONE_E_DATE,
COALESCE(RV.resevationNumber,ER.ReservationNumber, RM.ReservationMuxNumber) AS ReservationNumber
FROM
WF_PROJECT_DEFINITION P
INNER JOIN WORKFLOW_RECORD R ON P.PROJECT_ID=R.PROJECT_ID
INNER JOIN WORKFLOW_MILESTONE M ON R.WF_ID=M.WF_ID
AND R.PROJECT_ID NOT IN (7)
AND R.WF_STATUS <>'Rejected'
AND M.MILESTONE_STATUS='DONE'
AND M.MILESTONE_ROLE_ID IN (9,47,12)
AND M.MILESTONE_PROJECT_REL_ID IN (
,40 -- On air - 1--BTS GSM 900 Own Build
,67 -- On air - 2--BTS GSM 1800
,126 -- On air - 3--BTS GSM 900 Sharing
,168 -- On air - 4--3G Rollout
,584 -- On Air - 5--MW Link Installation & Commissioning PDH
,605 -- On Air - 6--MW Link Installation & Commissioning SDH
,248 -- On air - 8--BTS and NodeB Relocation
,381 -- On Air_RO - 9--GSM/WCDMA/LTE Optimization
,388 -- On Air_ANR - 9--GSM/WCDMA/LTE Optimization
,287 -- On air - 10--4th Cell Addition
,328 -- On air - 11--BTS and NodeB Relocation Sharing
,341 -- On Air - 12--TRX Relocation RO
,353 -- On Air - 13--TRX Relocation ANR
,371 -- On Air - 14--Hardware Addition ANR
,362 -- On Air - 15--Hardware Addition RO
,398 -- On Air - 16--Hardware Deletion ANR
,407 -- On Air - 17--Hardware Deletion RO
,466 -- On air - 20--New BSC
,492 -- On Air - 21--MW Link Swap
,527 -- On air - 22--Cabinet Relocation
,637 -- On air - 24--2G3G4G Cell Addition
,655 -- INP Check & On Air - 26--GB Expansion
,724 -- Swap Completed, Call Test & On Air - 28--BTS Swap
,862 -- On air - 33--IBS
,898 -- On air - 34--Repeater
,917 -- On air - 35--PSY Upgradation
,981 -- On air - 37--Event Management
,1027 -- On air - 38--3G New Site (Own Build)
,1071 -- Swap Completed, Call Test & On Air - 40--3G Swap
,1115 -- On air - 41--4G Rollout
,1146 -- On air - 42--U900
,1189 -- On air - 43--3G New Site (Sharing)
,1243 -- On air - 44--Small Cell (Own Build)
,1287 -- On air - 46--Small Cell (Sharing)
)
INNER JOIN LocationSelection S ON R.WF_ID = S.WFId_ProjectRequestId AND S.IsSource=0
LEFT JOIN LocationMaster LM ON LM.LocationMasterId=S.LocationMasterId AND (LM.IsDismantled IS NULL OR LM.IsDismantled=0)
----LEFT JOIN LocationMaster LM ON LM.SiteCode=RIGHT(R.SiteCode,7)
LEFT JOIN LocationProject LP ON LP.LocationProjectId=S.LocationProjectId AND LP.ProjectStatus<>'REJECTED'
LEFT JOIN USER_MASTER UM ON UM.EMPLID=M.LAST_MODIFIED_USER
LEFT JOIN Reservation RV ON RV.WFId_ProjectRequestId=R.WF_ID
LEFT JOIN EquipmentReceiveReservation ER ON ER.WF_ID=R.WF_ID
LEFT JOIN ReservationMux RM ON RM.WFId_ProjectRequestId=R.WF_ID
LEFT JOIN FlatTableOnAirTracker FONAIR ON FONAIR.WF_ID=R.WF_ID
WHERE FONAIR.WF_ID IS NULL
END
End