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
Solved

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

Posted on 2016-10-23
8
61 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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 49

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

856 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