SELECT --distinct
papf.EMPLOYEE_NUMBER,
papf.FULL_NAME EMPLOYEE_NAME,
to_char(htb1.start_time,'DD-MON-RRRR') START_DATE,
papa.segment1 PROJECT,
pt.TASK_number "Task",
hta.attribute3 "Type",
htb2.measure "Hours",
hts.approval_status "Status"
FROM hxc_time_building_blocks htb,
hxc_time_building_blocks htb1,
hxc_time_building_blocks htb2,
hxc_time_attribute_usages htau,
hxc_time_attributes hta,
pa_projects_all papa,
hxc_timecard_summary hts,
pa_tasks pt,
per_all_people_f papf
WHERE htb1.parent_building_block_id = htb.time_building_block_id
AND htb1.parent_building_block_ovn = htb.object_version_number
and HTB.SCOPE = 'TIMECARD'
AND htb.date_to = hr_general.end_of_time
AND htb1.date_to = hr_general.end_of_time
AND htb2.parent_building_block_id = htb1.time_building_block_id
AND htb2.parent_building_block_ovn = htb1.object_version_number
AND htb2.date_to = hr_general.end_of_time
AND htau.time_building_block_id = htb2.time_building_block_id
AND htau.time_building_block_ovn = htb2.object_version_number
AND htau.time_attribute_id = hta.time_attribute_id
AND papa.project_id = hta.attribute1
AND hts.start_time = htb.start_time
AND hts.resource_id = htb.resource_id
AND papf.person_id = htb.resource_id
AND papf.EMPLOYEE_NUMBER= nvl(:P_EMPLOYEE_NUMBER,papf.EMPLOYEE_NUMBER)
and htb1.start_time between papf.effective_start_date and papf.effective_end_Date
AND ((to_date(TO_CHAR(htb1.start_time ,'dd-mon-rrrr') ,'dd-mon-rrrr') >= to_date(:P_FM_DT,'dd-mon-rrrr') or :P_FM_DT is null)
AND (to_date(TO_CHAR(htb1.start_time ,'dd-mon-rrrr') ,'dd-mon-rrrr') <= to_date(:P_TO_DT,'dd-mon-rrrr') or :P_TO_DT is null))
AND hts.approval_status =nvl(:P_STATUS,hts.approval_status)
AND papa.segment1 =nvl(:P_PROJECT_NO,papa.segment1)
AND hta.attribute_category = 'PROJECTS'
AND hta.attribute2 = pt.task_id
AND hta.attribute1 = pt.project_id
and exists (select 1 from HXC_TIME_ATTRIBUTE_USAGES HTAU2,HXC_TIME_ATTRIBUTES HTA2
where HTAU2.TIME_ATTRIBUTE_ID = HTA2.TIME_ATTRIBUTE_ID
and HTAU2.TIME_BUILDING_BLOCK_ID = HTB2.TIME_BUILDING_BLOCK_ID
and HTA2.ATTRIBUTE_CATEGORY = 'SECURITY'
and HTA2.attribute1 = to_char(FND_GLOBAL.ORG_id)