Link to home
Start Free TrialLog in
Avatar of Hiroyuki Tamura
Hiroyuki TamuraFlag for United States of America

asked on

MS Access Weekly query

I'm trying to create a weekly query. from Mon to Sun.
I tried format ([time],"ww") but can't understand how it works.
Avatar of PatHartman
PatHartman
Flag of United States of America image

I'm not sure where you are going with this so I just made a sample from one of my tables.

SELECT Year([LastChangeDT]) AS ChngYear, Format([LastChangeDT],"ww") AS ChngWeek, Count(*) AS Expr1
FROM tblChangeDT
GROUP BY Year([LastChangeDT]), Format([LastChangeDT],"ww");

This query returns:
User generated image
Avatar of Hiroyuki Tamura

ASKER

Thank you. How does "ww" define a week?
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Gus' function returns 2017W13 for today's date.
Problem is, that Format and DatePart don't always get week 53 right. Also, both week 1 and 52/53 can belong to either a calendar year before or after New Year. Thus, you need that year part as well to sort and group correctly when data spans calendar years.

/gustav
That's why I included Year in my query example.  Since year does not divide cleanly into weeks, it is necessary to decide how to handle the first week of the year when it doesn't start on the first day of the week and that also impacts the definition of the Last week of the year.  The ISO method may be what Tamura wants but in this case, the correct answer is actually what satisfies the business users.
Who knows? Questioneer is  silent.

/gustav
Thank you, all. I'm trying to add ISO method.
is this the right way?
User generated image
Thank you all