Jeder fängt mal neu an, kein Problem :-)
Add/substract a number of days from a DATE is just DATE +/- x.
(datecol -
DATE
'0001-01-01'
)
returns the number of days between those dates.
DATE '0001-01-01' is a known monday (you could use any other monday instead), thus the difference modulo 7 returns 0 = monday to 7 = sunday. Substract that from your current date and you'll get the previous monday. Substract 20*7 and you'll get the monday 20 weeks before.
Your query is almost correct, you just have to use CURRENT_DATE instead of i.Abschluss_Erledigt_TS. And i'd suggest removing the typecast from i.Abschluss_Erledigt_TS (which might help the optimizer to get better estmates):
WHERE
Abschluss_Erledigt_TS < CAST(CURRENT_DATE + 1 AS TIMESTAMP) -- next day midnight
AND
Abschluss_Erledigt_TS >= CAST(CURRENT_DATE - ((CURRENT_DATE - DATE '0001-01-07') MOD 7) -20*7 AS TIMESTAMP) -- midnight monday 20 weeks before
Dieter
Jeder fängt mal neu an, kein Problem :-)
Add/substract a number of days from a DATE is just DATE +/- x.
(datecol -
DATE
'0001-01-01'
)
returns the number of days between those dates.DATE '0001-01-01' is a known monday (you could use any other monday instead), thus the difference modulo 7 returns 0 = monday to 7 = sunday. Substract that from your current date and you'll get the previous monday. Substract 20*7 and you'll get the monday 20 weeks before.
Your query is almost correct, you just have to use CURRENT_DATE instead of i.Abschluss_Erledigt_TS. And i'd suggest removing the typecast from i.Abschluss_Erledigt_TS (which might help the optimizer to get better estmates):
Dieter