简体   繁体   中英

Access Crosstab Query: based on sales totals within TWO date ranges

I'm looking for a way to create an Access crosstab query reporting sales totals by 'Brand', for two different date ranges.:

  • For Distributor: "DistID" (column, not visible)
  • Sales Totals: "Sales" (column)
  • TWO different date ranges: "depDate" for Period 1 and Period 2 (column):
    • Period1 = Between [forms]![frmRPT_YTDDepl_SF]![BDT1] And [forms]![frmRPT_YTDDepl_SF]![EDT1]
    • Period2 = Between [forms]![frmRPT_YTDDepl_SF]![BDT2] And [forms]![frmRPT_YTDDepl_SF]![EDT2]
  • Brands = "DprodBrand" (rows)

Currently, I have TWO separate crosstab queries for each period, working properly. -> CODE BELOW.

I am looking for a way to create ONE query displaying Brand's sales totals for each date range, in two separate columns or one crosstab query.

Period1:

TRANSFORM Sum(tblDepletions_DETAIL.detQuan) AS Sales
SELECT tblProducts_DEPL.DprodBrand
FROM tblDepletions INNER JOIN (tblProducts_DEPL INNER JOIN     tblDepletions_DETAIL ON tblProducts_DEPL.DprodZSKU = tblDepletions_DETAIL.detZSKU) ON tblDepletions.depID = tblDepletions_DETAIL.detDeplID
WHERE (((tblDepletions.depDate) Between [forms]![frmRPT_YTDDepl_SF]![BDT1]     And [forms]![frmRPT_YTDDepl_SF]![EDT1]) AND ((tblDepletions.depDistID)=132))
GROUP BY tblProducts_DEPL.DprodBrand
ORDER BY tblProducts_DEPL.DprodBrand
PIVOT Format([depDate],"yy")-(Format(Date(),"yy"))+2 In (1,2);

Period2:

TRANSFORM Sum(tblDepletions_DETAIL.detQuan) AS Sales
SELECT tblProducts_DEPL.DprodBrand
FROM tblDepletions INNER JOIN (tblProducts_DEPL INNER JOIN tblDepletions_DETAIL ON tblProducts_DEPL.DprodZSKU = tblDepletions_DETAIL.detZSKU) ON tblDepletions.depID = tblDepletions_DETAIL.detDeplID
WHERE (((tblDepletions.depDate) Between [forms]![frmRPT_YTDDepl_SF]![BDT2] And [forms]![frmRPT_YTDDepl_SF]![EDT2]) AND ((tblDepletions.depDistID)=132))
GROUP BY tblProducts_DEPL.DprodBrand
ORDER BY tblProducts_DEPL.DprodBrand
PIVOT Format([depDate],"yy")-(Format(Date(),"yy"))+2 In (1,2);

Many Thanks!!! ~~ Jacob

Consider simply joining the two saved, crosstab queries like any other pair of queries or tables using the DprodBrand as join key:

SELECT CrosstabQ1.DprodBrand, 
       CrosstabQ1.[1] As Period1_Year1, CrosstabQ2.[1] As Period2_Year1,
       CrosstabQ1.[2] As Period1_Year2, CrosstabQ2.[2] As Period2_Year2
FROM CrosstabQ1
INNER JOIN CrosstabQ2 ON CrosstabQ1.DprodBrand = CrosstabQ2.DprodBrand

Now if you only want one query to do it all, consider the conditional aggregate pivot query since crosstabs cannot be used as subqueries. Here you migrate WHERE to IIF() conditions:

SELECT p.DprodBrand,
       SUM(IIF((d.depDate BETWEEN [Forms]![frmRPT_YTDDepl_SF]![BDT1] 
                              AND [Forms]![frmRPT_YTDDepl_SF]![EDT1])
               AND (Format(d.[depDate],"yy")-(Format(Date(),"yy"))+2 = 1), 
               dt.detQuan, NULL)) AS Period1_Year1,

       SUM(IIF((d.depDate BETWEEN [Forms]![frmRPT_YTDDepl_SF]![BDT2]     
                              AND [Forms]![frmRPT_YTDDepl_SF]![EDT2)
               AND (Format(d.[depDate],"yy")-(Format(Date(),"yy"))+2 = 1), 
               dt.detQuan, NULL)) AS Period2_Year1,

       SUM(IIF((d.depDate BETWEEN [Forms]![frmRPT_YTDDepl_SF]![BDT1] 
                              AND [Forms]![frmRPT_YTDDepl_SF]![EDT1])
               AND (Format(d.[depDate],"yy")-(Format(Date(),"yy"))+2 = 2), 
               dt.detQuan, NULL)) AS Period1_Year2,

       SUM(IIF((d.depDate BETWEEN [Forms]![frmRPT_YTDDepl_SF]![BDT2]     
                              AND [Forms]![frmRPT_YTDDepl_SF]![EDT2])
               AND (Format(d.[depDate],"yy")-(Format(Date(),"yy"))+2 = 2), 
               dt.detQuan, NULL)) AS Period2_Year2

FROM tblDepletions d
INNER JOIN (tblProducts_DEPL p
INNER JOIN tblDepletions_DETAIL dt
     ON p.DprodZSKU = dt.detZSKU) 
     ON d.depID = dt.detDeplID

WHERE ((d.depDistID)=132)
GROUP BY p.DprodBrand
ORDER BY p.DprodBrand

As this is Access, it might be simpler to save the two queries leaving out the ORDER BY.

Then create a new query:

SELECT *
FROM Q1
UNION ALL
SELECT *
FROM Q2
ORDER BY DprodBrand

By: Dale Fye (Access MVP):

I'm not sure you need a CrossTab for this.

Select DProdBrand, 
SUM(IIF([DepDate] BETWEEN [Forms]![frmRpt_YTDDepl_SF]![BDT1] 
AND [[forms]![frmRPT_YTDDepl_SF]![EDT1], [Sales], 0) as Period1, 
SUM(IIF([DepDate] Between [forms]![frmRPT_YTDDepl_SF]![BDT2] 
AND [forms]![frmRPT_YTDDepl_SF]![EDT2], [Sales], 0) as Period2, 
SUM([Sales]) as [Sales Total]
FROM yourTable
GROUP BY DProdBrand

https://www.experts-exchange.com/questions/28978325/Access-Crosstab-Query-based-on-sales-totals-within-TWO-date-ranges.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM