ERROR : 0ut of memory

My code runs perfectly but at some moments it does not work as there is this error when i run it:

ERROR: out of memory Detail: Failed on request of size 160 in memory context "Caller tuples".

I don't know how to resolve it

You are selecting apparently almost six week's worth of data. Not sure how many records live in that amount of time, but if you reduce the amount of time to fewer weeks, does that fix the out of memory issue?

scale vertically your database engine, it's running out of RAM