我在Epicor中有一个查询,我使用BAQ Designer(它只是创建SQL查询的gui)构建的。我最近进行了更改以添加更多数据,现在收到以下错误。
查询处理器耗尽了内部资源,无法生成查询计划。这是一种罕见的事件,仅适用于引用大量表或分区的极其复杂的查询或查询。请简化查询。
我确定我创造的怪物不是最佳的,我可能会做一些改变以使其变得更好。如果任何专家可以看一看并给予帮助,我将非常感激,因为我不是SQL专家,只是足以打破东西.SQL是:
With [Sales_Base] AS
(select
[SalesRep].[SalesRepCode] as [SalesRep_SalesRepCode],
[SalesRep].[Name] as [SalesRep_Name],
[InvcHead].[InvoiceNum] as [InvcHead_InvoiceNum],
[InvcHead].[InvoiceDate] as [InvcHead_InvoiceDate],
[InvcHead].[PONum] as [InvcHead_PONum],
[InvcDtl].[ExtPrice] as [InvcDtl_ExtPrice],
[Customer].[CustID] as [Customer_CustID],
[Customer].[Name] as [Customer_Name],
[SalesCat].[Description] as [SalesCat_Description],
[InvcDtl].[ProdCode] as [InvcDtl_ProdCode],
[SalesRep].[RoleCode] as [SalesRep_RoleCode],
[Customer].[GroupCode] as [Customer_GroupCode],
(Constants.Today) as [Calculated_Today],
(Constants.FirstDayOfMonth) as [Calculated_BOM],
(case
when DatePart(month,Constants.Today) in (1,4,7,10) then Constants.FirstDayOfMonth
when DatePart(month,Constants.Today) in (2,5,8,11) then DateAdd(month,-1,Constants.FirstDayOfMonth)
else DateAdd(month,-2,Constants.FirstDayOfMonth)
end) as [Calculated_BOQ],
(case
when DatePart(month,Constants.Today) = 1 then Constants.FirstDayOfMonth
when DatePart(month,Constants.Today) = 2 then DateAdd(month,-1,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 3 then DateAdd(month,-2,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 4 then DateAdd(month,-3,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 5 then DateAdd(month,-4,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 6 then DateAdd(month,-5,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 7 then DateAdd(month,-6,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 8 then DateAdd(month,-7,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 9 then DateAdd(month,-8,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 10 then DateAdd(month,-9,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 11 then DateAdd(month,-10,Constants.FirstDayOfMonth)
else DateAdd(month,-11,Constants.FirstDayOfMonth)
end) as [Calculated_BOY],
[Customer5].[CustID] as [Customer5_CustID],
[Customer5].[Name] as [Customer5_Name],
[Customer5].[GroupCode] as [Customer5_GroupCode],
[SalesRep].[EMailAddress] as [SalesRep_EMailAddress],
[InvcDtl].[InvoiceLine] as [InvcDtl_InvoiceLine],
(InvcDtl.ExtPrice + IsNull(InvcMisc.MiscAmt,0)) as [Calculated_InvcDtl_ExtPrice]
from Erp.SalesRep as SalesRep
left outer join Erp.InvcHead as InvcHead on
SalesRep.Company = InvcHead.Company
And
SalesRep.SalesRepCode = [Ice].entry(1,SalesRepList,'~')
Or
SalesRep.SalesRepCode = [Ice].entry(2,SalesRepList,'~')
Or
SalesRep.SalesRepCode = [Ice].entry(3,SalesRepList,'~')
Or
SalesRep.SalesRepCode = [Ice].entry(4,SalesRepList,'~')
Or
SalesRep.SalesRepCode = [Ice].entry(5,SalesRepList,'~')
and ( InvcHead.Posted = TRUE and InvcHead.InvoiceType <> 'DEP' )
left outer join Erp.InvcDtl as InvcDtl on
InvcHead.Company = InvcDtl.Company
And
InvcHead.InvoiceNum = InvcDtl.InvoiceNum
left outer join Erp.Customer as Customer on
InvcHead.Company = Customer.Company
And
InvcHead.SoldToCustNum = Customer.CustNum
left outer join Erp.SalesCat as SalesCat on
InvcDtl.Company = SalesCat.Company
And
InvcDtl.SalesCatID = SalesCat.SalesCatID
left outer join Erp.Customer as Customer5 on
InvcDtl.Company = Customer5.Company
And
InvcDtl.BTCustNum = Customer5.CustNum
left outer join Erp.InvcMisc as InvcMisc on
InvcDtl.Company = InvcMisc.Company
And
InvcDtl.InvoiceNum = InvcMisc.InvoiceNum
And
InvcDtl.InvoiceLine = InvcMisc.InvoiceLine
and ( InvcMisc.MiscCode = 'exp' )
where (SalesRep.RoleCode in ('sales', 'salesden', 'salesflx', 'salesmed', 'salespm') and SalesRep.SalesRepCode <> 'default' and SalesRep.SalesRepCode <> 'national' and SalesRep.SalesRepCode <> 'house' and SalesRep.SalesRepCode <> 'flexi' and SalesRep.SalesRepCode <> 'prescale' and SalesRep.InActive = FALSE))
,[Sales_Base_Add_MR] AS
(select
(Sales_Base5.SalesRep_SalesRepCode) as [Calculated_A_SalesRepCode],
(Sales_Base5.SalesRep_Name) as [Calculated_A_SalesRepName],
(Sales_Base5.InvcHead_InvoiceNum) as [Calculated_A_InvoiceNum],
(Sales_Base5.InvcHead_InvoiceDate) as [Calculated_A_InvoiceDate],
(Sales_Base5.InvcHead_PONum) as [Calculated_A_PONum],
(Sales_Base5.Calculated_InvcDtl_ExtPrice) as [Calculated_A_ExtPrice],
(Sales_Base5.Customer_CustID) as [Calculated_A_CustID],
(Sales_Base5.Customer_Name) as [Calculated_A_Cust],
(Sales_Base5.SalesCat_Description) as [Calculated_A_SalesCat],
(Sales_Base5.InvcDtl_ProdCode) as [Calculated_A_ProdCode],
(Sales_Base5.SalesRep_RoleCode) as [Calculated_A_RoleCode],
(Sales_Base5.Customer_GroupCode) as [Calculated_A_GroupCode],
(Sales_Base5.Calculated_Today) as [Calculated_A_Today],
(Sales_Base5.Calculated_BOM) as [Calculated_A_BOM],
(Sales_Base5.Calculated_BOQ) as [Calculated_A_BOQ],
(Sales_Base5.Calculated_BOY) as [Calculated_A_BOY],
(Sales_Base5.Customer5_CustID) as [Calculated_A_BTCustID],
(Sales_Base5.Customer5_Name) as [Calculated_A_BTCust],
(Sales_Base5.Customer5_GroupCode) as [Calculated_A_BTGroupCode],
[SalesRep2].[SalesRepCode] as [SalesRep2_SalesRepCode],
[SalesRep2].[Name] as [SalesRep2_Name],
[SalesRep2].[RoleCode] as [SalesRep2_RoleCode],
(Sales_Base5.SalesRep_EMailAddress) as [Calculated_A_Email],
(Sales_Base5.InvcDtl_InvoiceLine) as [Calculated_A_Line]
from Sales_Base as Sales_Base5
left outer join Erp.InvcHead as InvcHead1 on
Sales_Base5.InvcHead_InvoiceNum = InvcHead1.InvoiceNum
left outer join Erp.SalesRep as SalesRep2 on
InvcHead1.Company = SalesRep2.Company
And
[Ice].entry(1,SalesRepList,'~') = SalesRep2.SalesRepCode
Or
[Ice].entry(2,SalesRepList,'~') = SalesRep2.SalesRepCode
Or
[Ice].entry(3,SalesRepList,'~') = SalesRep2.SalesRepCode
Or
[Ice].entry(4,SalesRepList,'~') = SalesRep2.SalesRepCode
Or
[Ice].entry(5,SalesRepList,'~') = SalesRep2.SalesRepCode
and ( SalesRep2.RoleCode = 'mfgrep' )
where (Sales_Base5.Calculated_InvcDtl_ExtPrice <> 0 and Sales_Base5.InvcDtl_ProdCode <> ''))
,[Total] AS
(select
[Sales_Base_Add_MR3].[Calculated_A_InvoiceNum] as [Calculated_A_InvoiceNum],
(sum( Sales_Base_Add_MR3.Calculated_A_ExtPrice )) as [Calculated_Invoice_Total]
from Sales_Base_Add_MR as Sales_Base_Add_MR3
group by [Sales_Base_Add_MR3].[Calculated_A_InvoiceNum])
,[Key1] AS
(select
(Sales_Base_Add_MR.Calculated_A_SalesRepName) as [Calculated_Key1_SR],
(Sales_Base_Add_MR.Calculated_A_SalesRepName + Sales_Base_Add_MR.SalesRep2_Name) as [Calculated_Key1_Key]
from Sales_Base_Add_MR as Sales_Base_Add_MR
where (Sales_Base_Add_MR.SalesRep2_RoleCode = 'mfgrep')
group by (Sales_Base_Add_MR.Calculated_A_SalesRepName) as [Calculated_Key1_SR],
(Sales_Base_Add_MR.Calculated_A_SalesRepName + Sales_Base_Add_MR.SalesRep2_Name) as [Calculated_Key1_Key])
,[Key2] AS
(select
(Sales_Base_Add_MR2.Calculated_A_SalesRepName) as [Calculated_Key2_SR],
(Sales_Base_Add_MR2.Calculated_A_SalesRepName + Sales_Base_Add_MR2.Calculated_A_BTCustID) as [Calculated_Key2_Key]
from Sales_Base_Add_MR as Sales_Base_Add_MR2
where (Sales_Base_Add_MR2.Calculated_A_BTGroupCode in ('dend', 'flxd', 'medd', 'prsd'))
group by (Sales_Base_Add_MR2.Calculated_A_SalesRepName) as [Calculated_Key2_SR],
(Sales_Base_Add_MR2.Calculated_A_SalesRepName + Sales_Base_Add_MR2.Calculated_A_BTCustID) as [Calculated_Key2_Key])
,[B] AS
(select
(Sales_Base_Add_MR14.Calculated_A_SalesRepCode) as [Calculated_B_SalesRepCode],
(Sales_Base_Add_MR14.Calculated_A_SalesRepName) as [Calculated_B_SalesRepName],
(Sales_Base_Add_MR14.Calculated_A_InvoiceNum) as [Calculated_B_InvoiceNum],
(Sales_Base_Add_MR14.Calculated_A_InvoiceDate) as [Calculated_B_InvoiceDate],
(Sales_Base_Add_MR14.Calculated_A_PONum) as [Calculated_B_PONum],
(Total.Calculated_Invoice_Total) as [Calculated_B_OrderTotal],
(Sales_Base_Add_MR14.Calculated_A_CustID) as [Calculated_B_CustID],
(Sales_Base_Add_MR14.Calculated_A_Cust) as [Calculated_B_Cust],
(Sales_Base_Add_MR14.Calculated_A_RoleCode) as [Calculated_B_RoleCode],
(Sales_Base_Add_MR14.Calculated_A_GroupCode) as [Calculated_B_GroupCode],
(Sales_Base_Add_MR14.Calculated_A_Today) as [Calculated_B_Today],
(Sales_Base_Add_MR14.Calculated_A_BOM) as [Calculated_B_BOM],
(Sales_Base_Add_MR14.Calculated_A_BOQ) as [Calculated_B_BOQ],
(Sales_Base_Add_MR14.Calculated_A_BOY) as [Calculated_B_BOY],
(Sales_Base_Add_MR14.Calculated_A_BTCustID) as [Calculated_B_BTCustID],
(Sales_Base_Add_MR14.Calculated_A_BTCust) as [Calculated_B_BTCust],
(Sales_Base_Add_MR14.Calculated_A_BTGroupCode) as [Calculated_B_BTGroupCode],
(Sales_Base_Add_MR14.SalesRep2_SalesRepCode) as [Calculated_B_MRCode],
(Sales_Base_Add_MR14.SalesRep2_Name) as [Calculated_B_MRName],
(Sales_Base_Add_MR14.SalesRep2_RoleCode) as [Calculated_B_MRRoleCode],
(Sales_Base_Add_MR14.Calculated_A_Email) as [Calculated_B_Email],
(Key1.Calculated_Key1_Key) as [Calculated_B_Key],
(Key2.Calculated_Key2_Key) as [Calculated_B_Key2]
from Sales_Base_Add_MR as Sales_Base_Add_MR14
left outer join Key1 as Key1 on
Sales_Base_Add_MR14.Calculated_A_SalesRepName = Key1.Calculated_Key1_SR
And
Calculated_A_SalesRepName + SalesRep2_Name = Key1.Calculated_Key1_Key
left outer join Key2 as Key2 on
Sales_Base_Add_MR14.Calculated_A_SalesRepName = Key2.Calculated_Key2_SR
And
Calculated_A_SalesRepName + Calculated_A_BTCustID = Key2.Calculated_Key2_Key
left outer join Total as Total on
Sales_Base_Add_MR14.Calculated_A_InvoiceNum = Total.Calculated_A_InvoiceNum
group by (Sales_Base_Add_MR14.Calculated_A_SalesRepCode) as [Calculated_B_SalesRepCode],
(Sales_Base_Add_MR14.Calculated_A_SalesRepName) as [Calculated_B_SalesRepName],
(Sales_Base_Add_MR14.Calculated_A_InvoiceNum) as [Calculated_B_InvoiceNum],
(Sales_Base_Add_MR14.Calculated_A_InvoiceDate) as [Calculated_B_InvoiceDate],
(Sales_Base_Add_MR14.Calculated_A_PONum) as [Calculated_B_PONum],
(Total.Calculated_Invoice_Total) as [Calculated_B_OrderTotal],
(Sales_Base_Add_MR14.Calculated_A_CustID) as [Calculated_B_CustID],
(Sales_Base_Add_MR14.Calculated_A_Cust) as [Calculated_B_Cust],
(Sales_Base_Add_MR14.Calculated_A_RoleCode) as [Calculated_B_RoleCode],
(Sales_Base_Add_MR14.Calculated_A_GroupCode) as [Calculated_B_GroupCode],
(Sales_Base_Add_MR14.Calculated_A_Today) as [Calculated_B_Today],
(Sales_Base_Add_MR14.Calculated_A_BOM) as [Calculated_B_BOM],
(Sales_Base_Add_MR14.Calculated_A_BOQ) as [Calculated_B_BOQ],
(Sales_Base_Add_MR14.Calculated_A_BOY) as [Calculated_B_BOY],
(Sales_Base_Add_MR14.Calculated_A_BTCustID) as [Calculated_B_BTCustID],
(Sales_Base_Add_MR14.Calculated_A_BTCust) as [Calculated_B_BTCust],
(Sales_Base_Add_MR14.Calculated_A_BTGroupCode) as [Calculated_B_BTGroupCode],
(Sales_Base_Add_MR14.SalesRep2_SalesRepCode) as [Calculated_B_MRCode],
(Sales_Base_Add_MR14.SalesRep2_Name) as [Calculated_B_MRName],
(Sales_Base_Add_MR14.SalesRep2_RoleCode) as [Calculated_B_MRRoleCode],
(Sales_Base_Add_MR14.Calculated_A_Email) as [Calculated_B_Email],
(Key1.Calculated_Key1_Key) as [Calculated_B_Key],
(Key2.Calculated_Key2_Key) as [Calculated_B_Key2])
,[Sales_BOM] AS
(select
(B1.Calculated_B_SalesRepName) as [Calculated_Sales_BOM_Rep],
(sum( B1.Calculated_B_OrderTotal )) as [Calculated_Sales_BOM_Sum]
from B as B1
where (B1.Calculated_B_InvoiceDate >= B1.Calculated_B_BOM and B1.Calculated_B_InvoiceDate <= B1.Calculated_B_Today)
group by (B1.Calculated_B_SalesRepName) as [Calculated_Sales_BOM_Rep])
,[Sales_BOQ] AS
(select
(B2.Calculated_B_SalesRepName) as [Calculated_Sales_BOQ_Rep],
(sum( B2.Calculated_B_OrderTotal )) as [Calculated_Sales_BOQ_Sum]
from B as B2
where (B2.Calculated_B_InvoiceDate >= B2.Calculated_B_BOQ and B2.Calculated_B_InvoiceDate <= B2.Calculated_B_Today)
group by (B2.Calculated_B_SalesRepName) as [Calculated_Sales_BOQ_Rep])
,[Sales_BOY] AS
(select
(B3.Calculated_B_SalesRepName) as [Calculated_Sales_BOY_Rep],
(sum( B3.Calculated_B_OrderTotal )) as [Calculated_Sales_BOY_Sum]
from B as B3
where (B3.Calculated_B_InvoiceDate >= B3.Calculated_B_BOY and B3.Calculated_B_InvoiceDate <= B3.Calculated_B_Today)
group by (B3.Calculated_B_SalesRepName) as [Calculated_Sales_BOY_Rep])
,[MR_BOM] AS
(select
(sum( B5.Calculated_B_OrderTotal )) as [Calculated_MR_BOM_Sum],
(B5.Calculated_B_Key) as [Calculated_MR_BOM_Key]
from B as B5
where (B5.Calculated_B_InvoiceDate >= B5.Calculated_B_BOM and B5.Calculated_B_InvoiceDate <= B5.Calculated_B_Today and B5.Calculated_B_MRRoleCode = 'mfgrep')
group by (B5.Calculated_B_Key) as [Calculated_MR_BOM_Key])
,[MR_BOQ] AS
(select
(sum( B6.Calculated_B_OrderTotal )) as [Calculated_MR_BOQ_Sum],
(B6.Calculated_B_Key) as [Calculated_MR_BOQ_Key]
from B as B6
where (B6.Calculated_B_InvoiceDate >= B6.Calculated_B_BOQ and B6.Calculated_B_InvoiceDate <= B6.Calculated_B_Today and B6.Calculated_B_MRRoleCode = 'mfgrep')
group by (B6.Calculated_B_Key) as [Calculated_MR_BOQ_Key])
,[MR_BOY] AS
(select
(sum( B7.Calculated_B_OrderTotal )) as [Calculated_MR_BOY_Sum],
(B7.Calculated_B_Key) as [Calculated_MR_BOY_Key],
(B7.Calculated_B_MRName) as [Calculated_MR_BOY_MR]
from B as B7
where (B7.Calculated_B_InvoiceDate >= B7.Calculated_B_BOY and B7.Calculated_B_InvoiceDate <= B7.Calculated_B_Today and B7.Calculated_B_MRRoleCode = 'mfgrep')
group by (B7.Calculated_B_Key) as [Calculated_MR_BOY_Key],
(B7.Calculated_B_MRName) as [Calculated_MR_BOY_MR])
,[Dist_BOM] AS
(select
(sum( B9.Calculated_B_OrderTotal )) as [Calculated_Dist_BOM_Sum],
(B9.Calculated_B_Key2) as [Calculated_Dist_BOM_Key]
from B as B9
where (B9.Calculated_B_InvoiceDate >= B9.Calculated_B_BOM and B9.Calculated_B_InvoiceDate <= B9.Calculated_B_Today and B9.Calculated_B_BTGroupCode in ('dend', 'flxd', 'medd', 'prsd'))
group by (B9.Calculated_B_Key2) as [Calculated_Dist_BOM_Key])
,[Dist_BOQ] AS
(select
(sum( B10.Calculated_B_OrderTotal )) as [Calculated_Dist_BOQ_Sum],
(B10.Calculated_B_Key2) as [Calculated_Dist_BOQ_Key]
from B as B10
where (B10.Calculated_B_InvoiceDate >= B10.Calculated_B_BOQ and B10.Calculated_B_InvoiceDate <= B10.Calculated_B_Today and B10.Calculated_B_BTGroupCode in ('dend', 'flxd', 'medd', 'prsd'))
group by (B10.Calculated_B_Key2) as [Calculated_Dist_BOQ_Key])
,[Dist_BOY] AS
(select
(sum( B11.Calculated_B_OrderTotal )) as [Calculated_Dist_BOY_Sum],
(B11.Calculated_B_Key2) as [Calculated_Dist_BOY_Key],
(B11.Calculated_B_BTCustID) as [Calculated_Dist_BOY_ID],
(B11.Calculated_B_BTCust) as [Calculated_Dist_BOYDist],
(B11.Calculated_B_BTGroupCode) as [Calculated_Dist_BOY_Group]
from B as B11
where (B11.Calculated_B_InvoiceDate >= B11.Calculated_B_BOY and B11.Calculated_B_InvoiceDate <= B11.Calculated_B_Today and B11.Calculated_B_BTGroupCode in ('dend', 'flxd', 'medd', 'prsd'))
group by (B11.Calculated_B_Key2) as [Calculated_Dist_BOY_Key],
(B11.Calculated_B_BTCustID) as [Calculated_Dist_BOY_ID],
(B11.Calculated_B_BTCust) as [Calculated_Dist_BOYDist],
(B11.Calculated_B_BTGroupCode) as [Calculated_Dist_BOY_Group])
,[Sales_Group] AS
(select
(B12.Calculated_B_SalesRepName) as [Calculated_Sales_Group_SR],
(B12.Calculated_B_Email) as [Calculated_Sales_Group_Email]
from B as B12
group by (B12.Calculated_B_SalesRepName) as [Calculated_Sales_Group_SR],
(B12.Calculated_B_Email) as [Calculated_Sales_Group_Email])
,[MR_Group] AS
(select
(B13.Calculated_B_SalesRepName) as [Calculated_MR_Group_SR],
(B13.Calculated_B_Email) as [Calculated_MR_Group_Email],
(B13.Calculated_B_Key) as [Calculated_MR_Group_Key]
from B as B13
where (B13.Calculated_B_Key is not null)
group by (B13.Calculated_B_SalesRepName) as [Calculated_MR_Group_SR],
(B13.Calculated_B_Email) as [Calculated_MR_Group_Email],
(B13.Calculated_B_Key) as [Calculated_MR_Group_Key])
,[Dist_Group] AS
(select
(B15.Calculated_B_SalesRepName) as [Calculated_Dist_Group_SR],
(B15.Calculated_B_Email) as [Calculated_Dist_Group_Email],
(B15.Calculated_B_Key2) as [Calculated_Dist_Group_Key]
from B as B15
where (B15.Calculated_B_Key2 is not null)
group by (B15.Calculated_B_SalesRepName) as [Calculated_Dist_Group_SR],
(B15.Calculated_B_Email) as [Calculated_Dist_Group_Email],
(B15.Calculated_B_Key2) as [Calculated_Dist_Group_Key])
,[Report] AS
(select
(IsNull(B16.Calculated_B_SalesRepCode,'Sales')) as [Calculated_R_SalesRepCode],
(Sales_Group.Calculated_Sales_Group_SR) as [Calculated_R_SalesRepName],
(IsNull(B16.Calculated_B_InvoiceNum,9999999)) as [Calculated_R_InvoiceNum],
(IsNull(B16.Calculated_B_InvoiceDate, convert(date, '30991231', 112))) as [Calculated_R_InvoiceDate],
(B16.Calculated_B_PONum) as [Calculated_R_PONum],
(B16.Calculated_B_OrderTotal) as [Calculated_R_InvoiceTotal],
(B16.Calculated_B_CustID) as [Calculated_R_CustID],
(B16.Calculated_B_Cust) as [Calculated_R_Cust],
(B16.Calculated_B_RoleCode) as [Calculated_R_RoleCode],
(B16.Calculated_B_GroupCode) as [Calculated_R_GroupCode],
(B16.Calculated_B_BTCustID) as [Calculated_R_BTCustID],
(B16.Calculated_B_BTCust) as [Calculated_R_BTCust],
(B16.Calculated_B_BTGroupCode) as [Calculated_R_BTGroupCode],
(B16.Calculated_B_MRCode) as [Calculated_R_MRCode],
(B16.Calculated_B_MRName) as [Calculated_R_MRName],
(B16.Calculated_B_MRRoleCode) as [Calculated_R_MRRoleCode],
(Sales_Group.Calculated_Sales_Group_Email) as [Calculated_R_Email],
(B16.Calculated_B_Key) as [Calculated_R_Key],
(B16.Calculated_B_Key2) as [Calculated_R_Key2],
(Sales_BOM.Calculated_Sales_BOM_Sum) as [Calculated_R_SalesBOM],
(Sales_BOQ.Calculated_Sales_BOQ_Sum) as [Calculated_R_SalesBOQ],
(Sales_BOY.Calculated_Sales_BOY_Sum) as [Calculated_R_SalesBOY],
(NULL) as [Calculated_R_MRBOM],
(NULL) as [Calculated_R_MRBOQ],
(NULL) as [Calculated_R_MRBOY],
(NULL) as [Calculated_R_DistBOM],
(NULL) as [Calculated_R_DistBOQ],
(NULL) as [Calculated_R_DistBOY]
from Sales_Group as Sales_Group
left outer join B as B16 on
Sales_Group.Calculated_Sales_Group_SR = B16.Calculated_B_SalesRepName
and ( B16.Calculated_B_Key is null and B16.Calculated_B_Key2 is null and B16.Calculated_B_InvoiceDate >= B16.Calculated_B_BOM and B16.Calculated_B_InvoiceDate <= B16.Calculated_B_Today )
left outer join Sales_BOM as Sales_BOM on
Sales_Group.Calculated_Sales_Group_SR = Sales_BOM.Calculated_Sales_BOM_Rep
left outer join Sales_BOQ as Sales_BOQ on
Sales_Group.Calculated_Sales_Group_SR = Sales_BOQ.Calculated_Sales_BOQ_Rep
left outer join Sales_BOY as Sales_BOY on
Sales_Group.Calculated_Sales_Group_SR = Sales_BOY.Calculated_Sales_BOY_Rep
UNION
select
(B17.Calculated_B_SalesRepCode) as [Calculated_C_1],
(MR_Group.Calculated_MR_Group_SR) as [Calculated_C_2],
(B17.Calculated_B_InvoiceNum) as [Calculated_C_3],
(B17.Calculated_B_InvoiceDate) as [Calculated_C_4],
(B17.Calculated_B_PONum) as [Calculated_C_5],
(B17.Calculated_B_OrderTotal) as [Calculated_C_6],
(B17.Calculated_B_CustID) as [Calculated_C_9],
(B17.Calculated_B_Cust) as [Calculated_C_10],
(B17.Calculated_B_RoleCode) as [Calculated_C_13],
(B17.Calculated_B_GroupCode) as [Calculated_C_14],
(B17.Calculated_B_BTCustID) as [Calculated_C_15],
(B17.Calculated_B_BTCust) as [Calculated_C_16],
(B17.Calculated_B_BTGroupCode) as [Calculated_C_17],
(B17.Calculated_B_MRCode) as [Calculated_C_20],
(IsNull(B17.Calculated_B_MRName,MR_BOY.Calculated_MR_BOY_MR)) as [Calculated_C_21],
(B17.Calculated_B_MRRoleCode) as [Calculated_C_22],
(MR_Group.Calculated_MR_Group_Email) as [Calculated_C_23],
(MR_Group.Calculated_MR_Group_Key) as [Calculated_C_24],
(B17.Calculated_B_Key2) as [Calculated_C_25],
(Sales_BOM2.Calculated_Sales_BOM_Sum) as [Calculated_C_27],
(Sales_BOQ2.Calculated_Sales_BOQ_Sum) as [Calculated_C_28],
(Sales_BOY2.Calculated_Sales_BOY_Sum) as [Calculated_C_29],
(MR_BOM.Calculated_MR_BOM_Sum) as [Calculated_C_31],
(MR_BOQ.Calculated_MR_BOQ_Sum) as [Calculated_C_32],
(MR_BOY.Calculated_MR_BOY_Sum) as [Calculated_C_33],
(NULL) as [Calculated_C_35],
(NULL) as [Calculated_C_36],
(NULL) as [Calculated_C_37]
from MR_Group as MR_Group
left outer join B as B17 on
MR_Group.Calculated_MR_Group_Key = B17.Calculated_B_Key
and ( B17.Calculated_B_InvoiceDate >= B17.Calculated_B_BOM and B17.Calculated_B_InvoiceDate <= B17.Calculated_B_Today )
left outer join MR_BOM as MR_BOM on
MR_Group.Calculated_MR_Group_Key = MR_BOM.Calculated_MR_BOM_Key
left outer join MR_BOQ as MR_BOQ on
MR_Group.Calculated_MR_Group_Key = MR_BOQ.Calculated_MR_BOQ_Key
left outer join MR_BOY as MR_BOY on
MR_Group.Calculated_MR_Group_Key = MR_BOY.Calculated_MR_BOY_Key
left outer join Sales_BOM as Sales_BOM2 on
MR_Group.Calculated_MR_Group_SR = Sales_BOM2.Calculated_Sales_BOM_Rep
left outer join Sales_BOQ as Sales_BOQ2 on
MR_Group.Calculated_MR_Group_SR = Sales_BOQ2.Calculated_Sales_BOQ_Rep
left outer join Sales_BOY as Sales_BOY2 on
MR_Group.Calculated_MR_Group_SR = Sales_BOY2.Calculated_Sales_BOY_Rep
UNION
select
(B18.Calculated_B_SalesRepCode) as [Calculated_D_1],
(Dist_Group.Calculated_Dist_Group_SR) as [Calculated_D_2],
(B18.Calculated_B_InvoiceNum) as [Calculated_D_3],
(B18.Calculated_B_InvoiceDate) as [Calculated_D_4],
(B18.Calculated_B_PONum) as [Calculated_D_5],
(B18.Calculated_B_OrderTotal) as [Calculated_D_6],
(B18.Calculated_B_CustID) as [Calculated_D_9],
(B18.Calculated_B_Cust) as [Calculated_D_10],
(B18.Calculated_B_RoleCode) as [Calculated_D_13],
(B18.Calculated_B_GroupCode) as [Calculated_D_14],
(IsNull(B18.Calculated_B_BTCustID,Dist_BOY.Calculated_Dist_BOY_ID)) as [Calculated_D_15],
(IsNull(B18.Calculated_B_BTCust,Dist_BOY.Calculated_Dist_BOYDist)) as [Calculated_D_16],
(IsNull(B18.Calculated_B_BTGroupCode,Dist_BOY.Calculated_Dist_BOY_Group)) as [Calculated_D_17],
(B18.Calculated_B_MRCode) as [Calculated_D_20],
(B18.Calculated_B_MRName) as [Calculated_D_21],
(B18.Calculated_B_MRRoleCode) as [Calculated_D_22],
(Dist_Group.Calculated_Dist_Group_Email) as [Calculated_D_23],
(B18.Calculated_B_Key) as [Calculated_D_24],
(Dist_Group.Calculated_Dist_Group_Key) as [Calculated_D_25],
(Sales_BOM1.Calculated_Sales_BOM_Sum) as [Calculated_D_27],
(Sales_BOQ1.Calculated_Sales_BOQ_Sum) as [Calculated_D_28],
(Sales_BOY1.Calculated_Sales_BOY_Sum) as [Calculated_D_29],
(NULL) as [Calculated_D_31],
(NULL) as [Calculated_D_32],
(NULL) as [Calculated_D_33],
(Dist_BOM.Calculated_Dist_BOM_Sum) as [Calculated_D_35],
(Dist_BOQ.Calculated_Dist_BOQ_Sum) as [Calculated_D_36],
(Dist_BOY.Calculated_Dist_BOY_Sum) as [Calculated_D_37]
from Dist_Group as Dist_Group
left outer join B as B18 on
Dist_Group.Calculated_Dist_Group_Key = B18.Calculated_B_Key2
and ( B18.Calculated_B_InvoiceDate >= B18.Calculated_B_BOM and B18.Calculated_B_InvoiceDate <= B18.Calculated_B_Today )
left outer join Dist_BOM as Dist_BOM on
Dist_Group.Calculated_Dist_Group_Key = Dist_BOM.Calculated_Dist_BOM_Key
left outer join Dist_BOQ as Dist_BOQ on
Dist_Group.Calculated_Dist_Group_Key = Dist_BOQ.Calculated_Dist_BOQ_Key
left outer join Dist_BOY as Dist_BOY on
Dist_Group.Calculated_Dist_Group_Key = Dist_BOY.Calculated_Dist_BOY_Key
left outer join Sales_BOM as Sales_BOM1 on
Dist_Group.Calculated_Dist_Group_SR = Sales_BOM1.Calculated_Sales_BOM_Rep
left outer join Sales_BOQ as Sales_BOQ1 on
Dist_Group.Calculated_Dist_Group_SR = Sales_BOQ1.Calculated_Sales_BOQ_Rep
left outer join Sales_BOY as Sales_BOY1 on
Dist_Group.Calculated_Dist_Group_SR = Sales_BOY1.Calculated_Sales_BOY_Rep)
select
[Report].[Calculated_R_SalesRepCode] as [Calculated_R_SalesRepCode],
[Report].[Calculated_R_SalesRepName] as [Calculated_R_SalesRepName],
[Report].[Calculated_R_InvoiceNum] as [Calculated_R_InvoiceNum],
[Report].[Calculated_R_InvoiceDate] as [Calculated_R_InvoiceDate],
[Report].[Calculated_R_PONum] as [Calculated_R_PONum],
[Report].[Calculated_R_InvoiceTotal] as [Calculated_R_InvoiceTotal],
[Report].[Calculated_R_CustID] as [Calculated_R_CustID],
[Report].[Calculated_R_Cust] as [Calculated_R_Cust],
[Report].[Calculated_R_RoleCode] as [Calculated_R_RoleCode],
[Report].[Calculated_R_GroupCode] as [Calculated_R_GroupCode],
[Report].[Calculated_R_BTCustID] as [Calculated_R_BTCustID],
[Report].[Calculated_R_BTCust] as [Calculated_R_BTCust],
[Report].[Calculated_R_BTGroupCode] as [Calculated_R_BTGroupCode],
[Report].[Calculated_R_MRCode] as [Calculated_R_MRCode],
[Report].[Calculated_R_MRName] as [Calculated_R_MRName],
[Report].[Calculated_R_MRRoleCode] as [Calculated_R_MRRoleCode],
[Report].[Calculated_R_Email] as [Calculated_R_Email],
[Report].[Calculated_R_Key] as [Calculated_R_Key],
[Report].[Calculated_R_Key2] as [Calculated_R_Key2],
[Report].[Calculated_R_SalesBOM] as [Calculated_R_SalesBOM],
[Report].[Calculated_R_SalesBOQ] as [Calculated_R_SalesBOQ],
[Report].[Calculated_R_SalesBOY] as [Calculated_R_SalesBOY],
[Report].[Calculated_R_MRBOM] as [Calculated_R_MRBOM],
[Report].[Calculated_R_MRBOQ] as [Calculated_R_MRBOQ],
[Report].[Calculated_R_MRBOY] as [Calculated_R_MRBOY],
[Report].[Calculated_R_DistBOM] as [Calculated_R_DistBOM],
[Report].[Calculated_R_DistBOQ] as [Calculated_R_DistBOQ],
[Report].[Calculated_R_DistBOY] as [Calculated_R_DistBOY]
from Report as Report
答案 0 :(得分:1)
a)寻找简化查询的部分。例如,这部分可以大大简化:
(case
when DatePart(month,Constants.Today) = 1 then Constants.FirstDayOfMonth
when DatePart(month,Constants.Today) = 2 then DateAdd(month,-1,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 3 then DateAdd(month,-2,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 4 then DateAdd(month,-3,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 5 then DateAdd(month,-4,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 6 then DateAdd(month,-5,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 7 then DateAdd(month,-6,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 8 then DateAdd(month,-7,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 9 then DateAdd(month,-8,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 10 then DateAdd(month,-9,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 11 then DateAdd(month,-10,Constants.FirstDayOfMonth)
else DateAdd(month,-11,Constants.FirstDayOfMonth)
end) as [Calculated_BOY],
可以改为
(case
when DatePart(month,Constants.Today) = 12 then Constants.FirstDayOfMonth
else DateAdd(month, - ( DatePart(month,Constants.Today) - 1), Constants.FirstDayOfMonth) end ) as [Calculated_BOY]
b)将子选择分成临时表,特别是那些你可以验证产生相对较小的结果集的表(让我们说数千行或更少)。将这些临时表提供给select而不是subselects将有助于查询处理器
答案 1 :(得分:0)
我真的很想知道是什么
[Ice].entry(1,SalesRepList,'~')
如果它是一个函数,你可以使用它。 但它似乎是创造恒定值,而不是每行动态
我看到查询是由多个CTE表达式
组成的报告CTE由UNION组成,如果可能,可以转换为UNION ALL
许多GROUP By用于计算 也许那些可以使用PARTITION BY子句
转换为带有SUM()函数的新聚合语法