473,762 Members | 8,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Counting grouped repeats within date range.

26 New Member
I have been pulling my hair out for a week on this one! In a query using Ms Access 2003, I am trying to group two columns. The third column is the date of the transaction. I would like the query to tell me the number of repeats for each record that fall within a 7 day period of each records date. Hope this makes sense, I can sure use some help.

Thanks.
Aug 4 '08 #1
6 2632
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi. As it stands, you need two queries to achieve what you ask. If I understand you correctly, you need to count how many records within the current grouping are within 7 days of the current record's date. I do not know whether you mean 7 days overall, or 7 days before or after the current date. I have taken you to mean 7 days before or after, but only you will know if this is correct. If in fact it is just up to 7 days before the current date the BETWEEN criterion would change to BETWEEN (x.trDate - 7) AND x.trDate

In the example below there are two queries. The first uses a self join to find all records within seven days of the current record date, and the second counts the results. Please note that the table and field names will need to be replaced in any testing you do with your actuals.

DateGroupQuery
Expand|Select|Wrap|Line Numbers
  1. SELECT X.Grp1, X.Grp2, X.trDate AS [Rec Date], Y.trDate AS [Other Date]
  2. FROM DateGroup AS X INNER JOIN DateGroup AS Y ON (X.Grp2 = Y.Grp2) AND (X.Grp1 = Y.Grp1)
  3. GROUP BY X.Grp1, X.Grp2, X.trDate, Y.trDate
  4. HAVING (((X.trDate)<>[Y].[trDate]) AND ((Y.trDate) Between ([x].[trDate]-7) And ([x].[trDate]+7)));
Count query
Expand|Select|Wrap|Line Numbers
  1. SELECT DateGroupQuery.Grp1, DateGroupQuery.Grp2, DateGroupQuery.[Rec Date], Count(DateGroupQuery.[Rec Date]) AS [Range count]
  2. FROM DateGroupQuery
  3. GROUP BY DateGroupQuery.Grp1, DateGroupQuery.Grp2, DateGroupQuery.[Rec Date];
The results of this query are appended below, on the test data shown.

-Stewart

Test Data
Expand|Select|Wrap|Line Numbers
  1. Grp1    Grp2    trDate
  2. A    A    01/01/2008
  3. A    A    02/01/2008
  4. A    A    03/01/2008
  5. A    A    04/01/2008
  6. A    A    05/01/2008
  7. A    A    06/01/2008
  8. A    A    07/01/2008
  9. A    A    08/01/2008
  10. A    A    09/01/2008
  11. A    B    01/02/2008
  12. A    B    02/02/2008
  13. A    B    03/02/2008
  14. A    B    04/02/2008
  15. A    B    05/02/2008
  16. A    B    10/02/2008
  17. A    B    11/02/2008
  18. A    B    01/03/2008
  19. A    B    08/03/2008
  20. A    B    09/03/2008
DateGroupQuery (extract)
Expand|Select|Wrap|Line Numbers
  1. Grp1    Grp2    Rec Date    Other Date
  2. A    A    01/01/2008    02/01/2008
  3. A    A    01/01/2008    03/01/2008
  4. A    A    01/01/2008    04/01/2008
  5. A    A    01/01/2008    05/01/2008
  6. A    A    01/01/2008    06/01/2008
  7. A    A    01/01/2008    07/01/2008
  8. A    A    01/01/2008    08/01/2008
  9. A    A    02/01/2008    01/01/2008
  10. A    A    02/01/2008    03/01/2008
  11. A    A    02/01/2008    04/01/2008
  12. A    A    02/01/2008    05/01/2008
  13. A    A    02/01/2008    06/01/2008
  14. A    A    02/01/2008    07/01/2008
  15. A    A    02/01/2008    08/01/2008
  16. A    A    02/01/2008    09/01/2008
  17. A    A    03/01/2008    01/01/2008
  18. A    A    03/01/2008    02/01/2008
  19. A    A    03/01/2008    04/01/2008
