How to properly index sql server table to increase query performance - sql-server

i need to add correct indexes to a temp table to speed up a query. according to msdn i check the menu "Include Actual Execution Plan" and run the query in SSMC, after the execution i can see the query plan but there's no indication about the indexes to create.
in the table #Tax the fields l1,l2....l10 identify the position of the element within a hierarchical structure:
the dummy data in the sample is representing the following structure:
--node idleaf=1
----child idleaf=3 of 1
--------child idleaf=5 of 3
----child idleaf=4 of 1
--node idleaf=2
with the tremendous query i need to extract the list of all parents of each record starting form the top
ex:
this is the result of the query (omitting some field is not interesting for the sample)
idleaf,description,node_path
1 node idleaf=1 1_________
3 child idleaf=3 of 1 1_3________
5 child idleaf=5 of 3 1_3_5_______
4 child idleaf=4 of 1 1_4________
2 node idleaf=2 2_________
query:
Create Table #Tax (IdLeaf int,l1 int,l2 int ,l3 int ,l4 int ,l5 int ,l6 int
,l7 int ,l8 int ,l9 int ,l10 int ,Descrizione varchar(max), FlagDocumento bit)
INSERT INTO #Tax (IdLeaf,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,Descrizione,FlagDocumento)
VALUES (1,1,0,0,0,0,0,0,0,0,0,'node idleaf=1',1)
INSERT INTO #Tax (IdLeaf,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,Descrizione,FlagDocumento)
VALUES (2,2,0,0,0,0,0,0,0,0,0,'node idleaf=2',1)
INSERT INTO #Tax (IdLeaf,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,Descrizione,FlagDocumento)
VALUES (3,1,1,0,0,0,0,0,0,0,0,'child idleaf=3 of 1',1)
INSERT INTO #Tax (IdLeaf,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,Descrizione,FlagDocumento)
VALUES (4,1,2,0,0,0,0,0,0,0,0,'child idleaf=4 of 1',1)
INSERT INTO #Tax (IdLeaf,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,Descrizione,FlagDocumento)
VALUES (5,1,1,1,0,0,0,0,0,0,0,'child idleaf=5 of 3',1)
SELECT Levels.IdLeaf , Levels.Descrizione,Levels.FlagDocumento,
CASE FlagDocumento WHEN 1 Then
ISNULL(CONVERT(varchar(10),Levels.Level1),'') + '_' +
ISNULL(CONVERT(varchar(10),Levels.Level2),'') + '_' +
ISNULL(CONVERT(varchar(10),Levels.Level3),'') + '_' +
ISNULL(CONVERT(varchar(10),Levels.Level4),'') + '_' +
ISNULL(CONVERT(varchar(10),Levels.Level5),'') + '_' +
ISNULL(CONVERT(varchar(10),Levels.Level6),'') + '_' +
ISNULL(CONVERT(varchar(10),Levels.Level7),'') + '_' +
ISNULL(CONVERT(varchar(10),Levels.Level8),'') + '_' +
ISNULL(CONVERT(varchar(10),Levels.Level9),'') + '_' +
ISNULL(CONVERT(varchar(10),Levels.Level10),'')
Else '' END AS NodePath,
CASE
When Levels.Level10>0 Then Levels.IdLeaf9
When Levels.Level9>0 Then Levels.IdLeaf8
When Levels.Level8>0 Then Levels.IdLeaf7
When Levels.Level7>0 Then Levels.IdLeaf6
When Levels.Level6>0 Then Levels.IdLeaf5
When Levels.Level5>0 Then Levels.IdLeaf4
When Levels.Level4>0 Then Levels.IdLeaf3
When Levels.Level3>0 Then Levels.IdLeaf2
When Levels.Level2>0 Then Levels.IdLeaf1
When Levels.Level1>0 Then null
END AS IdParent,
CASE
When Levels.Level10>0 Then 10
When Levels.Level9>0 Then 9
When Levels.Level8>0 Then 8
When Levels.Level7>0 Then 7
When Levels.Level6>0 Then 6
When Levels.Level5>0 Then 5
When Levels.Level4>0 Then 4
When Levels.Level3>0 Then 3
When Levels.Level2>0 Then 2
When Levels.Level1>0 Then 1
END AS TaxLevel
FROM
(
SELECT f10.*,
f1.IdLeaf AS IdLeaf1,
f2.IdLeaf AS IdLeaf2,
f3.IdLeaf AS IdLeaf3,
f4.IdLeaf AS IdLeaf4,
f5.IdLeaf AS IdLeaf5,
f6.IdLeaf AS IdLeaf6,
f7.IdLeaf AS IdLeaf7,
f8.IdLeaf AS IdLeaf8,
f9.IdLeaf AS IdLeaf9,
f10.IdLeaf AS IdLeaf10,
CONVERT(varchar(10), f1.IdLeaf) + '_' + CONVERT(varchar(10), f2.IdLeaf) + '_' + CONVERT(varchar(10), f3.IdLeaf) + '_' + CONVERT(varchar(10), f4.IdLeaf) + CONVERT(varchar(10), f5.IdLeaf) + CONVERT(varchar(10), f6.IdLeaf) + CONVERT(varchar(10), f7.IdLeaf) + CONVERT(varchar(10), f8.IdLeaf) + CONVERT(varchar(10), f9.IdLeaf) + CONVERT(varchar(10), f10.IdLeaf) + '_' AS NODE_PATH,
f1.IdLeaf AS 'Level1',
CASE WHEN f2.IdLeaf <> f1.IdLeaf THEN f2.IdLeaf END AS 'Level2',
CASE WHEN f3.IdLeaf <> f2.IdLeaf THEN f3.IdLeaf END AS 'Level3',
CASE WHEN f4.IdLeaf <> f3.IdLeaf THEN f4.IdLeaf END AS 'Level4',
CASE WHEN f5.IdLeaf <> f4.IdLeaf THEN f5.IdLeaf END AS 'Level5',
CASE WHEN f6.IdLeaf <> f5.IdLeaf THEN f6.IdLeaf END AS 'Level6',
CASE WHEN f7.IdLeaf <> f6.IdLeaf THEN f7.IdLeaf END AS 'Level7',
CASE WHEN f8.IdLeaf <> f7.IdLeaf THEN f8.IdLeaf END AS 'Level8',
CASE WHEN f9.IdLeaf <> f8.IdLeaf THEN f9.IdLeaf END AS 'Level9',
CASE WHEN f10.IdLeaf <> f9.IdLeaf THEN f10.IdLeaf END AS 'Level10'
FROM
(SELECT * FROM #Tax) f10
JOIN #Tax f9 ON f9.l1 = f10.l1
AND f9.l2 = f10.l2
AND f9.l3 = f10.l3
AND f9.l4 = f10.l4
AND f9.l5 = f10.l5
AND f9.l6 = f10.l6
AND f9.l7 = f10.l7
AND f9.l8 = f10.L8
AND f9.l9 = f10.l9
AND f9.l10 = 0
JOIN #Tax f8 ON f8.l1 = f10.l1
AND f8.l2 = f10.l2
AND f8.l3 = f10.l3
AND f8.l4 = f10.l4
AND f8.l5 = f10.l5
AND f8.l6 = f10.l6
AND f8.l7 = f10.l7
AND f8.l8 = f10.l8
AND f8.l9 = 0
AND f8.l10 = 0
JOIN #Tax f7 ON f7.l1 = f10.l1
AND f7.l2 = f10.l2
AND f7.l3 = f10.l3
AND f7.l4 = f10.l4
AND f7.l5 = f10.l5
AND f7.l6 = f10.l6
AND f7.l7 = f10.l7
AND f7.l8 = 0
AND f7.l9 = 0
AND f7.l10 = 0
JOIN #Tax f6 ON f6.l1 = f10.l1
AND f6.l2 = f10.l2
AND f6.l3 = f10.l3
AND f6.l4 = f10.l4
AND f6.l5 = f10.l5
AND f6.l6 = f10.l6
AND f6.l7 = 0
AND f6.l8 = 0
AND f6.l9 = 0
AND f6.l10 = 0
JOIN #Tax f5 ON f5.l1 = f10.l1
AND f5.l2 = f10.l2
AND f5.l3 = f10.l3
AND f5.l4 = f10.l4
AND f5.l5 = f10.l5
AND f5.l6 = 0
AND f5.l7 = 0
AND f5.l8 = 0
AND f5.l9 = 0
AND f5.l10 = 0
JOIN #Tax f4 ON f4.l1 = f10.l1
AND f4.l2 = f10.l2
AND f4.l3 = f10.l3
AND f4.l4 = f10.l4
AND f4.l5 = 0
AND f4.l6 = 0
AND f4.l7 = 0
AND f4.l8 = 0
AND f4.l9 = 0
AND f4.l10 = 0
JOIN #Tax f3 ON f3.l1 = f10.l1
AND f3.l2 = f10.l2
AND f3.l3 = f10.l3
AND f3.l4 = 0
AND f3.l5 = 0
AND f3.l6 = 0
AND f3.l7 = 0
AND f3.l8 = 0
AND f3.l9 = 0
AND f3.l10 = 0
JOIN #Tax f2 ON f2.l1 = f10.l1
AND f2.l2 = f10.l2
AND f2.l3 = 0
AND f2.l4 = 0
AND f2.l5 = 0
AND f2.l6 = 0
AND f2.l7 = 0
AND f2.l8 = 0
AND f2.l9 = 0
AND f2.l10 = 0
JOIN #Tax f1 ON f1.l1 = f10.l1
AND f1.l2 = 0
AND f1.l3 = 0
AND f1.l4 = 0
AND f1.l5 = 0
AND f1.l6 = 0
AND f1.l7 = 0
AND f1.l8 = 0
AND f1.l9 = 0
AND f1.l10 = 0
) Levels Order By L1,L2,L3,L4,L5,L6,L7,L8,L9,L10

Related

Why does this code has an error of incompatible array size?

*[I found that the error occurred at line 57, z2 = w12.a1 + b12; with incompatible array size,
May I know how to fix it? Basically the data is from the simulation on MATLAB Simulink, so the size of the array changes as we increase the inputs from the original code which only have 1 input type][1]
%% load training data (use training_data.mat attached file)
% initialize number of nodes, weights, biases, errors and gradients.
%epochs and mini-batch size
clc
data = [out.dutycycle out.voltage out.current]
sample=1000;
labels = data(1:sample,1);
y = labels'; %output vector
images = data(1:sample,2:3);
images(:,1) = images(:,1)/400;
images(:,2) = images(:,2)/100;
images = images'; %Input vectors
hn1 = 80; %Number of neurons in the first hidden layer
hn2 = 60; %Number of neurons in the second hidden layer
%Initializing weights and biases
w12 = randn(hn1,1000).*sqrt(2/1000);
w23 = randn(hn2,hn1)*sqrt(2/hn1);
w34 = randn(1,hn2)*sqrt(2/hn2);
b12 = randn(hn1,1);
b23 = randn(hn2,1);
b34 = randn(1,1);
%learning rate
eta = 0.0058;
%Initializing errors and gradients
error4 = zeros(1,1);
error3 = zeros(hn2,1);
error2 = zeros(hn1,1);
errortot4 = zeros(1,1);
errortot3 = zeros(hn2,1);
errortot2 = zeros(hn1,1);
grad4 = zeros(1,1);
grad3 = zeros(hn2,1);
grad2 = zeros(hn1,1);
epochs = 50;
m = 10; %Minibatch size
%% Training phase
for k = 1:epochs %Outer epoch loop
batches = 1;
for j = 1:sample/m
error4 = zeros(1,1);
error3 = zeros(hn2,1);
error2 = zeros(hn1,1);
errortot4 = zeros(1,1);
errortot3 = zeros(hn2,1);
errortot2 = zeros(hn1,1);
grad4 = zeros(1,1);
grad3 = zeros(hn2,1);
grad2 = zeros(hn1,1);
for i = batches:batches+m-1 %Loop over each minibatch
%Feed forward
a1 = images(:,i);
z2 = w12.*a1 + b12;
a2 = elu(z2);
z3 = w23*a2 + b23;
a3 = elu(z3);
z4 = w34*a3 + b34;
a4 = elu(z4); %Output vector
%backpropagation
error4 = (a4-y(:,i)).*elup(z4);
error3 = (w34'*error4).*elup(z3);
error2 = (w23'*error3).*elup(z2);
errortot4 = errortot4 + error4;
errortot3 = errortot3 + error3;
errortot2 = errortot2 + error2;
grad4 = grad4 + error4*a3';
grad3 = grad3 + error3*a2';
grad2 = grad2 + error2*a1';
end
%Gradient descent
w34 = w34 - eta/m*grad4;
w23 = w23 - eta/m*grad3;
w12 = w12 - eta/m*grad2;
b34 = b34 - eta/m*errortot4;
b23 = b23 - eta/m*errortot3;
b12 = b12 - eta/m*errortot2;
batches = batches + m;
end
fprintf('Epochs:');
disp(k) %Track number of epochs
[images,y] = shuffle(images,y); %Shuffles order of the images for next epoch
end
disp('Training done!')
%note : create folder for this experiment and save the parameters.
% don't forget to keep the folder in matlab path!
save('wfour.mat','w34');
save('wthree.mat','w23');
save('wtwo.mat','w12');
save('bfour.mat','b34');
save('bthree.mat','b23');
save('btwo.mat','b12');
%% Testing phase
% load testing data with labels ... (use testing_data.mat attached file)
testsample = 100;
labels = test(1:testsample,1);
y = labels';
images = test(1:testsample,2:3);
images(:,1) = images(:,1)/400;
images(:,2) = images(:,2)/100;%copy
images = images';
we34 = matfile('wfour.mat');
w4 = we34.w34;
we23 = matfile('wthree.mat');
w3 = we23.w23;
we12 = matfile('wtwo.mat');
w2 = we12.w12;
bi34 = matfile('bfour.mat');
b4 = bi34.b34;
bi23 = matfile('bthree.mat');
b3 = bi23.b23;
bi12 = matfile('btwo.mat');
b2 = bi12.b12;
success = 0;
n = testsample;
for i = 1:n
out2 = elu(w2*images(:,i)+b2);
out3 = elu(w3*out2+b3);
out = elu(w4*out3+b4);
big = 0;
num = 0;
for k = 1:10
if out(k) > big
num = k-1;
big = out(k);
end
end
if labels(i) == num
success = success + 1;
end
end
fprintf('Accuracy: ');
fprintf('%f',success/n*100);
disp(' %');
%% ELU activation function
function fr = elu(x)
f = zeros(length(x),1);
for i = 1:length(x)
if x(i)>=0
f(i) = x(i);
else
f(i) = 0.2*(exp(x(i))-1);
end
end
fr = f;
end
%% derivative of the ELU activation function
function fr = elup(x)
f = zeros(length(x),1);
for i = 1:length(x)
if x(i)>=0
f(i) = 1;
else
f(i) = 0.2*exp(x(i));
end
end
fr = f;
end
%% SHuffle function
function [B,v] = shuffle(A,y)
cols = size(A,2);
P = randperm(cols);
B = A(:,P);
v = y(:,P);
end
Your help is much appreciated, thank you
MATLAB Error :
Arrays have incompatible sizes for this operation.
Error in ANN_PV_Array (line 57)
z2 = w12.*a1 + b12;

Writing For loop for summation of B in Matlab

the summation to be written in a for loop is here
TCPC = csvread ( 'tcpc.csv' ) ;
TCPC ( : , 3) = TCPC ( : , 3 ) * (100000); %unit conversion
TCPC ( : , 5) = TCPC ( : , 5 ) * (1e-6); %unit conversion
%------------------------------------------------------------------------
% Within the outer loop we calculate Bii's Psat for all components
for i = 1 : n
W(i,i) = TCPC (SP(i),1) ;
TC(i,i) = TCPC (SP(i),2);
PC(i,i) = TCPC (SP(i),3);
ZC(i,i) = TCPC (SP(i),4);
VC(i,i) = TCPC (SP(i),5);
TR(i,i) = T/TC(i,i);
Bi(i,i) = (R*TC(i,i)/PC(i,i))*((.083-(.422/(TR(i,i)^(1.6))))+(W(i,i)*(.139-
((.0172/(TR(i,i)^4.2))))));
phisat(i) = exp((Bi(i,i)*P)/(R*T)) ;
for j = i+1 : n % Within the inner loop we calculate Bij's (mixed coefficients)
W(i,j) = (TCPC (SP(i),1) + TCPC (SP(j),1)) / 2 ;
TC(i,j) = sqrt((TCPC (SP(i),2) * (TCPC (SP(j),2)))) ;
ZC(i,j) = ((TCPC (SP(i),4) * (TCPC (SP(j),4))))/2 ;
VC(i,j) = (((TCPC (SP(i),5)^(1/3) * (TCPC (SP(j),5)^(1/3))))/2)^(3) ;
PC(i,j) = ((ZC(i,j) * R * (TC(i,j)))/VC(i,j)) ;
Bi(i,j) = ((R*TC(i,j))/PC(i,j))*((.083-(.422/(TR(i,i)^(1.6))))+(W(i,j)*(.139-
((.0172/(TR(i,i)^4.2))))));
Bi(j,i)=Bi(i,j); % as Bij=Bji we write the inner loop for j = i+1 : n and assigned
the values to rest
end
end
for i = 1 : n
B(i,i) = ((X(i))^2)*Bi(i,i);
a = sum(B(i,i));
for j = i+1 : n
B(i,j) = X(i)*X(j)*Bi(i,j);
B(j,i) = B(i,j);
b = sum(B(i,j)); c = sum(B(j,I));
end
end
Bfinal = a+b+c
%------------------------------------------------------------------------
% Having calculated all the second virial coefficients,
% we calculate the fugacity coefficients for all the components of the mixture
% use phi bar 1-3 and phi 1-3
for i = 1:n
for j = i+1 : n
phi(i,j) = exp((((-Bfinal+2*X(i)Bi(i) + 2X(j)))P)/(RT));
end
end

Which file stores data from an application in VB?

I'm having trouble identifying and opening the file that contains the data of an application programmed in old VB.
I found in source code a subroutine that opens location and data upload files:
***** LOCATION SUBJECT AND LOADING TEST DATA *********
LOADING:
GoSub ABREARQ
If Val(BAKCP$) >= Val(NI$) And Val(BAKCP$) <= Val(NF$) Then
GoSub LIMPA
Else
GoSub LIMPA
Get #1, 1, GEALL: ORD1& = Val(GEALL.FB)
EX& = 0
Open Path$ + "NSALL.IND" For Random Shared As #5 Len = Len(NSALL)
NR& = LOF(5) / Len(NSALL)
If ORD1& > (NR& + 2) Then
For IX& = ORD1& - 1 To NR& + 2 Step -1
Get #1, IX&, GEALL
If Val(BAKCP$) >= Val(GEALL.NI) And Val(BAKCP$) <= Val(GEALL.NF) Then
EX& = IX&: IX& = NR& + 2
End If
Next
End If
If EX& = 0 Then
BR& = 1: TR& = NR&
Do Until (TR& < BR&)
MR& = Int((TR& + BR&) / 2)
Get #5, MR&, NSALL
If Val(BAKCP$) >= NSALL.NSI And Val(BAKCP$) <= NSALL.NSF Then
EX& = NSALL.RE
Exit Do
ElseIf Val(BAKCP$) > NSALL.NSI Then
BR& = MR& + 1
Else
TR& = MR& - 1
End If
Loop
End If
Close #5
If EX& <> 0 Then
Get #1, EX&, GEALL
FB$ = GEALL.FB: NI$ = GEALL.NI: NF$ = GEALL.NF: NP$ = GEALL.NP: NP0$ = GEALL.NP
REGDT& = EX& - 1: REGRS& = Val(GEALL.RS)
GoSub CARGADT
MSG$ = ""
Else
For Y = 1 To 12: For X = 1 To DT%(0, Y): DT$(X, Y) = "": Next: Next
FB$ = "": NI$ = "": NF$ = "": NP$ = "": NP0$ = ""
REGDT& = 0: REGRS& = 0
MSG$ = " N„o cadastrado/inex.! "
BAKCP$ = " ": Close: Return
End If
End If
Here is the routine that opens the files:
ABREARQ:
Open Path$ + "GEALL.ENS" For Random Shared As #1 Len = Len(GEALL)
Open Path$ + "DTALL.ENS" For Random Shared As #2 Len = Len(DTALL)
Open Path$ + "RSALL.ENS" For Random Shared As #3 Len = Len(DR)
Open Path$ + "CFALL.ENS" For Random Shared As #4 Len = 1080
FIELD #4, 1075 AS CFALL$, 5 AS PROX$
Return
I think the memory files are those with .ENS extension, but when I open them in text format, random and messy numbers appear.
Is there another way to open these ENS files?
Thanks in advance

Optimize My Sql

I have a query in Epicor that I built using the BAQ Designer (which is just a gui for creating a SQL query). I recently made a change to add more data and now receive the following error.
The query processor ran out of internal resources and could not produce a query plan. This is a rare event and only expected for extremely complex queries or queries that reference a very large number of tables or partitions. Please simplify the query.
I'm sure the monster I created is not optimal and I could probably make some changes to make it better. If any of the experts could take a look and help it would be greatly appreciated as I am not a SQL expert, just good enough to break stuff.The SQL is:
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
a) Look for simplifying parts of the query. For example, this part can be vastly simplified:
(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],
Could be instead
(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) Separate the subselects into temporary tables, particularly those that you can verify produce relatively small resultsets (let's say thousand of rows or less). Providing those temporary tables to the select instead of subselects will help the query processor
I really wonder what is
[Ice].entry(1,SalesRepList,'~')
if it is a function, you can work on it.
But it seems to be creating constant values, not dynamic per row
I see that the query is formed of multiple CTE expressions
The Report CTE is formed of UNIONs can be converted to UNION ALLs if possible
And many GROUP By for calculations
Perhaps those can be converted to new aggregation syntax with SUM() function using PARTITION BY clause

Open read columns of each line in text file and do (windows)?

This is the Fortran code that I modified.
The problem is the code can not open the text file.
I am getting the following error:
The message is: RunCode: Fail to run.. At line 123 of file <unit = 90, file = 'cunnette_xyz.txt'> Fortran runtime error: End of file
The file is below in 4368 rows (decimals are the same for three columns):
602340.440000,129706.190000,28.892939
602340.880000,129706.390000,28.955128
602884.500000,128780.700000,29.876873
602884.380000,128781.190000,29.875114
602884.250000,128781.660000,29.885448
602884.130000,128782.150000,29.895996
602883.940000,128782.630000,29.899380
602883.810000,128783.120000,29.903221
602883.690000,128783.590000,29.907070
602883.560000,128784.080000,29.910931
602883.440000,128784.560000,29.914803
602883.310000,128785.050000,29.918684
602883.190000,128785.520000,29.922575
602883.060000,128786.010000,29.926479
602882.940000,128786.490000,29.930393
602882.810000,128786.980000,29.934317
602882.630000,128787.450000,29.938253
602882.500000,128787.940000,29.942202
.
.
.
the program is:
----------------------------
USE BIEF
USE DECLARATIONS_TELEMAC2D
IMPLICIT NONE
INTEGER LNG,LU, K, ITRAC,I, NSOM,J, NLINE, NDOWN
DOUBLE PRECISION, PARAMETER:: BATHY_RADIER_up= 29.84D0
DOUBLE PRECISION, PARAMETER:: DEPTH_up = 2.15D0
REAL A(5000),B(5000),C(5000)
DOUBLE PRECISION XPOLYD(14), YPOLYD(14), INPOLYD(14)
COMMON/INFO/LNG,LU
DOUBLE PRECISION XPOLY(6), YPOLY(6),COTE_RADIER_up
NSOM = 6
XPOLY(1) = 602883.13
XPOLY(2) = 602886.15
XPOLY(3) = 602887.15
XPOLY(4) = 602905.46
XPOLY(5) = 602902.52
XPOLY(6) = 602884.13
YPOLY(1) = 128779.99
YPOLY(2) = 128780.80
YPOLY(3) = 128777.12
YPOLY(4) = 128741.21
YPOLY(5) = 128739.75
YPOLY(6) = 128775.96
AT = 0.D0
CALL OS( 'X=0 ' , X=U )
CALL OS( 'X=0 ' , X=V )
IF(CDTINI(1:10).EQ.'COTE NULLE'.OR.
* CDTINI(1:14).EQ.'ZERO ELEVATION') THEN
CALL OS( 'X=C ' , H , H , H , 0.D0 )
CALL OS( 'X=X-Y ' , H , ZF , H , 0.D0 )
ELSEIF(CDTINI(1:14).EQ.'COTE CONSTANTE'.OR.
* CDTINI(1:18).EQ.'CONSTANT ELEVATION') THEN
CALL OS( 'X=C ' , H , H , H , COTINI )
CALL OS( 'X=X-Y ' , H , ZF , H , 0.D0 )
ELSEIF(CDTINI(1:13).EQ.'HAUTEUR NULLE'.OR.
* CDTINI(1:10).EQ.'ZERO DEPTH') THEN
CALL OS( 'X=C ' , H , H , H , 0.D0 )
ELSEIF(CDTINI(1:17).EQ.'HAUTEUR CONSTANTE'.OR.
* CDTINI(1:14).EQ.'CONSTANT DEPTH') THEN
CALL OS( 'X=C ' , H , H , H , HAUTIN )
ELSEIF(CDTINI(1:13).EQ.'PARTICULIERES'.OR.
* CDTINI(1:10).EQ.'PARTICULAR'.OR.
* CDTINI(1:07).EQ.'SPECIAL') THEN
COTE_RADIER_up = BATHY_RADIER_up + DEPTH_up
input for coordinates of the downstream
NDOWN = 14
XPOLYD(1) = 602883.13
XPOLYD(2) = 602886.15
XPOLYD(3) = 602864.47
XPOLYD(4) = 602837.90
XPOLYD(5) = 602821.91
XPOLYD(6) = 602649.77
XPOLYD(7) = 602634.35
XPOLYD(8) = 602345.08
XPOLYD(9) = 602326.07
XPOLYD(10) = 602619.31
XPOLYD(11) = 602638.33
XPOLYD(12) = 602811.64
XPOLYD(13) = 602831.52
XPOLYD(14) = 602857.16
YPOLYD(1) = 128779.99
YPOLYD(2) = 128780.80
YPOLYD(3) = 128867.74
YPOLYD(4) = 128936.74
YPOLYD(5) = 128953.95
YPOLYD(6) = 129105.43
YPOLYD(7) = 129143.43
YPOLYD(8) = 129713.38
YPOLYD(9) = 129708.26
YPOLYD(10) = 129136.41
YPOLYD(11) = 129094.72
YPOLYD(12) = 128941.16
YPOLYD(13) = 128931.09
YPOLYD(14) = 128865.81
DO 10 J=1,NPOIN
IF(INPOLY(X(J),Y(J),XPOLY,YPOLY,NSOM)) THEN
write(lu,*) 'upstream is recognized....'
H%R(J)=MAX(0.D0,COTE_RADIER_up-ZF%R(J))
U%R(J)=0.0D0
ELSE
PRINT *, 'opening file'
OPEN(unit =90, FILE = 'cunnette_xyz.txt', FORM ='FORMATTED')
PRINT *, 'read now'
READ (90, *)NLINE
PRINT *, 'DO now'
READ (90, *) A(K),B(K),C(K)
IF(INPOLY(X(J),Y(J),XPOLYD,YPOLYD,NDOWN)) THEN
DO K=1,NLINE
H%R(K)=0.45D0
U%R(K)=0.D0
ENDDO
ELSE
H%R(J)=0.0D0
U%R(J)=0.0D0
ENDIF
ENDIF
write(lu,*) 'downstream ....',K,H%r(K)
10 CONTINUE
ELSE
IF(LNG.EQ.1) THEN
WRITE(LU,*) 'CONDIN : CONDITION INITIALE NON PREVUE : ',CDTINI
ENDIF
IF(LNG.EQ.2) THEN
WRITE(LU,*) 'CONDIN: INITIAL CONDITION UNKNOWN: ',CDTINI
ENDIF
STOP
ENDIF
IF(NTRAC.GT.0) THEN
DO ITRAC=1,NTRAC
CALL OS( 'X=C ' , X=T%ADR(ITRAC)%P , C=TRAC0(ITRAC) )
ENDDO
ENDIF
CALL OS( 'X=C ' , VISC , VISC , VISC , PROPNU )
RETURN
END

Resources