In fact, S-SQL had nearly everything we needed!

A bit verbose but we can work on that.