SELECT taskdet.TASKID AS "Task ID",
taskdet.TITLE AS "Title",
taskowner.FIRST_NAME AS "Owner",
taskcreatedby.FIRST_NAME AS "Created By",
taskstatus.STATUSNAME AS "Task Status",
taskdesc.DESCRIPTION AS "Description",
longtodate(projectdet.SCHEDULEDSTARTTIME) "Project Schedule start",
longtodate(projectdet.SCHEDULEDENDTIME) "Project Schedule end",
longtodate(projectdet.ACTUALSTARTTIME) "Project Actual start",
longtodate(projectdet.ACTUALENDTIME) "Project Actual end",
taskdet.PER_OF_COMPLETION "Task Percentage Of Completion",
MAX(c.comment) "Comments" FROM TaskDetails taskdet
LEFT JOIN TaskToProjects projtotaskdet ON taskdet.TASKID=projtotaskdet.TASKID
LEFT JOIN ProjectDetails projectdet ON projtotaskdet.PROJECTID=projectdet.PROJECTID
LEFT JOIN TaskToProjects miletotaskdet ON taskdet.TASKID=miletotaskdet.TASKID
LEFT JOIN MileStoneDetails md ON miletotaskdet.MILESTONEID=md.MILESTONEID
LEFT JOIN SDUser projectownerdet ON projectdet.OWNERID=projectownerdet.USERID
LEFT JOIN AaaUser taskcreatedby ON taskdet.CREATEDBY=taskcreatedby.USER_ID
LEFT JOIN SDUser taskownersdu ON taskdet.OWNERID=taskownersdu.USERID
LEFT JOIN AaaUser taskowner ON taskownersdu.USERID=taskowner.USER_ID
LEFT JOIN StatusDefinition taskstatus ON taskdet.STATUSID=taskstatus.STATUSID
LEFT JOIN TaskDescription taskdesc ON taskdet.TASKID=taskdesc.TASKID
LEFT JOIN TaskToComment tskc ON tskc.taskid = taskdet.taskid
LEFT JOIN Comments c ON tskc.commentid=c.commentid
where c.commentid= (select MAX(TaskToComment.commentid) from TaskToComment ) or tskc .taskid is null
GROUP BY TaskDet.TASKID,taskdet.TITLE,taskowner.FIRST_NAME,taskcreatedby.FIRST_NAME,taskstatus.STATUSNAME,taskdesc.DESCRIPTION, c.comment,ProjectDet.SCHEDULEDSTARTTIME,ProjectDet.SCHEDULEDENDTIME,ProjectDet.ACTUALSTARTTIME,ProjectDet.ACTUALENDTIME,TaskDet.PER_OF_COMPLETION