I am writing a query to return the current revision and the prior revision in two different columns. When I write it in the BAQ designer it takes 15 seconds to return 3 parts. When I paste the code in SQL Management Studio it runs instantly. Any thoughts on what I may be doing wrong or how to improve the BAQ performance?
BAQ Output:
Top query:
The sub query is basically generating a rownumber and sorting by effective date descending and the top query is filtering the subquery by row two for the prior revision.
Here is the code the BAQ generated:
select
[Part].[PartNum] as [Part_PartNum],
[Part].[PartDescription] as [Part_PartDescription],
[PartRev].[RevisionNum] as [PartRev_RevisionNum],
[PartRev].[Approved] as [PartRev_Approved],
[PartRev].[EffectiveDate] as [PartRev_EffectiveDate],
[LastRevision].[PartRevLastRevision_RevisionNum] as [PartRevLastRevision_RevisionNum],
[LastRevision].[PartRevLastRevision_EffectiveDate] as [PartRevLastRevision_EffectiveDate],
((case when LastRevision.PartRevLastRevision_RevisionNum <> ‘’ then LastRevision.PartRevLastRevision_RevisionNum else ‘New Part’ end)) as [Calculated_PriorRevision],
[PartRev].[ECO] as [PartRev_ECO]
from Erp.Part as Part
inner join Erp.PartRev as PartRev on
Part.Company = PartRev.Company
and Part.PartNum = PartRev.PartNum
and ( PartRev.ECO = ‘152’ and PartRev.PartNum = ANY (select PartRevLastRevision_PartNum from ((select
[PartRevLastRevision].[PartNum] as [PartRevLastRevision_PartNum],
[PartRevLastRevision].[RevisionNum] as [PartRevLastRevision_RevisionNum],
[PartRevLastRevision].[Approved] as [PartRevLastRevision_Approved],
[PartRevLastRevision].[EffectiveDate] as [PartRevLastRevision_EffectiveDate],
[PartRevLastRevision].[Company] as [PartRevLastRevision_Company],
(ROW_NUMBER() OVER(PARTITION BY PartRevLastRevision.PartNum ORDER BY PartRevLastRevision.EffectiveDate DESC)) as [Calculated_RevisionRow]
from Erp.PartRev as PartRevLastRevision)) as LastRevision) )
left outer join (select
[PartRevLastRevision].[PartNum] as [PartRevLastRevision_PartNum],
[PartRevLastRevision].[RevisionNum] as [PartRevLastRevision_RevisionNum],
[PartRevLastRevision].[Approved] as [PartRevLastRevision_Approved],
[PartRevLastRevision].[EffectiveDate] as [PartRevLastRevision_EffectiveDate],
[PartRevLastRevision].[Company] as [PartRevLastRevision_Company],
(ROW_NUMBER() OVER(PARTITION BY PartRevLastRevision.PartNum ORDER BY PartRevLastRevision.EffectiveDate DESC)) as [Calculated_RevisionRow]
from Erp.PartRev as PartRevLastRevision) as LastRevision on
PartRev.Company = LastRevision.PartRevLastRevision_Company
and PartRev.PartNum = LastRevision.PartRevLastRevision_PartNum
and ( LastRevision.Calculated_RevisionRow = 2 )
Thanks,
Ross