[英]How to track changes in records in SQL Server?
I have the following table which has tracking records of all students. 我有下表,其中包含所有学生的跟踪记录。
|==========================================|
| ID | Department | Date |
|==========================================|
| 001 | English | Feb 3 2017 |
| 001 | English | Feb 4 2017 |
| 001 | Science | Mar 1 2017 |
| 001 | Science | Apr 2 2017 |
| 001 | Maths | Apr 7 2017 |
| 002 | Maths | Feb 1 2017 |
| 002 | Maths | Apr 7 2017 |
| 003 | Maths | Apr 3 2017 |
| 004 | Science | Feb 1 2017 |
| 004 | Maths | Apr 7 2017 |
|==========================================|
I need to fetch the previous record just before when the student has changed the department. 我需要在学生更换系之前获取以前的记录。 For the example above, the record set returned should be
对于上面的示例,返回的记录集应为
For 001, 对于001,
| 001 | English | Feb 4 2017 |
| 001 | Science | Apr 2 2017 |
For 002 and 003 对于002和003
No changes 没有变化
For 004 对于004
| 004 | Science | Feb 1 2017 |
There is also a possibility that the same user can change back to the same department. 同一用户也有可能改回同一部门。 for example, user001 can change from dept a to dept b to dept c and back to dept a.
例如,user001可以从部门a更改为部门b再更改为部门c,然后再更改为部门a。 I have read about T-SQL send and receive.
我已经阅读了有关T-SQL发送和接收的信息。 But not sure if that would help in this scenario.
但是不确定在这种情况下是否有帮助。 Please help.
请帮忙。
One way to do it is to use ROW_NUMBER
function with partitioning to detect when the value of the Department
column changes. 一种方法是对分区使用
ROW_NUMBER
函数,以检测Department
列的值何时更改。
Sample data 样本数据
DECLARE @T TABLE (ID int, Department nvarchar(100), dt date);
INSERT INTO @T (ID, Department, dt) VALUES
(1, 'English', 'Feb 3 2017'),
(1, 'English', 'Feb 4 2017'),
(1, 'Science', 'Mar 1 2017'),
(1, 'Science', 'Apr 2 2017'),
(1, 'Maths ', 'Apr 7 2017'),
(2, 'Maths ', 'Feb 1 2017'),
(2, 'Maths ', 'Apr 7 2017'),
(3, 'Maths ', 'Apr 3 2017'),
(4, 'Science', 'Feb 1 2017'),
(4, 'Maths ', 'Apr 7 2017');
Query 询问
WITH
CTE
AS
(
SELECT
ID
,Department
,dt
,ROW_NUMBER() OVER (PARTITION BY ID, Department ORDER BY dt DESC) AS rnPart
,ROW_NUMBER() OVER (PARTITION BY ID ORDER BY dt DESC) AS rnID
FROM @T
)
SELECT
ID
,Department
,dt
FROM CTE
WHERE
rnPart = 1
AND rnID <> 1
ORDER BY
ID
,dt
;
Result 结果
+----+------------+------------+
| ID | Department | dt |
+----+------------+------------+
| 1 | English | 2017-02-04 |
| 1 | Science | 2017-04-02 |
| 4 | Science | 2017-02-01 |
+----+------------+------------+
This can be done by creating after insert/update trigger on the tables that you need to track. 这可以通过在您要跟踪的表上在插入/更新触发器之后创建来完成。 Using the logical tables Inserted/Deleted in sql server you can track the newly inserted and modified fields on a table.
使用在sql server中插入/删除的逻辑表,您可以跟踪表上新插入和修改的字段。
Inserted (Logical table):It'll give you the details of the newly inserted records/updated values (column values). 插入(逻辑表):它将为您提供新插入的记录/更新值(列值)的详细信息。
Deleted(Logical Table): It'll give you the old value of a field before it got modified/deleted. Deleted(逻辑表):它将为您提供字段的旧值,然后对其进行修改/删除。
I'm not that sure about it but you can get help with the concept of stored procedures. 我不确定,但是您可以在存储过程的概念上获得帮助。 They are functions which are a part of database.
它们是数据库的一部分。 You can set the display when you make the query
您可以在查询时设置显示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.