Question

I have a table with sample data below:

ID    | RecordID  | Time     | Start/End
1       1111       09:00:10          
5       1111       09:00:12    
13      1111       09:01:10   
24      1111       09:02:30          
27      9999       10:00:10          
29      9999       10:01:22   
30      9999       10:03:10          
38      7777       10:20:10          
59      7777       10:21:10   
60      7777       10:24:10          
71      1111       14:20:10          
72      1111       14:21:10   
75      1111       14:24:10          

How do I query the table to add a 1 to the Start/End column when the RecordID is the first and last instance?

ID    | RecordID  | Time     | Start/End
1       1111       09:00:10          1
5       1111       09:00:12    
13      1111       09:01:10   
24      1111       09:02:30          1
27      9999       10:00:10          1
29      9999       10:01:22   
30      9999       10:03:10          1
38      7777       10:20:10          1
59      7777       10:21:10   
60      7777       10:24:10          1
71      1111       14:20:10          1
72      1111       14:21:10   
75     1111       14:24:10          1

Do I need to loop through the table?

Please note that the RecordID can be in the table more than once throughout the day.

EDIT -----------------------------

Aplogies, i originally stated the ID would be incremental but that is not the case - the above table is what it would look like if i pulled out the data for 1 user.

Was it helpful?

Solution

Query:

SQLFIDDLEEXample

UPDATE t
SET [Start/End] = CASE WHEN t1.ID is null or t2.ID is null
     THEN 1
     WHEN t.RecordID <> t1.RecordID OR t.RecordID <> t2.RecordID
     THEN 1 END 
FROM Table1 t
LEFT JOIN Table1 t1
  ON t1.ID = t.ID - 1
LEFT JOIN Table1 t2
  ON t2.ID = t.ID + 1

Result:

| ID | RECORDID |     TIME | START/END |
|----|----------|----------|-----------|
|  1 |     1111 | 09:00:10 |         1 |
|  2 |     1111 | 09:00:12 |    (null) |
|  3 |     1111 | 09:01:10 |    (null) |
|  4 |     1111 | 09:02:30 |         1 |
|  5 |     9999 | 10:00:10 |         1 |
|  6 |     9999 | 10:01:22 |    (null) |
|  7 |     9999 | 10:03:10 |         1 |
|  8 |     7777 | 10:20:10 |         1 |
|  9 |     7777 | 10:21:10 |    (null) |
| 10 |     7777 | 10:24:10 |         1 |
| 11 |     1111 | 14:20:10 |         1 |
| 12 |     1111 | 14:21:10 |    (null) |
| 13 |     1111 | 14:24:10 |         1 |

EDIT

New Query: SQLFIDDLEExample

;WITH CTE AS (
SELECT *,
ROW_NUMBER()OVER(ORDER BY ID) rnka
FROM Table1)


UPDATE t
SET [Start/End] = CASE WHEN t1.ID is null or t2.ID is null
     THEN 1
     WHEN t.RecordID <> t1.RecordID OR t.RecordID <> t2.RecordID
     THEN 1 END 
FROM CTE t
LEFT JOIN CTE t1
  ON t1.rnka = t.rnka - 1
LEFT JOIN CTE t2
  ON t2.rnka = t.rnka + 1

Result:

| ID | RECORDID |     TIME | START/END |
|----|----------|----------|-----------|
|  1 |     1111 | 09:00:10 |         1 |
|  5 |     1111 | 09:00:12 |    (null) |
| 13 |     1111 | 09:01:10 |    (null) |
| 24 |     1111 | 09:02:30 |         1 |
| 27 |     9999 | 10:00:10 |         1 |
| 29 |     9999 | 10:01:22 |    (null) |
| 30 |     9999 | 10:03:10 |         1 |
| 38 |     7777 | 10:20:10 |         1 |
| 59 |     7777 | 10:21:10 |    (null) |
| 60 |     7777 | 10:24:10 |         1 |
| 71 |     1111 | 14:20:10 |         1 |
| 72 |     1111 | 14:21:10 |    (null) |
| 75 |     1111 | 14:24:10 |         1 |

OTHER TIPS

Edited My answer:

Well, you can use below update queries:

UPDATE t SET t.Start/End = 1 FROM table t INNER JOIN (SELECT MIN(Id) IdMin,MIN(Time) FROM table GROUP BY RecordId) t1 ON t.Id = t1.IdMin

UPDATE t SET t.Start/End = 1 FROM table t INNER JOIN (SELECT NAX(Id) IdMax,MAX(Time) FROM table WHERE Start/End IS NULL GROUP BY RecordId ) t1 ON t.Id = t1.IdMax

This will work:

;WITH CTE AS (
        SELECT t1.ID,
               CASE WHEN t1.RecordID <> ISNULL(t2.RecordID,0) THEN 1 ELSE 0 END [Start/END]
            FROM TableName t1
                LEFT JOIN TableName t2
                ON t1.ID = t2.ID + 1

        UNION ALL

        select t1.ID,
                CASE WHEN t1.RecordID <> ISNULL(t2.RecordID,0) THEN 1 ELSE 0 END [Start/END]
            FROM TableName t1
                LEFT JOIN TableName t2
                ON t1.ID = t2.ID - 1

)
UPDATE TableName
SET [Start/END] = 1
    FROM TableName t
        INNER JOIN cte
        ON t.ID = CTE.ID
    WHERE cte.[Start/END] = 1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top