Solved

MS Access Query similar to crosstab query

Posted on 2016-09-22
6
69 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 38

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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

717 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