I have a table called projecthistory
,
it is actually a view so I could take advantage of the Rank
function to get a unique record number.
From this view I need to create a temp table that has a uniqueid
field which would be calculated bases on these rules:
-
Rule 1 – The next sequential record for the id changes from one
project to another or -
Rule 2 – If the id and project match for the next sequential record
, but the days between the end date of the current record and start
date of the next record for the is more than 7 days.
It would also consolidate the start and end dates appropriately for each group.
I believe this would be best done in a stored procedure that can call the data when needed, but I'm fairly new to stored procedures.
Here is an example of the table data, as well as the desired output
Projecthistory
╔════════╦══════╦═════════╦═══════════╦═══════════╗
║ Record ║ Id ║ Project ║ Start ║ End ║
╠════════╬══════╬═════════╬═══════════╬═══════════╣
║ 1 ║ 1111 ║ A ║ 1/1/2016 ║ 1/7/2016 ║
║ 2 ║ 1111 ║ A ║ 1/10/2016 ║ 1/20/2016 ║
║ 3 ║ 1111 ║ B ║ 1/21/2016 ║ 1/25/2016 ║
║ 4 ║ 1111 ║ A ║ 1/26/2016 ║ NULL ║
║ 5 ║ 2222 ║ A ║ 1/1/2016 ║ 2/1/2016 ║
║ 6 ║ 2222 ║ B ║ 2/2/2016 ║ NULL ║
║ 7 ║ 3333 ║ D ║ 1/15/2016 ║ 2/1/2016 ║
║ 8 ║ 4444 ║ B ║ 2/10/2016 ║ NULL ║
║ 9 ║ 5555 ║ A ║ 2/1/2016 ║ 2/5/2016 ║
║ 10 ║ 5555 ║ A ║ 2/20/2016 ║ 2/27/2016 ║
║ 11 ║ 5555 ║ A ║ 2/28/2016 ║ 3/10/2016 ║
║ 12 ║ 5555 ║ A ║ 3/11/2016 ║ NULL ║
╚════════╩══════╩═════════╩═══════════╩═══════════╝
Desired output
╔════════╦══════╦═════════╦══════════════╦════════════╗
║ unique ║ Id ║ Project ║ ProjectStart ║ ProjectEnd ║
╠════════╬══════╬═════════╬══════════════╬════════════╣
║ 1111A1 ║ 1111 ║ A ║ 1/1/2016 ║ 1/20/2016 ║
║ 1111B1 ║ 1111 ║ B ║ 1/21/2016 ║ 1/25/2016 ║
║ 1111A2 ║ 1111 ║ A ║ 1/26/2016 ║ NULL ║
║ 2222A1 ║ 2222 ║ A ║ 1/1/2016 ║ 2/1/2016 ║
║ 2222B1 ║ 2222 ║ B ║ 2/2/2016 ║ NULL ║
║ 3333D1 ║ 3333 ║ D ║ 1/15/2016 ║ 2/1/2016 ║
║ 4444B1 ║ 4444 ║ B ║ 2/10/2016 ║ NULL ║
║ 5555A1 ║ 5555 ║ A ║ 2/1/2016 ║ 2/5/2016 ║
║ 5555A2 ║ 5555 ║ A ║ 2/20/2016 ║ Null ║
╚════════╩══════╩═════════╩══════════════╩════════════╝
Here are some notes about the desired output and some of
more difficult scenarios in this data.
Take note that records 1-2 became 1111A1
,
record 3 becomes 1111b1
,
record 4 became the second instance of that project for id 1111 so its uniqueid
became 1111A2
, be careful this doesn’t get lumped
in with records 1&2.
Record 9 become 55555A1
and because of the date gap
between records 9&10, record 10-12 became uniqueid
5555A2
I look forward to any solutions, feedback or questions. I’m
truly grateful in advance.
Best Answer
ok I think I got it. I was able to get the results I was looking for using the following code, but it is slow when executing it to get the results. One thing I forget, was the location column, so I was sure to account for this in my code. I did end up breaking the uid and instance into two separate fields, but I know I could have merged them into one. I figured this would give me greater flexibility when pulling data against it in the future. Feel free to improve, I know I would appreciate it. I'm also happy to be posting my first answer. Thanks!