我有一张包含以下示例数据的表格:
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
当RecordID是第一个和最后一个实例时,如何查询表以向Start/End
列添加1?
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
我是否需要遍历表格?
请注意,RecordID可以在一天内不止一次出现在表格中。
EDIT -----------------------------
Aplogies,我最初声明ID会是增量的,但事实并非如此 - 如果我为1位用户提取数据,上面的表就是这样。
答案 0 :(得分:4)
查询:
<强> 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
结果:
| 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 |
修改强>
新查询: 的 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
结果:
| 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 |
答案 1 :(得分:0)
编辑我的回答:
好吧,您可以使用以下更新查询:
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
答案 2 :(得分:0)
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