• You can probably do that following the examples in Jeff Moden's article. http://www.sqlservercentral.com/articles/T-SQL/63681/

    I would be nice and work on it, but it's almost 2 AM here... start there and if you get stuck, post your create table definition and insert statements to populate the table with dummy data (similar to the real data, but not necessarily real), and then you should get a tested result.

    HTH