I am working on a BAQ to give some information about how much work is done on a job in the same month that it ships. I am very close, but I am getting duplicates in my output data. I am having trouble tracking and eliminating the duplicates.
My BAQ starts with the ship tables to determine the date shipments went out. From that, we lookup the number of hours spent on the job where the payroll date is within the same month and year as the ShipDate. We also gather the other side of that data to learn how many hours were spent working on a job where the payroll date is in a different month and/or year from the ShipDate.
If there is only one shipment, then this seems to return the correct data, but if there was more than one shipment (or more than one shipping line) then the values get duplicated at the top level.
Here is a brief example of the output:
Here you can see job 26053/7 has 87.71 hours of labor incurred in April, and 0.88 hours incurred in May. In the other column you can see 4 records of labor incurred in a month or year other than the month/year of the shipment. I have the month and year in here for debugging, but I think I would like to get rid of those columns and just have a single row for each job that shows in total how much time was spent on jobs that ship and jobs that don’t ship.
As you can see the value 87.71 and 0.88 are duplicated, and the values in the other column are duplicated as well.
At the very bottom you can see job 26093/2 correctly reporting a single line with 13.18 hours logged on the same month as the shipment, and only 0.05 hours logged outside of the month.
Here is my SQL and BAQ file:
select distinct
[ShipDtl2].[JobNum] as [ShipDtl2_JobNum],
[JobHead].[PartNum] as [JobHead_PartNum],
(year(ShipHead2.ShipDate)) as [Calculated_Year],
(month(ShipHead2.ShipDate)) as [Calculated_Month],
[ShippingJobsLabor].[Calculated_TotalLaborHours] as [Calculated_TotalLaborHours],
[NonShippingJobsLabor].[Calculated_TotalLabor] as [Calculated_TotalLabor]
from Erp.ShipHead as ShipHead2
inner join Erp.ShipDtl as ShipDtl2 on
ShipHead2.Company = ShipDtl2.Company
and ShipHead2.PackNum = ShipDtl2.PackNum
inner join Erp.JobHead as JobHead on
ShipDtl2.Company = JobHead.Company
and ShipDtl2.JobNum = JobHead.JobNum
left outer join (select distinct
[LaborDtl].[JobNum] as [LaborDtl_JobNum],
(month(LaborDtl.PayrollDate)) as [Calculated_LaborMonth],
(year(LaborDtl.PayrollDate)) as [Calculated_LaborYear],
(sum(LaborDtl.LaborHrs)) as [Calculated_TotalLaborHours]
from Erp.LaborDtl as LaborDtl
group by [LaborDtl].[JobNum],
(month(LaborDtl.PayrollDate)),
(year(LaborDtl.PayrollDate))) as ShippingJobsLabor on
ShipDtl2.JobNum = ShippingJobsLabor.LaborDtl_JobNum
left outer join (select distinct
[LaborDtl1].[JobNum] as [LaborDtl1_JobNum],
(year(LaborDtl1.PayrollDate)) as [Calculated_LaborYear],
(month(LaborDtl1.PayrollDate)) as [Calculated_LaborMonth],
(sum(LaborDtl1.LaborHrs)) as [Calculated_TotalLabor]
from Erp.LaborDtl as LaborDtl1
group by [LaborDtl1].[JobNum],
(year(LaborDtl1.PayrollDate)),
(month(LaborDtl1.PayrollDate))) as NonShippingJobsLabor on
ShipDtl2.JobNum = NonShippingJobsLabor.LaborDtl1_JobNum
where (ShipHead2.ShipDate >= dateadd (year, -4, Constants.Today))
and (not ShipDtl2.JobNum = '' and (month(ShipHead2.ShipDate)) = ShippingJobsLabor.Calculated_LaborMonth and (year(ShipHead2.ShipDate)) = ShippingJobsLabor.Calculated_LaborYear and (month(ShipHead2.ShipDate)) <> NonShippingJobsLabor.Calculated_LaborMonth and (year(ShipHead2.ShipDate)) <> NonShippingJobsLabor.Calculated_LaborYear)
PercWorkNonShipJobsByMonth.baq (43.8 KB)
Thanks for taking a look!
Nate