Hi Graham,
To assist you with your issue/problem adequately, it would be constructive for us to reproduce your scenario.
Please provide us with at least the following things:
(1) DDL and sample data population, i.e., CREATE table(s) plus INSERT, T-SQL statements.
(2) What you need to do, i.e., logic and your attempt implementation of it in T-SQL.
(3) Desired output based on the sample data in #1 above.
(4) Your SQL Server version (SELECT @@version;)
I hope my answer is helpful to you,
Your
Bjoern Peters
If the reply was helpful, please upvote and/or accept it as an answer, as this helps others in the community with similar questions. Thanks!