Count query
Expand|Select|Wrap|Line Numbers
  1. Grp1    Grp2    Rec Date    Range count
  2. A    A    01/01/2008    7
  3. A    A    02/01/2008    8
  4. A    A    03/01/2008    8
  5. A    A    04/01/2008    8
  6. A    A    05/01/2008    8
  7. A    A    06/01/2008    8
  8. A    A    07/01/2008    8
  9. A    A    08/01/2008    8
  10. A    A    09/01/2008    7
  11. A    B    01/02/2008    4
  12. A    B    02/02/2008    4
  13. A    B    03/02/2008    5
  14. A    B    04/02/2008    6
  15. A    B    05/02/2008    6
  16. A    B    10/02/2008    4
  17. A    B    11/02/2008    3
  18. A    B    01/03/2008    1
  19. A    B    08/03/2008    2
  20. A    B    09/03/2008    1
Aug 10 '08 #2
NeoPa
32,572 Recognized Expert Moderator MVP
A perfectly good answer Stewart.

I would simply add that it is possible to combine the two queries into one larger query using a subquery.

This is not necessarily a desirable approach, but there are situations where it is necessary.

I should make clear that this is principally Stewart's SQL and I just made a few changes. The subquery [subQ] ([DateGroupQuery] in Stewart's post) has been changed not to GROUP at all (HAVING clause replaced by code in ON sub-clause), and the query [DateGroupQuery] has been incorporated into the single SQL code as [subQ].
Expand|Select|Wrap|Line Numbers
  1. SELECT subQ.Grp1,
  2.        subQ.Grp2,
  3.        subQ.[Rec Date],
  4.        Count(subQ.[Rec Date]) AS [Range Count]
  5.  
  6. FROM (SELECT DG1.Grp1,
  7.              DG1.Grp2,
  8.              DG1.trDate AS [Rec Date],
  9.              DG2.trDate AS [Other Date]
  10.  
  11.       FROM DateGroup AS DG1 INNER JOIN DateGroup AS DG2
  12.         ON ((DG1.Grp1=DG2.Grp1)
  13.        AND  (DG1.Grp2=DG2.Grp2)
  14.        AND  (DG1.trDate<>DG2.trDate)
  15.        AND  (DG2.trDate Between DG1.trDate-7 And DG1.trDate+7))) AS subQ
  16.  
  17. GROUP BY subQ.Grp1,
  18.          subQ.Grp2,
  19.          subQ.[Rec Date]
(Please excuse the plagiarism ;->)
Aug 10 '08 #3
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Lovely subquery solution, NeoPa. Excellent formatting of your SQL too, as always - how do you do the lining up??

I remain a tad wary of subqueries as I find that the speed of execution of many equivalent queries much poorer than that achieved by a cascaded solution. To freeflyer I would simply suggest trying it out to see what happens - it is certainly neater and conceptually simpler than the cascaded two-query version.

Cheers

Stewart
Aug 11 '08 #4
NeoPa
32,572 Recognized Expert Moderator MVP
Lovely subquery solution, NeoPa. Excellent formatting of your SQL too, as always - how do you do the lining up??
...
Thank you. Coming from you that's an accolade indeed :)

I use TextPad - a text based editor, configured to use a non-proportional font. Critically, I also use spaces. Tab characters are only reliable when the tab columns defined for the output display exactly match those where you're typing it in.
...
I remain a tad wary of subqueries as I find that the speed of execution of many equivalent queries much poorer than that achieved by a cascaded solution. To freeflyer I would simply suggest trying it out to see what happens - it is certainly neater and conceptually simpler than the cascaded two-query version.
...
No argument there Stewart. Access suffers particularly from poor optimisation where subqueries or UNION queries (and probably various others) are concerned. I repeat this was simply an option, not a recommendation. That's for the OP to decide on.

QueryDefs (saved Access Queries) typically store within them the optimisation info that has previously been determined for them. This is almost always helpful for determining how to optimise a query based on them (for the engine I mean. This is not a manual process in Access).
Aug 11 '08 #5
freeflyer30339
26 New Member
Thank you very much for your time and assistance, the query worked perfectly. I apologize for the duplicate posting.

Would it be too much to ask for a little more help with this?

