I created a report where in an ID is selected by the user. Under that ID there is a cut off period and within that ID it is split into one or more records that have date range also. For example:
ID: MMA1512 - 3/1-3/15
compose of
-- > 'P2306.2012.15.03.A.STAFF' - 2/24-3/8
-- > 'P2306.2012.08.03.A.DAILY' - 3/1-3/15
Tables:
Work Allocation
id
startdate
Work Allocation Details
workallocationid
employee no
costcode
subcostcode
hrswork
The problem is there is an overlapping data for 3/1-3/8 but I cannot exclude the overlapping date because it was needed on 'P2306.2012.08.03.A.DAILY'. Here is my query
Wrong Output
SELECT wa.companycode,wa.sitecode,wa_details.employeeno,
wa_details.costcode,wa_details.subcostcode,
SUM(wa_details.hrswork) hrswork,ttl_work_hrs
FROM WorkAllocation wa
INNER JOIN WorkAllocation_details wa_details ON wa.id = wa_details.workallocationid
INNER JOIN
( SELECT employee_id,ttl_work_hrs FROM [dbo].[Get_Total_Work_Hours] ('mma1512','P2306.2012.15.03.l.STAFF,P2306.2012.08.03.l.DAILY','p2306','l'))
view_ttl_hours
ON
wa_details.employeeno=view_ttl_hours.employee_id
WHERE
companycode='l'
AND
sitecode='p2306'
AND
startdate BETWEEN '2012-02-24' AND '2012-03-15'
GROUP BY wa.companycode,wa.sitecode, wa_details.employeeno,wa_details.costcode,wa_details.subcostcode,ttl_work_hrs
Company Site EmployeeNo CostCode SubCostCode HrsWork Total Hours Work
LCPI P2306 PH080323 8321 169 160.50 162.5
LCPI P2306 PH080323 10184 2648 14.50 162.5
Correct Output
SELECT wa.companycode,wa.sitecode,wa_details.employeeno,
wa_details.costcode,wa_details.subcostcode,
SUM(wa_details.hrswork) hrswork,ttl_work_hrs
FROM WorkAllocation wa
INNER JOIN WorkAllocation_details wa_details ON wa.id = wa_details.workallocationid
INNER JOIN
( SELECT employee_id,ttl_work_hrs FROM [dbo].[Get_Total_Work_Hours] ('mma1512','P2306.2012.15.03.l.STAFF,P2306.2012.08.03.l.DAILY','p2306','l'))
view_ttl_hours
ON
wa_details.employeeno=view_ttl_hours.employee_id
WHERE
companycode='l'
AND
sitecode='p2306'
AND
startdate BETWEEN '2012-02-24' AND '2012-03-08'
GROUP BY wa.companycode,wa.sitecode, wa_details.employeeno,wa_details.costcode,wa_details.subcostcode,ttl_work_hrs
Company Site EmployeeNo CostCode SubCostCode HrsWork Total Hours Work
LCPI P2306 PH080323 8321 169 148.00 162.5
LCPI P2306 PH080323 10184 2648 14.50 162.5
The difference between the query is the startdate BETWEEN '2012-02-24' AND '2012-03-15'