Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MS Access Query similar to crosstab query

Posted on 2016-09-22
6
Medium Priority
?
81 Views
Last Modified: 2016-09-30
I have table tblSales with next fields:
Shop      DateSold      All_Yellow_Cap_Sold      All_Black_Cap_Sold      All_White_Cap_SoldAll_Red_Cap_Sold

….._Cap_Sold are Yes/No type filed in table.

I need query that will give me next result. In example I gave you I put just several entries in table that actually have several year entries of data.


Shop      DateSold      All_Yellow_Cap_Sold      All_Black_Cap_Sold      All_White_Cap_Sold    All_Red_Cap_Sold

Shop1      01/02/2016      Yes                        Yes                  No            Yes
Shop1      01/10/2016      Yes                        Yes                  Yes            No
shop1      01/23/2016      No                        No                  No            No
Shop1      02/11/2016      Yes                        No                  No            Yes
Shop1      02/20/2016      Yes                        Yes                  Yes            Yes
Shop1      03/04/2016      No                        No                  Yes            Yes
Shop2      01/03/2016      Yes                        Yes                  Yes            No
Shop2        01/14/2016      No                        Yes                  No            Yes
Shop2      01/28/2016      Yes                        No                  No            No
Shop2   02/20/2016      No                        Yes                  Yes            Yes
Shop2      02/25/2016      Yes                        Yes                  No            No
Shop2      03/05/2016      Yes                        No                  No            No


I need result as this:

Shop      DateSold              All_Yellow_Cap_Sold         All_Black_Cap_Sold       All_White_Cap_Sold    All_Red_Cap_Sold
Shop1      January/2016              2                                      2                                    1                                 1
Shop1      February/2016      2                                          1                                        1                                   2
Shop1   March/2016              0                                       0                                    1                                 1
Shop2      January/2016        2                                       2                                    1                                     1
Shop2      February/2016      1                                       2                                      1                                   1
Shop2      March/2016           1                                      0                                    0                                  0
..........
0
Comment
Question by:Taras
[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
  • 4
  • 2
6 Comments
 
LVL 39

Expert Comment

by:PatHartman
ID: 41811035
Select Shop, Format(DateSold,"mmm/yyyy") As YMSold,  Sum(IIf(All_Yellow_Cap_Sold = "Yes", 1, 0)) as SumYellow, Sum(IIf(All_Black_Cap_Sold = "yes", 1, 0)) As SumBlack,  Sum(IIf(All_White_Cap_Sold = "yes", 1, 0)), Sum(IIf(   All_Red_Cap_Sold = "yes", 1, 0)) As SumRed
From YourTable
Group By Shop, Format(DateSold,"mmm/yyyy") ;
0
 

Author Comment

by:Taras
ID: 41811228
Pat I need just mmm/yyyy grouping not Shops any more, I adjusted your solution it gives me column totals as first row but not proper numbers for mmm/yyyy rows just 1 or 0 in those rows. Should I use count instead sum??
0
 

Author Comment

by:Taras
ID: 41811229
I your suggestion I changed Sum(IIf(All_Yellow_Cap_Sold = "Yes", 1, 0)) with
Sum(IIf(All_Yellow_Cap_Sold = -1, 1, 0)) as I was getting error.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:Taras
ID: 41811238
In table those fields are seen as check boxes
0
 
LVL 39

Accepted Solution

by:
PatHartman earned 2000 total points
ID: 41811317
If they are checkboxes, thange "yes"  to True with no quotes.

Actually since they are Y/N fields rather than strings, do this:

Select Shop, Format(DateSold,"mmm/yyyy") As YMSold,  Abs(Sum(All_Yellow_Cap_Sold)) as SumYellow, Abs(Sum(All_Black_Cap_Sold)) As SumBlack,  Abs(Sum(All_White_Cap_Sold)), Abs(Sum(All_Red_Cap_Sold)) As SumRed
From YourTable
Group By Shop, Format(DateSold,"mmm/yyyy") ;

To omit shop:

Select  Format(DateSold,"mmm/yyyy") As YMSold,  Abs(Sum(All_Yellow_Cap_Sold)) as SumYellow, Abs(Sum(All_Black_Cap_Sold)) As SumBlack,  Abs(Sum(All_White_Cap_Sold)), Abs(Sum(All_Red_Cap_Sold)) As SumRed
From YourTable
Group By Format(DateSold,"mmm/yyyy") ;

True values = -1 and False values = 0 so just summing and converting to the absolute value "counts" the instances of true.

The Count() function counts instances selected so  if you were to change the Sum() to Count() all the values would be identical and would be the number of rows for the month/year rather than the sum of true values.
0
 

Author Comment

by:Taras
ID: 41811351
Thank you Pat excellent as always!!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
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 …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

610 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