Thanks a lot for your suggestion, that in fact gets me through the loop. Only problem is, I need to group by origine and get that sum(bytes) and by destinazione and get that sum(bytes). They are different. Your statement only groups for origine (in fact it groups by address, which is non-existent in the day table, and gets me null values). In the meantime, I have discovered that the number of queries is irrelevant to the crash, the crash happens in the INSERT statement when the datetime field is set to 12:00:00 or 12:59:59 or whatever, but always at 12. Ludo