I have all of that done except for 1 issue that is confusing the hell out of me and it is a duplicate entry. It's a lot of data so I'll try to break it apart using images with the results of each step and explanations above them. I have limited my results to 1 Assignment that will have 3 inspections in the end and consists of 4 total interactions on the assignment. If you have a better way of doing something let me know, because this query takes a while to run when going over 33000 interactions based on a 2 day range.
Step 1: Get a list of Filtered Interactions
QUERY:
DECLARE @StartTime datetime = CONVERT(DATETIME, '2015-07-20') DECLARE @EndTime datetime = CONVERT(DATETIME, '2015-07-24') CREATE TABLE #FilteredInteractions ( InteractionID BIGINT NULL, LocationID INT NULL, AssignmentID BIGINT NULL, InteractionEvent NVARCHAR(30) NULL, StartTime DATETIME NULL, EndTime DATETIME NULL ); INSERT INTO #FilteredInteractions SELECT ii.InteractionId, ia.LocationId, ia.AssignmentId, ii.InteractionEvent, ii.StartTime, ii.EndTime FROM InspInteractions ii LEFT OUTER JOIN InspAssignments ia ON ii.AssignmentId = ia.AssignmentId WHERE (ii.StartTime >= @StartTime OR @StartTime IS NULL) AND (ii.EndTime < @EndTime OR @EndTime IS NULL) SELECT * FROM #FilteredInteractions WHERE AssignmentID = 3133200;
RESULT

STEP 2: Get a list of all the Interactions marked 'Complete' and assign them an InspectionID that I generate on the fly.
CREATE TABLE #InspectionsByCompleteDate ( AssignmentID BIGINT NULL, InspectionID UNIQUEIDENTIFIER NOT NULL, LastInteractionID BIGINT NULL, CompleteDateStart DATETIME NULL, CompleteDateEnd DATETIME NULL, CONSTRAINT [PK_Inspection_InspectionID] PRIMARY KEY CLUSTERED ([InspectionID]), ); INSERT INTO #InspectionsByCompleteDate SELECT AssignmentId, NEWID(), InteractionId LastInteractionID, StartTime CompleteDateStart, EndTime CompleteDateEnd FROM #FilteredInteractions WHERE InteractionEvent = 'Complete' AND AssignmentID = 3133200 SELECT * FROM #InspectionsByCompleteDate
RESULT:

STEP 3: Create a temp table for holding the inspections (this will be a real table after I get this issue worked out). Also declare some variables for comparison in the upcoming steps
CREATE TABLE #Inspections ( InspectionID NVARCHAR(128) NULL, AssignmentID BIGINT NULL, InteractionID BIGINT NULL, ); DECLARE @CurInspectionID NVARCHAR(128) DECLARE @CurAssignmentID BIGINT DECLARE @CurInteractionID BIGINT DECLARE @PrevInspectionID NVARCHAR(128) DECLARE @PrevAssignmentID BIGINT DECLARE @PrevInteractionID BIGINT
STEP 4: Loop through the InspectionsByCompleteDate table and get the current values and the previous values for an inspection based on the assignmentID
WHILE EXISTS(SELECT 1 FROM #InspectionsByCompleteDate) BEGIN -- GET CURRENT VALUES SELECT TOP (1) @CurAssignmentID = ibcd.AssignmentID, @CurInspectionID = ibcd.InspectionID, @CurInteractionID = ibcd.LastInteractionID FROM #InspectionsByCompleteDate ibcd ORDER BY ibcd.CompleteDateStart DESC; --GET Previous Values SELECT TOP (1) @PrevAssignmentID = ibcd.AssignmentID, @PrevInspectionID = ibcd.InspectionID, @PrevInteractionID = ibcd.LastInteractionID FROM #InspectionsByCompleteDate ibcd WHERE ibcd.LastInteractionID < @CurInteractionID AND ibcd.AssignmentID = @CurAssignmentID ORDER BY ibcd.CompleteDateStart DESC; SELECT @CurAssignmentID CurAssignment, @CurInspectionID CurInspection, @CurInteractionID CurInteraction, @PrevAssignmentID PrevAssignment, @PrevInspectionID PrevInspection, @PrevInteractionID PrevInteraction;
RESULTS: The results vary based on how many complete interactions there were on an assignment

STEP 5: Insert all interactions from #FilteredInteractions into the #Inspections table where the interactionID > the previous interaction id and greater than or equal to the current interaction id. Then if the previous interaction id is null insert everything for that assignment with an interactionID greater than the currentinteractionid but with a new inspection id because everything after the last complete interaction is a new inspection. Then set all variables to null for the next iteraction through the loop.
DELETE FROM #InspectionsByCompleteDate WHERE LastInteractionID = @CurInteractionID; INSERT INTO #Inspections SELECT @CurInspectionID InspectionID, ii.AssignmentId, ii.InteractionID FROM #FilteredInteractions ii WHERE ii.AssignmentId = @CurAssignmentID AND (ii.InteractionId > @PrevInteractionID Or @PrevInteractionID IS NULL) AND ii.InteractionId <= @CurInteractionID IF @PrevInteractionID IS NULL BEGIN DECLARE @TempInspectionID UNIQUEIDENTIFIER = NEWID() INSERT INTO #Inspections SELECT @TempInspectionID, ii.AssignmentId, ii.InteractionID FROM #FilteredInteractions ii WHERE ii.AssignmentId = @CurAssignmentID AND ii.InteractionID > @CurInteractionID END SELECT @CurAssignmentID = NULL, @CurInspectionID= NULL, @CurInteractionID = NULL, @PrevAssignmentID = NULL, @PrevInspectionID = NULL, @PrevInteractionID = NULL END SELECT * FROM #Inspections ORDER BY InteractionID
RESULTS: As you can see the InteractionDI 11642262 is duplicated which from what I see shouldn't be possible and is the issue I'm having.

BELOW IS THE ENTIRE QUERY PUT TOGETHER
Spoiler

New Topic/Question
Reply



MultiQuote



|