SELECT DISTINCT
W.WO_ID,
W.PLANNED_TIME,
W.TITLE,
W.ID,
W.COTPROJ_PROJID,
W.PROJPHASE,
W.START_DATE
FROM PROJECT.MAINTENANCE_WO W
JOIN PROJECT.MAINTENANCE_WORKFLOW_STATES S
ON S.ID = W.MAINT_WF_STATE_ID
AND S.COTSTD_STDID = W.MAINT_WF_STATE_STDID
LEFT JOIN PROJECT.MAINTENANCE_SCHEDULE
ON MAINTENANCE_SCHEDULE.WO_ID = W.ID
AND MAINTENANCE_SCHEDULE.WO_PROJID = W.COTPROJ_PROJID
AND MAINTENANCE_SCHEDULE.WO_PHASE = W.PROJPHASE
WHERE W.COTPROJ_PROJID = 'DALSE'
AND S.CLOSED = 'N'
AND (((MAINTENANCE_SCHEDULE.FINISHTIME - MAINTENANCE_SCHEDULE.STARTTIME) *24*60*60) < W.PLANNED_TIME
OR ((CASE WHEN W.PLANNED_TIME IS NULL THEN 0 ELSE W.PLANNED_TIME END) = 0
AND MAINTENANCE_SCHEDULE.STARTTIME IS NULL))
ORDER BY W.WO_ID