Hi,
sorry to revisit this post but this forum is a source of help & information for users of all abilities.
this post has been marked as solved and users will understandably believe that the posted solution is both accurate & reliable.
it was obvious to me that the code posted by 'MTP' was not the solution to the problem. That is the reason I previously posted a reply.
this is not a criticism of 'MTP' who does an outstanding job as a volunteer.
the user 'pjbrass' has not rigorously tested the code or may have insufficient data to do so.
'DAYOFWEEK()' produces a number for each weekday.
1=sunday, 2=monday, 3=tuesday, 4=wednesday, 5=thursday, 6=friday, 7=saturday.
we want our week to start on thursday & end on wednesday.
the sequence we need is 5, 6, 7, 1, 2, 3, 4.
that is 5, 6 & 7 of the current week followed by 1, 2, 3 & 4 from the next (following) week.
'WEEK()' produces a number for each week,
the first day of the week is always monday & the last day of the week is always sunday.
this dictates that the week number we use for the days thursday, friday , saturday & sunday is 'WEEK(current week)'.
so we need to change the WEEK number for the days monday, tuesday, & wednesday (of the following week) to that of 'WEEK(current week)'.
the week number ranges from 1 to 53. every 5 or 6 years we have a 53 week year.
upcoming years which have 53 weeks are 2015, 2020 & 2026.
the code below has been thoroughly tested using several different years including all which contain 53 weeks. I believe it to be 100% accurate.
it assumes the user has one table named 'tFirst_Day=Thurs_GroupByWeek_Sales' which contains the fields 'Date' & 'Sales'.
it answers the question posted by 'pjbrass'.
It is for use with HSQLDB 1.8.10.
In my opinion this is not a satisfactory solution to the problem because output is ambiguous & open to misinterpretation.
contains REM statements so should be run from menu:Edit>Run SQL command directly.
| Edit: made alteration to code. first line 2nd case structure was 'when week("Date")=53' now reads 'when week("Date") in(52, 53)' |
Code: Select all
--DEFAULTS ARE: week start = monday(dayofweek = 2) & week end = sunday(dayofweek = 1)
--we start our week on 'thursday'(dayofweek = 5) & end on 'wednesday'(dayofweek = 4)
select
case
when dayofweek("Date") between 2 and 4 and week("Date")<>1
then week("Date")-1
when dayofweek("Date") between 2 and 4 and week("Date")=1 and week(cast(Year("Date")-1 || '-12-28' as date)) = 53
then 53
when dayofweek("Date") between 2 and 4 and week("Date")=1 --and week(cast(Year("Date")-1 || '-12-28' as date)) <> 53
then 52
else
week("Date")
end
as "MyWeek",
case
when week("Date") in(52, 53) and month("Date")=1 and dayofweek("Date") not between 2 and 4
then year("Date")-1
when week("Date")=1 and month("Date")=1 and dayofweek("Date") between 2 and 4
then year("Date")-1
else
year("Date")
end
as "MyYear",
sum( "Sales" ) as "SalesTotal"
from "First_Day=Thurs_GroupByWeek_Sales"
group by "MyYear", "MyWeek"
order by "MyYear", "MyWeek"