A
Alex
I have a table with products and territories. The same product can be for
different territories. But, a combination of Product_ID and Territory is
unique in the table.
To don’t transfer a product for a territory where quantities for all months
are equal 0, I had a query as follows:
INSERT INTO tblTable1 ( Product_ID, Description, TerritoryID, Terr_Num,
Territory, ProdCateg_Code, ProdCateg, AdjY1F, Aug, FAug, Sep, FSep, Oct,
FOct, Nov, FNov, [Dec], FDec, Jan, FJan, Feb, FFeb, Mar, FMar, Apr, FApr,
May, FMay, Jun, FJun, Jul, FJul, AugNew, SepNew )
SELECT qryQueryFrom.Product_ID, qryQueryFrom.Description,
qryQueryFrom.TerritoryID, qryQueryFrom.Terr_Num, qryQueryFrom.Territory,
qryQueryFrom.ProdCateg_Code, qryQueryFrom.ProdCateg, qryQueryFrom.AdjY1F,
qryQueryFrom.Aug, qryQueryFrom.FAug, qryQueryFrom.Sep, qryQueryFrom.FSep,
qryQueryFrom.Oct, qryQueryFrom.FOct, qryQueryFrom.Nov, qryQueryFrom.FNov,
qryQueryFrom.Dec, qryQueryFrom.FDec, qryQueryFrom.Jan, qryQueryFrom.FJan,
qryQueryFrom.Feb, qryQueryFrom.FFeb, qryQueryFrom.Mar, qryQueryFrom.FMar,
qryQueryFrom.Apr, qryQueryFrom.FApr, qryQueryFrom.May, qryQueryFrom.FMay,
qryQueryFrom.Jun, qryQueryFrom.FJun, qryQueryFrom.Jul, qryQueryFrom.FJul,
qryQueryFrom.AugNew, qryQueryFrom.SepNew
FROM qryQueryFrom
WHERE (((qryQueryFrom.AdjY1F)>0)) OR (((qryQueryFrom.Aug)<>0)) OR
(((qryQueryFrom.Sep)<>0)) OR (((qryQueryFrom.Oct)<>0)) OR
(((qryQueryFrom.Nov)<>0)) OR (((qryQueryFrom.Dec)<>0)) OR
(((qryQueryFrom.Jan)<>0)) OR (((qryQueryFrom.Feb)<>0)) OR
(((qryQueryFrom.Mar)<>0)) OR (((qryQueryFrom.Apr)<>0)) OR
(((qryQueryFrom.May)<>0)) OR (((qryQueryFrom.Jun)<>0)) OR
(((qryQueryFrom.Jul)<>0)) OR (((qryQueryFrom.AugNew)<>0)) OR
(((qryQueryFrom.SepNew)<>0));
It was working well enough.
Then, I’ve changed it into a query to transfer the product for all
territories if at least for some territory the quantity is not equal 0 (to
allow a user to enter some qty later) as follows:
INSERT INTO tblTable1 ( Product_ID, Description, TerritoryID, Terr_Num,
Territory, ProdCateg_Code, ProdCateg, AdjY1F, Aug, FAug, Sep, FSep, Oct,
FOct, Nov, FNov, [Dec], FDec, Jan, FJan, Feb, FFeb, Mar, FMar, Apr, FApr,
May, FMay, Jun, FJun, Jul, FJul, AugNew, SepNew )
SELECT qryQueryFrom.Product_ID, qryQueryFrom.Description,
qryQueryFrom.TerritoryID, qryQueryFrom.Terr_Num, qryQueryFrom.Territory,
qryQueryFrom.ProdCateg_Code, qryQueryFrom.ProdCateg, qryQueryFrom.AdjY1F,
qryQueryFrom.Aug, qryQueryFrom.FAug, qryQueryFrom.Sep, qryQueryFrom.FSep,
qryQueryFrom.Oct, qryQueryFrom.FOct, qryQueryFrom.Nov, qryQueryFrom.FNov,
qryQueryFrom.Dec, qryQueryFrom.FDec, qryQueryFrom.Jan, qryQueryFrom.FJan,
qryQueryFrom.Feb, qryQueryFrom.FFeb, qryQueryFrom.Mar, qryQueryFrom.FMar,
qryQueryFrom.Apr, qryQueryFrom.FApr, qryQueryFrom.May, qryQueryFrom.FMay,
qryQueryFrom.Jun, qryQueryFrom.FJun, qryQueryFrom.Jul, qryQueryFrom.FJul,
qryQueryFrom.AugNew, qryQueryFrom.SepNew
FROM qryQueryFrom
WHERE (((qryQueryFrom.Product_ID) In (SELECT qryQueryFrom.Product_ID FROM
qryQueryFrom GROUP BY qryQueryFrom.Product_ID HAVING
Sum(qryQueryFrom.AdjY1F)>0 OR Sum(qryQueryFrom.Aug)<>0 OR
Sum(qryQueryFrom.Sep)<>0 OR Sum(qryQueryFrom.Oct)<>0 OR
Sum(qryQueryFrom.Nov)<>0 OR Sum(qryQueryFrom.Dec)<>0 OR
Sum(qryQueryFrom.Jan)<>0 OR Sum(qryQueryFrom.Feb)<>0 OR
Sum(qryQueryFrom.Mar)<>0 OR Sum(qryQueryFrom.Apr)<>0 OR
Sum(qryQueryFrom.May)<>0 OR Sum(qryQueryFrom.Jun)<>0 OR
Sum(qryQueryFrom.Jul)<>0 OR Sum(qryQueryFrom.AugNew)<>0 OR
Sum(qryQueryFrom.SepNew)<>0)));
But, the query is extremely slow. Could anybody advise how I could make it
better.
Thanks
different territories. But, a combination of Product_ID and Territory is
unique in the table.
To don’t transfer a product for a territory where quantities for all months
are equal 0, I had a query as follows:
INSERT INTO tblTable1 ( Product_ID, Description, TerritoryID, Terr_Num,
Territory, ProdCateg_Code, ProdCateg, AdjY1F, Aug, FAug, Sep, FSep, Oct,
FOct, Nov, FNov, [Dec], FDec, Jan, FJan, Feb, FFeb, Mar, FMar, Apr, FApr,
May, FMay, Jun, FJun, Jul, FJul, AugNew, SepNew )
SELECT qryQueryFrom.Product_ID, qryQueryFrom.Description,
qryQueryFrom.TerritoryID, qryQueryFrom.Terr_Num, qryQueryFrom.Territory,
qryQueryFrom.ProdCateg_Code, qryQueryFrom.ProdCateg, qryQueryFrom.AdjY1F,
qryQueryFrom.Aug, qryQueryFrom.FAug, qryQueryFrom.Sep, qryQueryFrom.FSep,
qryQueryFrom.Oct, qryQueryFrom.FOct, qryQueryFrom.Nov, qryQueryFrom.FNov,
qryQueryFrom.Dec, qryQueryFrom.FDec, qryQueryFrom.Jan, qryQueryFrom.FJan,
qryQueryFrom.Feb, qryQueryFrom.FFeb, qryQueryFrom.Mar, qryQueryFrom.FMar,
qryQueryFrom.Apr, qryQueryFrom.FApr, qryQueryFrom.May, qryQueryFrom.FMay,
qryQueryFrom.Jun, qryQueryFrom.FJun, qryQueryFrom.Jul, qryQueryFrom.FJul,
qryQueryFrom.AugNew, qryQueryFrom.SepNew
FROM qryQueryFrom
WHERE (((qryQueryFrom.AdjY1F)>0)) OR (((qryQueryFrom.Aug)<>0)) OR
(((qryQueryFrom.Sep)<>0)) OR (((qryQueryFrom.Oct)<>0)) OR
(((qryQueryFrom.Nov)<>0)) OR (((qryQueryFrom.Dec)<>0)) OR
(((qryQueryFrom.Jan)<>0)) OR (((qryQueryFrom.Feb)<>0)) OR
(((qryQueryFrom.Mar)<>0)) OR (((qryQueryFrom.Apr)<>0)) OR
(((qryQueryFrom.May)<>0)) OR (((qryQueryFrom.Jun)<>0)) OR
(((qryQueryFrom.Jul)<>0)) OR (((qryQueryFrom.AugNew)<>0)) OR
(((qryQueryFrom.SepNew)<>0));
It was working well enough.
Then, I’ve changed it into a query to transfer the product for all
territories if at least for some territory the quantity is not equal 0 (to
allow a user to enter some qty later) as follows:
INSERT INTO tblTable1 ( Product_ID, Description, TerritoryID, Terr_Num,
Territory, ProdCateg_Code, ProdCateg, AdjY1F, Aug, FAug, Sep, FSep, Oct,
FOct, Nov, FNov, [Dec], FDec, Jan, FJan, Feb, FFeb, Mar, FMar, Apr, FApr,
May, FMay, Jun, FJun, Jul, FJul, AugNew, SepNew )
SELECT qryQueryFrom.Product_ID, qryQueryFrom.Description,
qryQueryFrom.TerritoryID, qryQueryFrom.Terr_Num, qryQueryFrom.Territory,
qryQueryFrom.ProdCateg_Code, qryQueryFrom.ProdCateg, qryQueryFrom.AdjY1F,
qryQueryFrom.Aug, qryQueryFrom.FAug, qryQueryFrom.Sep, qryQueryFrom.FSep,
qryQueryFrom.Oct, qryQueryFrom.FOct, qryQueryFrom.Nov, qryQueryFrom.FNov,
qryQueryFrom.Dec, qryQueryFrom.FDec, qryQueryFrom.Jan, qryQueryFrom.FJan,
qryQueryFrom.Feb, qryQueryFrom.FFeb, qryQueryFrom.Mar, qryQueryFrom.FMar,
qryQueryFrom.Apr, qryQueryFrom.FApr, qryQueryFrom.May, qryQueryFrom.FMay,
qryQueryFrom.Jun, qryQueryFrom.FJun, qryQueryFrom.Jul, qryQueryFrom.FJul,
qryQueryFrom.AugNew, qryQueryFrom.SepNew
FROM qryQueryFrom
WHERE (((qryQueryFrom.Product_ID) In (SELECT qryQueryFrom.Product_ID FROM
qryQueryFrom GROUP BY qryQueryFrom.Product_ID HAVING
Sum(qryQueryFrom.AdjY1F)>0 OR Sum(qryQueryFrom.Aug)<>0 OR
Sum(qryQueryFrom.Sep)<>0 OR Sum(qryQueryFrom.Oct)<>0 OR
Sum(qryQueryFrom.Nov)<>0 OR Sum(qryQueryFrom.Dec)<>0 OR
Sum(qryQueryFrom.Jan)<>0 OR Sum(qryQueryFrom.Feb)<>0 OR
Sum(qryQueryFrom.Mar)<>0 OR Sum(qryQueryFrom.Apr)<>0 OR
Sum(qryQueryFrom.May)<>0 OR Sum(qryQueryFrom.Jun)<>0 OR
Sum(qryQueryFrom.Jul)<>0 OR Sum(qryQueryFrom.AugNew)<>0 OR
Sum(qryQueryFrom.SepNew)<>0)));
But, the query is extremely slow. Could anybody advise how I could make it
better.
Thanks