?
Solved

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

Posted on 2016-10-23
8
Medium Priority
?
89 Views
Last Modified: 2016-10-24
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);

Open in new window


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);

Open in new window

Many Thanks!!! ~~  Jacob
0
Comment
Question by:Chi Is Current
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41856394
try creating a UNION query

Select A.*
From
(
  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)
UNION ALL
 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)
) As A
0
 
LVL 2

Author Comment

by:Chi Is Current
ID: 41856426
Thank you, Rey, for your reply.

When I create the Union query from the code you provide, the query does not save, I get an error msg.: "Syntax error in FROM clause."  --  Both queries run independently.
???

I am hoping to find a way of creating one crosstab query with sales figures by brands within date range #1 in one column and sales figures by brands within date range #2 in the other column, ideally avoiding a union query, if possible.

Jacob
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41856428
can you upload a copy of the db?
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 2

Author Comment

by:Chi Is Current
ID: 41856434
Thank you, Rey ~

It has a bunch of client data in it.  I'll have to clear it out and make a demo version.  I need to leave now - so, either later tonight or tomorrow.

Best ~ Jacob
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 41856557
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

Open in new window

/gustav
0
 
LVL 48

Accepted Solution

by:
Dale Fye earned 2000 total points
ID: 41856775
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
1
 
LVL 2

Author Comment

by:Chi Is Current
ID: 41857541
Dale ~ Thank you for your new eyes here!  I agree, a crosstab query is not needed!
With a little  massaging (expected - see '[[forms]!...') your code is GREAT!
Even better!!!

Many Thanks ~ Jacob
0
 
LVL 48

Expert Comment

by:Dale Fye
ID: 41857678
sorry about the typo; glad I could help.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question