@Olga - Using that exactly as it is (IE: what you created) works great. However, if I take out the Part parameter, it returns no results. The only parameters I will be passing (once completed) will be a start and end date, which is why I tried to remove Part from the equation.
I tried a different-ish approach on this as well:
with [id] as
(select
[InvcDtl].[PartNum] as [InvcDtl_PartNum],
[InvcDtl].[UnitPrice] as [InvcDtl_UnitPrice],
[InvcDtl].[ShipDate] as [InvcDtl_ShipDate],
(ROW_NUMBER() OVER (PARTITION BY InvcDtl.PartNum ORDER BY InvcDtl.ShipDate DESC)) as [Calculated_rn]
from Erp.InvcDtl as InvcDtl
group by [InvcDtl].[PartNum],
[InvcDtl].[UnitPrice],
[InvcDtl].[ShipDate])
,[ld] as
(select
[LaborDtl].[Company] as [LaborDtl_Company],
[LaborDtl].[EmployeeNum] as [LaborDtl_EmployeeNum],
[LaborDtl].[ClockInDate] as [LaborDtl_ClockInDate],
[LaborDtl].[JobNum] as [LaborDtl_JobNum],
[JobHead].[PartNum] as [JobHead_PartNum]
from Erp.LaborDtl as LaborDtl
inner join Erp.JobHead as JobHead on
LaborDtl.Company = JobHead.Company
and LaborDtl.JobNum = JobHead.JobNum
group by [LaborDtl].[Company],
[LaborDtl].[EmployeeNum],
[LaborDtl].[ClockInDate],
[LaborDtl].[JobNum],
[JobHead].[PartNum])
select
[ld].[JobHead_PartNum] as [JobHead_PartNum],
[ld].[LaborDtl_ClockInDate] as [LaborDtl_ClockInDate],
[ld].[LaborDtl_Company] as [LaborDtl_Company],
[ld].[LaborDtl_EmployeeNum] as [LaborDtl_EmployeeNum],
[ld].[LaborDtl_JobNum] as [LaborDtl_JobNum],
[id].[Calculated_rn] as [Calculated_rn],
[id].[InvcDtl_PartNum] as [InvcDtl_PartNum],
[id].[InvcDtl_ShipDate] as [InvcDtl_ShipDate],
[id].[InvcDtl_UnitPrice] as [InvcDtl_UnitPrice]
from id as id
inner join ld as ld on
id.InvcDtl_PartNum = ld.JobHead_PartNum
where (id.Calculated_rn = 1)
and (ld.LaborDtl_ClockInDate = '01/03/2022')
This works perfectly from what I can tell. And when I run it in SSMS it takes less than 3 seconds.
As a BAQ, however, it takes almost 2 minutes ![]()