select Approver,BusinessUnit,Department,sum(Total) Total,sum(OnTime) OnTime,sum(Delayed) Delayed from
(select distinct
case
when t.Task_AssigneeIsGroup = 0 then t.Task_Assignee
when t.Task_AssigneeIsGroup = 1 and gm.G_ISROLE=0 then t.Task_Assignee
when t.Task_AssigneeIsGroup = 1 and gm.G_ISROLE=1 then case
when u.user_name is null then t.Task_Assignee
else u.user_fullname end
end Approver ,
pa.PROJECT_ATTRVALUE BusinessUnit,
t.TASK_TYPENAME Department,
(select count(t1.TASK_ID) Reviews from
WCREPORTS_TASKSQ t1 left join WCREPORTS_PROJECT_ATTRQ pa1 onpa1.PROJECT_ID=t1.PROJECT_ID
left join PROJECT_WCTASK_DOCUMENTSQ td ont1.TASK_ID=td.WCT_TASK_ID
where t1.TASK_ASSIGNEE=t.TASK_ASSIGNEE andpa1.PROJECT_ATTRNAME=pa.PROJECT_ATTRNAME
and pa1.PROJECT_ATTRVALUE=pa.PROJECT_ATTRVALUE
and td.wct_deleted=0 and t1.TASK_TYPEID=t.TASK_TYPEID
) Total,
(select count(t1.TASK_ID) from
WCREPORTS_TASKSQ t1 left join WCREPORTS_PROJECT_ATTRQ pa1 onpa1.PROJECT_ID=t1.PROJECT_ID
left join PROJECT_WCTASK_DOCUMENTSQ td on t1.TASK_ID=td.WCT_TASK_ID
where t1.TASK_ASSIGNEE=t.TASK_ASSIGNEE andpa1.PROJECT_ATTRNAME=pa.PROJECT_ATTRNAME
and pa1.PROJECT_ATTRVALUE=pa.PROJECT_ATTRVALUE
and t1.TASK_COMPLETEDDATE <= t1.TASK_DUEDATE and td.wct_deleted=0
and t1.TASK_TYPEID=t.TASK_TYPEID
)OnTime,
(select count(t1.TASK_ID) from
WCREPORTS_TASKSQ t1 left join WCREPORTS_PROJECT_ATTRQ pa1 onpa1.PROJECT_ID=t1.PROJECT_ID
left join PROJECT_WCTASK_DOCUMENTSQ td on t1.TASK_ID=td.WCT_TASK_ID
where t1.TASK_ASSIGNEE=t.TASK_ASSIGNEE andpa1.PROJECT_ATTRNAME=pa.PROJECT_ATTRNAME
and pa1.PROJECT_ATTRVALUE=pa.PROJECT_ATTRVALUE
and (t1.TASK_COMPLETEDDATE is null or t1.TASK_COMPLETEDDATE>t1.TASK_DUEDATE)
and td.wct_deleted=0 and t1.TASK_TYPEID=t.TASK_TYPEID
) Delayed
from WCREPORTS_PROJECTSQ p left join WCREPORTS_PROJECT_ATTRQ pa on p.PROJECT_ID=pa.PROJECT_ID
left join WCREPORTS_TASKSQ t on t.PROJECT_ID=pa.PROJECT_ID
left join GROUP_MEMBERSQ gm on t.Task_assigneeId = gm.G_Id andgm.m_project_id=t.PROJECT_ID
left join PROJECT_USERS_GROUPS_ROLESQ ugr on ugr.g_id=t.Task_assigneeId andugr.p_id=t.PROJECT_ID
left join WCREPORTS_USERSQ u on ugr.u_id=u.USER_ID
where pa.PROJECT_ATTRNAME='Business Unit' and t.TASK_TYPENAME like'Approval - %'
and t.TASK_TYPEID=case $P{TaskTypeName}
when 'ALL' then t.TASK_TYPEID
when t.TASK_TYPEID then $P{TaskTypeName}
end
and p.PROJECT_CREATEDDATE >= to_Date($P{StartDate},'DD/MM/YY')
and p.PROJECT_CREATEDDATE < to_Date($P{EndDate},'DD/MM/YY')
and pa.PROJECT_ATTRVALUE=case $P{BusinessUnit}
when 'ALL' then pa.PROJECT_ATTRVALUE
when pa.PROJECT_ATTRVALUE then $P{BusinessUnit}
end
) tmp
group by BusinessUnit ,Department , Approver
(select distinct
case
when t.Task_AssigneeIsGroup = 0 then t.Task_Assignee
when t.Task_AssigneeIsGroup = 1 and gm.G_ISROLE=0 then t.Task_Assignee
when t.Task_AssigneeIsGroup = 1 and gm.G_ISROLE=1 then case
when u.user_name is null then t.Task_Assignee
else u.user_fullname end
end Approver ,
pa.PROJECT_ATTRVALUE BusinessUnit,
t.TASK_TYPENAME Department,
(select count(t1.TASK_ID) Reviews from
WCREPORTS_TASKSQ t1 left join WCREPORTS_PROJECT_ATTRQ pa1 onpa1.PROJECT_ID=t1.PROJECT_ID
left join PROJECT_WCTASK_DOCUMENTSQ td ont1.TASK_ID=td.WCT_TASK_ID
where t1.TASK_ASSIGNEE=t.TASK_ASSIGNEE andpa1.PROJECT_ATTRNAME=pa.PROJECT_ATTRNAME
and pa1.PROJECT_ATTRVALUE=pa.PROJECT_ATTRVALUE
and td.wct_deleted=0 and t1.TASK_TYPEID=t.TASK_TYPEID
) Total,
(select count(t1.TASK_ID) from
WCREPORTS_TASKSQ t1 left join WCREPORTS_PROJECT_ATTRQ pa1 onpa1.PROJECT_ID=t1.PROJECT_ID
left join PROJECT_WCTASK_DOCUMENTSQ td on t1.TASK_ID=td.WCT_TASK_ID
where t1.TASK_ASSIGNEE=t.TASK_ASSIGNEE andpa1.PROJECT_ATTRNAME=pa.PROJECT_ATTRNAME
and pa1.PROJECT_ATTRVALUE=pa.PROJECT_ATTRVALUE
and t1.TASK_COMPLETEDDATE <= t1.TASK_DUEDATE and td.wct_deleted=0
and t1.TASK_TYPEID=t.TASK_TYPEID
)OnTime,
(select count(t1.TASK_ID) from
WCREPORTS_TASKSQ t1 left join WCREPORTS_PROJECT_ATTRQ pa1 onpa1.PROJECT_ID=t1.PROJECT_ID
left join PROJECT_WCTASK_DOCUMENTSQ td on t1.TASK_ID=td.WCT_TASK_ID
where t1.TASK_ASSIGNEE=t.TASK_ASSIGNEE andpa1.PROJECT_ATTRNAME=pa.PROJECT_ATTRNAME
and pa1.PROJECT_ATTRVALUE=pa.PROJECT_ATTRVALUE
and (t1.TASK_COMPLETEDDATE is null or t1.TASK_COMPLETEDDATE>t1.TASK_DUEDATE)
and td.wct_deleted=0 and t1.TASK_TYPEID=t.TASK_TYPEID
) Delayed
from WCREPORTS_PROJECTSQ p left join WCREPORTS_PROJECT_ATTRQ pa on p.PROJECT_ID=pa.PROJECT_ID
left join WCREPORTS_TASKSQ t on t.PROJECT_ID=pa.PROJECT_ID
left join GROUP_MEMBERSQ gm on t.Task_assigneeId = gm.G_Id andgm.m_project_id=t.PROJECT_ID
left join PROJECT_USERS_GROUPS_ROLESQ ugr on ugr.g_id=t.Task_assigneeId andugr.p_id=t.PROJECT_ID
left join WCREPORTS_USERSQ u on ugr.u_id=u.USER_ID
where pa.PROJECT_ATTRNAME='Business Unit' and t.TASK_TYPENAME like'Approval - %'
and t.TASK_TYPEID=case $P{TaskTypeName}
when 'ALL' then t.TASK_TYPEID
when t.TASK_TYPEID then $P{TaskTypeName}
end
and p.PROJECT_CREATEDDATE >= to_Date($P{StartDate},'DD/MM/YY')
and p.PROJECT_CREATEDDATE < to_Date($P{EndDate},'DD/MM/YY')
and pa.PROJECT_ATTRVALUE=case $P{BusinessUnit}
when 'ALL' then pa.PROJECT_ATTRVALUE
when pa.PROJECT_ATTRVALUE then $P{BusinessUnit}
end
) tmp
group by BusinessUnit ,Department , Approver
MS-SQL-Error