Solved

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

Posted on 2016-10-23
8
66 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
  • 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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 50

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 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 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 47

Expert Comment

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access syntax 1 34
Sharepoint list to Access database 9 52
Access query error 3 26
Email question 12 20
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

726 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