Solved

MS Access Query similar to crosstab query

Posted on 2016-09-22
6
39 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
  • 4
  • 2
6 Comments
 
LVL 34

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

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

Accepted Solution

by:
PatHartman earned 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now