Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    accessmatt is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    199

    How to group by week

    I have a list of rows in a table with a date field in every row. How can I pull out that data in a way that counts the number of rows in a certain week.



    So basically, I want to report on entries grouped by week.

    Thanks,

    Matt
    new to access

  2. #2
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    In a new field in your query, create an expression using the DatePart Function

    Week: DatePart("ww",DateFieldName)

    Then create a new query using the query with your Week Field and create an aggregate query Group on the week and count the date field.

    Here is the SQL for that

    SELECT Query1.Week, Count(Query1.Date1) AS CountOfDate1
    FROM Query1
    GROUP BY Query1.Week;

    Alan

  3. #3
    accessmatt is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    199
    Hi Alan, thanks for trying to help but I'm not totally clear on what to do from your answer.

    I understand that I'm creating another field using the DatePart function in my current query but why am I doing that?

    Second, why do I need to create another query??

    Please try to break this down a bit more.

    thanks!

  4. #4
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    Access won't let you combine easily an aggregate query and a calculation query. Therefore, you create the first one to get the week and use it as a basis to aggregate the information you want. When you run the queries, you will only have to run the second one as it will run from the first one. Look at the example I have attached.

    Forgot to attach. See next thread.

  5. #5
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    See attachment. One table and two queries.

  6. #6
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    The other way, if this is truly in a report, is to just pull the data without grouping and then use the report's SORTING AND GROUPING to do the work for you.

  7. #7
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    Bob;
    Excellent point. I do so few "reports" that I tend to think of how to do it in Queries and not in reports. Great thing about forums is there is always someone that has a different approach from what you think.

    Alan

  8. #8
    accessmatt is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    199
    Alan,

    Thank you for that very helpful example. I was able to replicate it in my db.

    However 2 other issues have come up: 1.) I have multiple years in my table and 2.) I really want to see weeks by date (starting on Sunday). So for example, this week is, 9/18/11. I think they can both be solved by printing week like 9/18/11 instead of by the number of the week but hHow can I adjust my query to see report that way?

    THANKS!
    Matt

    PS: Bob, Thanks for your comment about making a report. That's what I was originally thinking but this way works nicely too and I can make a report from it to make it look better after I've done!

  9. #9
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    Added a year to the week notation. Look at the db attached.

  10. #10
    accessmatt is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    199
    Thanks Alan but how can I show the weeks so that they're they're actually in day/month/year format?

  11. #11
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    An Expression is required, Put this in a new field:

    =iif(DatePart("w",Date1)=1,DateAdd("d",6,Date1),ii f(DatePart("w",Date1)=2,DateAdd("d",5,Date1),iif(D atePart("w",Date1)=3,DateAdd("d",4,Date1),iif(Date Part("w",Date1)=4,DateAdd("d",3,Date1),iif(DatePar t("w",Date1)=5,DateAdd("d",2,Date1),iif(DatePart(" w",Date1)=6,DateAdd("d",6,Date1),Date1))))))

    See Attached.

  12. #12
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Quote Originally Posted by accessmatt View Post
    PS: Bob, Thanks for your comment about making a report. That's what I was originally thinking but this way works nicely too and I can make a report from it to make it look better after I've done!
    Believe it or not, it would have been extremely easy to use the report to do not only your grouping but to display your dates by week in Date format without all of the hassle. But, that's the way it goes sometimes. People sometimes take the harder route because it sounds easier than the easy route.

  13. #13
    accessmatt is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    199
    Bob,

    How can I do this with a report? I'm able to do the grouping but I need to know how to show the Dates in Date format and also roll up all of the entries so that I only see a count of number of entries grouped by week with full date.

    Matt

  14. #14
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    I don't have time to try to write all of that out. Can you upload a copy of your database so I can take a look at it? I think it may be easier, for me at least, to be able to go do it and then you can see what I did.

  15. #15
    accessmatt is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    199

    Database attached

    Ok, thanks Bob, here it is. I couldn't get it less then 500kb so I'm giving you a link to it where I put it on the web: http://goo.gl/KqQD4

    I need to see the amount of jobs that I applied to grouped by week. With the week spelled out (i.e. week of 9/25/2011, starting every Sunday)

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Data group by week
    By hoachen in forum Access
    Replies: 2
    Last Post: 09-09-2011, 08:37 AM
  2. Replies: 5
    Last Post: 11-29-2010, 08:16 AM
  3. Finding last week
    By jgelpi16 in forum Queries
    Replies: 5
    Last Post: 09-21-2010, 02:32 PM
  4. Group by Week in a Crosstab Query
    By NMJones in forum Access
    Replies: 1
    Last Post: 06-24-2010, 04:09 PM
  5. columns for group detail but not group header?
    By Coolpapabell in forum Reports
    Replies: 0
    Last Post: 08-21-2009, 08:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums