>Does this code depend on the key being unique?
No, it's just the index expression being compared to its current value... set step on and try it.
>(And, by the way, it's fairly common in my application for the filter to be based on a relation to another table, so the SQL still wouldn't work.)
So then rework this to be a copy to... for ... thing. It should work (not that I tested it, though, I'm at work :).
>P.S.: I was unable to include the text of your message, as I got some complaint about tags within the message. Not sure what it meant.
That happens because UT does some mumbo-jumbo with the text when it includes a lot of less-than signs between pre-/pre tags. Generally works fine, except when there's any html-like text inside. That's why I had to replace the regular merge tags with {{ }} - or else I was getting the same message.