Because the poor formatting and example (making the code look too much as sql!), some nuance was lost. Consider instead:
let keys = SELECT key FROM BTreeIndex_CustomerCode;
let city = SELECT name;
let groups = GROUP BY (whatever -- And I mean GROUP, not SUMMARIZE!);
let filter = WHERE id = @id;
let query = city + filter(id=1);
for city in query GROUP BY keys
....