Quantcast
Channel: Teradata Forums - Database
Viewing all articles
Browse latest Browse all 14773

Get last full 20 weeks with function ADD_MONTHS(date - EXTRACT(day FROM date) +0, 0) - response (3) by dnoeth

$
0
0

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
 
 
 

 
 
 


Viewing all articles
Browse latest Browse all 14773

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>