Thank you, that helped me quite a lot!
I continued with your example:
WITH [{idx: 1, name: 'Mon'}, {idx: 2, name: 'Tue'}, {idx: 3, name: 'Wed'}, {idx: 4, name: 'Thu'}, {idx: 5, name: 'Fri'}, {idx: 6, name: 'Sat'}, {idx: 7, name: 'Sun'} ] AS WeekDays
UNWIND WeekDays as WeekDay
MATCH (o:Order)
WHERE datetime(o.createdAt).dayOfWeek = WeekDay.idx
WITH WeekDay, collect(datetime(o.createdAt)) as OrderTimestamps
RETURN
WeekDay.name as DayName,
size([ts IN OrderTimestamps WHERE ts.hour = 0]) as h00,
size([ts IN OrderTimestamps WHERE ts.hour = 1]) as h01,
size([ts IN OrderTimestamps WHERE ts.hour = 2]) as h02,
size([ts IN OrderTimestamps WHERE ts.hour = 3]) as h03,
size([ts IN OrderTimestamps WHERE ts.hour = 4]) as h04,
size([ts IN OrderTimestamps WHERE ts.hour = 5]) as h05,
size([ts IN OrderTimestamps WHERE ts.hour = 6]) as h06,
size([ts IN OrderTimestamps WHERE ts.hour = 7]) as h07,
size([ts IN OrderTimestamps WHERE ts.hour = 8]) as h08,
size([ts IN OrderTimestamps WHERE ts.hour = 9]) as h09,
size([ts IN OrderTimestamps WHERE ts.hour = 10]) as h10,
size([ts IN OrderTimestamps WHERE ts.hour = 11]) as h11,
size([ts IN OrderTimestamps WHERE ts.hour = 12]) as h12,
size([ts IN OrderTimestamps WHERE ts.hour = 13]) as h13,
size([ts IN OrderTimestamps WHERE ts.hour = 14]) as h14,
size([ts IN OrderTimestamps WHERE ts.hour = 15]) as h15,
size([ts IN OrderTimestamps WHERE ts.hour = 16]) as h16,
size([ts IN OrderTimestamps WHERE ts.hour = 17]) as h17,
size([ts IN OrderTimestamps WHERE ts.hour = 18]) as h18,
size([ts IN OrderTimestamps WHERE ts.hour = 19]) as h19,
size([ts IN OrderTimestamps WHERE ts.hour = 20]) as h20,
size([ts IN OrderTimestamps WHERE ts.hour = 21]) as h21,
size([ts IN OrderTimestamps WHERE ts.hour = 22]) as h22,
size([ts IN OrderTimestamps WHERE ts.hour = 23]) as h23
Instead of executing 7 MATCH queries during UNWIND, I changed it to 1 MATCH query for all orders and do the filtering of DayOfWeek and Hour in the list comprehension on the timestamps. I'm not sure it is actually faster, I would just guess so?
MATCH (o:Order)
WITH collect(datetime(o.createdAt)) as OrderTimestamps, [{idx: 1, name: 'Mon'}, {idx: 2, name: 'Tue'}, {idx: 3, name: 'Wed'}, {idx: 4, name: 'Thu'}, {idx: 5, name: 'Fri'}, {idx: 6, name: 'Sat'}, {idx: 7, name: 'Sun'} ] AS WeekDays
UNWIND WeekDays as WeekDay
RETURN
WeekDay.name as DayName,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 0]) as h00,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 1]) as h01,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 2]) as h02,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 3]) as h03,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 4]) as h04,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 5]) as h05,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 6]) as h06,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 7]) as h07,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 8]) as h08,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 9]) as h09,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 10]) as h10,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 11]) as h11,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 12]) as h12,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 13]) as h13,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 14]) as h14,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 15]) as h15,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 16]) as h16,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 17]) as h17,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 18]) as h18,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 19]) as h19,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 20]) as h20,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 21]) as h21,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 22]) as h22,
size([ts IN OrderTimestamps WHERE ts.dayOfWeek = WeekDay.idx and ts.hour = 23]) as h23
Is there any way to further improve this cypher query?