T
Tony Wainwright
Hi all
I am writing an application that examines sales for a company. However the
data structure is very simple but the queries are getting really
complicated - and to do the reporting that the company wants will take
around 800 queries to generate 152 reports. What I am doing at the moment
is writing 1 query each for current month, prior month, current YTD, prior
YTD, current rolling year, prior rolling year, and a query to show the
fields to show (qryAJoin). These are all used in the report query. The
Main entity (tblSoap) contains around 24,000 records across 30 attributes.
Below is a sample of one of the most complicated
CurrentMonth
==========
SELECT tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup, Sum(tblSoap.NetAmount) AS CurrentMonth
FROM tblSoap
WHERE (((tblSoap.Month)=[Forms]![frmReportParameters]![txtCurrentMonth]))
GROUP BY tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup;
PriorMonth
========
SELECT tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup, Sum(tblSoap.NetAmount) AS PriorMonth
FROM tblSoap
WHERE (((tblSoap.Month)=[Forms]![frmReportParameters]![txtPriorMonth]))
GROUP BY tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup
ORDER BY tblSoap.Market;
CurrentYTD
=========
SELECT tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup, Sum(tblSoap.NetAmount) AS CurrentYTD
FROM tblSoap
WHERE (((tblSoap.Month) Between
[Forms]![frmReportParameters]![txtCurrentYTD] And
[Forms]![frmReportParameters]![txtCurrentMonth]))
GROUP BY tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup
ORDER BY tblSoap.Market;
PriorYTD
=======
SELECT tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup, Sum(tblSoap.NetAmount) AS PriorYTD
FROM tblSoap
WHERE (((tblSoap.Month) Between [Forms]![frmReportParameters]![txtPriorYTD]
And [Forms]![frmReportParameters]![txtPriorMonth]))
GROUP BY tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup
ORDER BY tblSoap.Market;
CurrentRolling
==========
SELECT tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup, Sum(tblSoap.NetAmount) AS CurrentRolling
FROM tblSoap
WHERE (((tblSoap.Month) Between
[Forms]![frmReportParameters]![txtCurrentRolling] And
[Forms]![frmReportParameters]![txtCurrentMonth]))
GROUP BY tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup
ORDER BY tblSoap.Market;
PriorRolling
========
SELECT tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup, Sum(tblSoap.NetAmount) AS PriorRolling
FROM tblSoap
WHERE (((tblSoap.Month) Between
[Forms]![frmReportParameters]![txtPriorRolling] And
[Forms]![frmReportParameters]![txtPriorMonth]))
GROUP BY tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup
ORDER BY tblSoap.Market;
AJoin
====
SELECT luMarket.MarketID, luRegion.RegionID, tblSoap.AccountRef,
luDistributor.DistributorID, luMarket.Market, luRegion.RegionName,
tblSoap.Name, luDistributor.Distributor
FROM luDistributor INNER JOIN (luRegion INNER JOIN (luMarket INNER JOIN
tblSoap ON luMarket.MarketID = tblSoap.Market) ON luRegion.RegionID =
tblSoap.Region) ON luDistributor.DistributorID = tblSoap.DistGroup
GROUP BY luMarket.MarketID, luRegion.RegionID, tblSoap.AccountRef,
luDistributor.DistributorID, luMarket.Market, luRegion.RegionName,
tblSoap.Name, luDistributor.Distributor;
ReportQuery
=========
SELECT qryCustDeptDistEURegionAJoin.MarketID,
qryCustDeptDistEURegionAJoin.RegionID,
qryCustDeptDistEURegionAJoin.DistributorID,
qryCustDeptDistEURegionAJoin.AccountRef,
qryCustDeptDistEURegionAJoin.Market, qryCustDeptDistEURegionAJoin.Name,
qryCustDeptDistEURegionAJoin.RegionName,
qryCustDeptDistEURegionAJoin.Distributor,
qryCustDeptDistEURegionCurrentMonth.CurrentMonth,
qryCustDeptDistEURegionPriorMonth.PriorMonth,
qryCustDeptDistEURegionCurrentYTD.CurrentYTD,
qryCustDeptDistEURegionPriorYTD.PriorYTD,
qryCustDeptDistEURegionCurrentRolling12Month.CurrentRolling,
qryCustDeptDistEURegionPriorRolling12Month.PriorRolling
FROM (((((qryCustDeptDistEURegionAJoin LEFT JOIN
qryCustDeptDistEURegionCurrentMonth ON
(qryCustDeptDistEURegionAJoin.DistributorID =
qryCustDeptDistEURegionCurrentMonth.DistGroup) AND
(qryCustDeptDistEURegionAJoin.AccountRef =
qryCustDeptDistEURegionCurrentMonth.AccountRef) AND
(qryCustDeptDistEURegionAJoin.RegionID =
qryCustDeptDistEURegionCurrentMonth.Region) AND
(qryCustDeptDistEURegionAJoin.MarketID =
qryCustDeptDistEURegionCurrentMonth.Market)) LEFT JOIN
qryCustDeptDistEURegionPriorMonth ON
(qryCustDeptDistEURegionAJoin.DistributorID =
qryCustDeptDistEURegionPriorMonth.DistGroup) AND
(qryCustDeptDistEURegionAJoin.AccountRef =
qryCustDeptDistEURegionPriorMonth.AccountRef) AND
(qryCustDeptDistEURegionAJoin.RegionID =
qryCustDeptDistEURegionPriorMonth.Region) AND
(qryCustDeptDistEURegionAJoin.MarketID =
qryCustDeptDistEURegionPriorMonth.Market)) LEFT JOIN
qryCustDeptDistEURegionCurrentYTD ON
(qryCustDeptDistEURegionAJoin.DistributorID =
qryCustDeptDistEURegionCurrentYTD.DistGroup) AND
(qryCustDeptDistEURegionAJoin.AccountRef =
qryCustDeptDistEURegionCurrentYTD.AccountRef) AND
(qryCustDeptDistEURegionAJoin.RegionID =
qryCustDeptDistEURegionCurrentYTD.Region) AND
(qryCustDeptDistEURegionAJoin.MarketID =
qryCustDeptDistEURegionCurrentYTD.Market)) LEFT JOIN
qryCustDeptDistEURegionPriorYTD ON
(qryCustDeptDistEURegionAJoin.DistributorID =
qryCustDeptDistEURegionPriorYTD.DistGroup) AND
(qryCustDeptDistEURegionAJoin.AccountRef =
qryCustDeptDistEURegionPriorYTD.AccountRef) AND
(qryCustDeptDistEURegionAJoin.RegionID =
qryCustDeptDistEURegionPriorYTD.Region) AND
(qryCustDeptDistEURegionAJoin.MarketID =
qryCustDeptDistEURegionPriorYTD.Market)) LEFT JOIN
qryCustDeptDistEURegionCurrentRolling12Month ON
(qryCustDeptDistEURegionAJoin.DistributorID =
qryCustDeptDistEURegionCurrentRolling12Month.DistGroup) AND
(qryCustDeptDistEURegionAJoin.AccountRef =
qryCustDeptDistEURegionCurrentRolling12Month.AccountRef) AND
(qryCustDeptDistEURegionAJoin.RegionID =
qryCustDeptDistEURegionCurrentRolling12Month.Region) AND
(qryCustDeptDistEURegionAJoin.MarketID =
qryCustDeptDistEURegionCurrentRolling12Month.Market)) LEFT JOIN
qryCustDeptDistEURegionPriorRolling12Month ON
(qryCustDeptDistEURegionAJoin.DistributorID =
qryCustDeptDistEURegionPriorRolling12Month.DistGroup) AND
(qryCustDeptDistEURegionAJoin.AccountRef =
qryCustDeptDistEURegionPriorRolling12Month.AccountRef) AND
(qryCustDeptDistEURegionAJoin.RegionID =
qryCustDeptDistEURegionPriorRolling12Month.Region) AND
(qryCustDeptDistEURegionAJoin.MarketID =
qryCustDeptDistEURegionPriorRolling12Month.Market)
WHERE (((qryCustDeptDistEURegionAJoin.MarketID) Like
[Forms]![frmReportParameters]![cboEURMarket]) AND
((qryCustDeptDistEURegionAJoin.RegionID) Like
[Forms]![frmReportParameters]![cboEURDepartment]) AND
((qryCustDeptDistEURegionAJoin.DistributorID) Like
[Forms]![frmReportParameters]![cboEURProdDist]));
Is there anyway I could simplify this?
Cheers
Tony
I am writing an application that examines sales for a company. However the
data structure is very simple but the queries are getting really
complicated - and to do the reporting that the company wants will take
around 800 queries to generate 152 reports. What I am doing at the moment
is writing 1 query each for current month, prior month, current YTD, prior
YTD, current rolling year, prior rolling year, and a query to show the
fields to show (qryAJoin). These are all used in the report query. The
Main entity (tblSoap) contains around 24,000 records across 30 attributes.
Below is a sample of one of the most complicated
CurrentMonth
==========
SELECT tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup, Sum(tblSoap.NetAmount) AS CurrentMonth
FROM tblSoap
WHERE (((tblSoap.Month)=[Forms]![frmReportParameters]![txtCurrentMonth]))
GROUP BY tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup;
PriorMonth
========
SELECT tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup, Sum(tblSoap.NetAmount) AS PriorMonth
FROM tblSoap
WHERE (((tblSoap.Month)=[Forms]![frmReportParameters]![txtPriorMonth]))
GROUP BY tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup
ORDER BY tblSoap.Market;
CurrentYTD
=========
SELECT tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup, Sum(tblSoap.NetAmount) AS CurrentYTD
FROM tblSoap
WHERE (((tblSoap.Month) Between
[Forms]![frmReportParameters]![txtCurrentYTD] And
[Forms]![frmReportParameters]![txtCurrentMonth]))
GROUP BY tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup
ORDER BY tblSoap.Market;
PriorYTD
=======
SELECT tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup, Sum(tblSoap.NetAmount) AS PriorYTD
FROM tblSoap
WHERE (((tblSoap.Month) Between [Forms]![frmReportParameters]![txtPriorYTD]
And [Forms]![frmReportParameters]![txtPriorMonth]))
GROUP BY tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup
ORDER BY tblSoap.Market;
CurrentRolling
==========
SELECT tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup, Sum(tblSoap.NetAmount) AS CurrentRolling
FROM tblSoap
WHERE (((tblSoap.Month) Between
[Forms]![frmReportParameters]![txtCurrentRolling] And
[Forms]![frmReportParameters]![txtCurrentMonth]))
GROUP BY tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup
ORDER BY tblSoap.Market;
PriorRolling
========
SELECT tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup, Sum(tblSoap.NetAmount) AS PriorRolling
FROM tblSoap
WHERE (((tblSoap.Month) Between
[Forms]![frmReportParameters]![txtPriorRolling] And
[Forms]![frmReportParameters]![txtPriorMonth]))
GROUP BY tblSoap.Market, tblSoap.Region, tblSoap.AccountRef,
tblSoap.DistGroup
ORDER BY tblSoap.Market;
AJoin
====
SELECT luMarket.MarketID, luRegion.RegionID, tblSoap.AccountRef,
luDistributor.DistributorID, luMarket.Market, luRegion.RegionName,
tblSoap.Name, luDistributor.Distributor
FROM luDistributor INNER JOIN (luRegion INNER JOIN (luMarket INNER JOIN
tblSoap ON luMarket.MarketID = tblSoap.Market) ON luRegion.RegionID =
tblSoap.Region) ON luDistributor.DistributorID = tblSoap.DistGroup
GROUP BY luMarket.MarketID, luRegion.RegionID, tblSoap.AccountRef,
luDistributor.DistributorID, luMarket.Market, luRegion.RegionName,
tblSoap.Name, luDistributor.Distributor;
ReportQuery
=========
SELECT qryCustDeptDistEURegionAJoin.MarketID,
qryCustDeptDistEURegionAJoin.RegionID,
qryCustDeptDistEURegionAJoin.DistributorID,
qryCustDeptDistEURegionAJoin.AccountRef,
qryCustDeptDistEURegionAJoin.Market, qryCustDeptDistEURegionAJoin.Name,
qryCustDeptDistEURegionAJoin.RegionName,
qryCustDeptDistEURegionAJoin.Distributor,
qryCustDeptDistEURegionCurrentMonth.CurrentMonth,
qryCustDeptDistEURegionPriorMonth.PriorMonth,
qryCustDeptDistEURegionCurrentYTD.CurrentYTD,
qryCustDeptDistEURegionPriorYTD.PriorYTD,
qryCustDeptDistEURegionCurrentRolling12Month.CurrentRolling,
qryCustDeptDistEURegionPriorRolling12Month.PriorRolling
FROM (((((qryCustDeptDistEURegionAJoin LEFT JOIN
qryCustDeptDistEURegionCurrentMonth ON
(qryCustDeptDistEURegionAJoin.DistributorID =
qryCustDeptDistEURegionCurrentMonth.DistGroup) AND
(qryCustDeptDistEURegionAJoin.AccountRef =
qryCustDeptDistEURegionCurrentMonth.AccountRef) AND
(qryCustDeptDistEURegionAJoin.RegionID =
qryCustDeptDistEURegionCurrentMonth.Region) AND
(qryCustDeptDistEURegionAJoin.MarketID =
qryCustDeptDistEURegionCurrentMonth.Market)) LEFT JOIN
qryCustDeptDistEURegionPriorMonth ON
(qryCustDeptDistEURegionAJoin.DistributorID =
qryCustDeptDistEURegionPriorMonth.DistGroup) AND
(qryCustDeptDistEURegionAJoin.AccountRef =
qryCustDeptDistEURegionPriorMonth.AccountRef) AND
(qryCustDeptDistEURegionAJoin.RegionID =
qryCustDeptDistEURegionPriorMonth.Region) AND
(qryCustDeptDistEURegionAJoin.MarketID =
qryCustDeptDistEURegionPriorMonth.Market)) LEFT JOIN
qryCustDeptDistEURegionCurrentYTD ON
(qryCustDeptDistEURegionAJoin.DistributorID =
qryCustDeptDistEURegionCurrentYTD.DistGroup) AND
(qryCustDeptDistEURegionAJoin.AccountRef =
qryCustDeptDistEURegionCurrentYTD.AccountRef) AND
(qryCustDeptDistEURegionAJoin.RegionID =
qryCustDeptDistEURegionCurrentYTD.Region) AND
(qryCustDeptDistEURegionAJoin.MarketID =
qryCustDeptDistEURegionCurrentYTD.Market)) LEFT JOIN
qryCustDeptDistEURegionPriorYTD ON
(qryCustDeptDistEURegionAJoin.DistributorID =
qryCustDeptDistEURegionPriorYTD.DistGroup) AND
(qryCustDeptDistEURegionAJoin.AccountRef =
qryCustDeptDistEURegionPriorYTD.AccountRef) AND
(qryCustDeptDistEURegionAJoin.RegionID =
qryCustDeptDistEURegionPriorYTD.Region) AND
(qryCustDeptDistEURegionAJoin.MarketID =
qryCustDeptDistEURegionPriorYTD.Market)) LEFT JOIN
qryCustDeptDistEURegionCurrentRolling12Month ON
(qryCustDeptDistEURegionAJoin.DistributorID =
qryCustDeptDistEURegionCurrentRolling12Month.DistGroup) AND
(qryCustDeptDistEURegionAJoin.AccountRef =
qryCustDeptDistEURegionCurrentRolling12Month.AccountRef) AND
(qryCustDeptDistEURegionAJoin.RegionID =
qryCustDeptDistEURegionCurrentRolling12Month.Region) AND
(qryCustDeptDistEURegionAJoin.MarketID =
qryCustDeptDistEURegionCurrentRolling12Month.Market)) LEFT JOIN
qryCustDeptDistEURegionPriorRolling12Month ON
(qryCustDeptDistEURegionAJoin.DistributorID =
qryCustDeptDistEURegionPriorRolling12Month.DistGroup) AND
(qryCustDeptDistEURegionAJoin.AccountRef =
qryCustDeptDistEURegionPriorRolling12Month.AccountRef) AND
(qryCustDeptDistEURegionAJoin.RegionID =
qryCustDeptDistEURegionPriorRolling12Month.Region) AND
(qryCustDeptDistEURegionAJoin.MarketID =
qryCustDeptDistEURegionPriorRolling12Month.Market)
WHERE (((qryCustDeptDistEURegionAJoin.MarketID) Like
[Forms]![frmReportParameters]![cboEURMarket]) AND
((qryCustDeptDistEURegionAJoin.RegionID) Like
[Forms]![frmReportParameters]![cboEURDepartment]) AND
((qryCustDeptDistEURegionAJoin.DistributorID) Like
[Forms]![frmReportParameters]![cboEURProdDist]));
Is there anyway I could simplify this?
Cheers
Tony