Microsoft SQL Server: Implement Partitions and Custom Logic