I would like to add one more field [location] in the results as to where the product was purchased. The location field is in the same table as the rest of the data. Just not sure how to incorporate the field into the query as to not skew the data.

Thanks again for all your help.
Aug 11 '08 #6
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi. Whilst there is no problem at all with adding a location field to the main and subquery (or both of the cascade versions) you will inevitably change the totals, as you are introducing a further grouping.

Just add the location field in the SELECT and GROUP BY clauses of each query/subquery as necessary. You need to make sure you add the location field in both (i.e. that you select the field and that you group by it) for this to work.

-Stewart
Aug 11 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
4507
by: Herb Kauhry | last post by:
I've been scratching my head on this for quite awhile and it has me stumped. I hope to define a query which I can use to fill a "day planner" type of calendar. Although I've see a lot of these, only one has had what I think is a really nice feature - it collected into groups all events that overlapped into contiguous blocks of time. The net result of this is that it becomes possible to output a calendar (html table) that is much less...
2
3421
by: David Mitchell | last post by:
I have tried using the following code to count the specific number of each weekday but get a compile error "User defined type not defined" which I think relates to the first line of the function: - Public Function HowManySpecificDays(StartDate As Date, EndDate As Date) As DayCounts and specifically the word "DayCounts" Can anyone help?
2
2304
by: BlackFireNova | last post by:
I have an Access 2003 mdb which contains software records. I need to sort on a particular type of software, and then identify and count how many copies there are per each group of that type purchased on the same date. I have no trouble doing a query to extract the type (say MS Excel 2002, for example). The trouble is, there could be 50 copies purchased on date "x", 80 copies purchased on date "y", and 250 copies purchased on date "z",...
18
2943
by: ChadDiesel | last post by:
I appreciate the help on this group. I know I've posted a lot here the last couple of weeks, but I was thrown into a database project at my work with very little Access experience. No other employee knows anything about Access. I've searched Google Groups, and that has been a lot of help, but there are some questions that I just can't find the answer to. I'll try to take it easy on the group after this question. I have one more...
2
1342
by: ckpoll2 | last post by:
Hello, On a report that I've created, I ask the user to input a starting date and an ending date to specify the range for which they want to see data. I've done this using and in the query builder on the report. I need to find averages based on the data which requires me to have a number of days in the range that is entered, like 31 if they enter 8/1/06 and 8/31/06 or 5 if they enter 3/8/06 and 3/12/06. Is there any way that I can do...
3
1767
by: gardner | last post by:
Please help. I am trying to count something in a Group Footer using a Text Box. If what I am doing is not doable, or there is a better way to structure this please tell me. Here is the situation: This is an attendance report for Summer School ( Students may be enrolled in both a AM session or a PM session and could be absent in one or both.) I have a report that is Grouped first by Student ID, then by date, in the detail section, I have...
1
3734
by: RussCRM | last post by:
I need some help getting unique records from our database! I work for a small non-profit homeless shelter. We keep track of guest information as well as what services we have offered for statistical purposes. I've been using Here’s the situation: I have two main tables: Guest (stores data such as GuestID, First Name, Last Name, etc.) and Services (stores data such as the type of service the guest used (Shelter Bed, Lunch, Dinner,...
11
1992
by: freeflyer30339 | last post by:
I am trying to create code and or a query in Ms Access 03 to calculate the number of times a customer has purchased the same product within 7 days. Customer Product_no Pur_date Repeats A 21-22-23 6/1/2008 2 A 49-72-02 6/2/2008 0 B 17-24-04 6/1/2008 3 B 17-24-04 6/5/2008 2 A 21-22-23 6/5/2008 0 C 67-42-20 6/6/2008 0 B ...
1
1466
by: elgoober | last post by:
Hi I have a report that looks at no of products sold by customer by range. The report groups on the range and I'm interested in counting the number of companies buying the range. Since they are grouped at query level I can rest assured that duplicates will not be included. I've tried adding a dumb text box () and then adding a textbox on the group footer referencing () but that does'nt play ball. BigSeries is the group
0
9378
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9927
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7360
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3914
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.