sql-server - Track the changes of a few columns in an existing table leveraging primary keys?
问题描述
I'm currently trying to track the changes of a few columns (let's call them col1
& col2
) in a SQL Server table. The table is not being "updated/inserted/deleted" over time; new records are just being added to it (please see below 10/01 vs 11/01).
My end-goal would be to run a SQL query or stored procedure that would highlight the changes overtime using primary keys following the framework:
PrimaryKey | ColumnName | BeforeValue | AfterValue | Date
e.g.
Original table:
+-------+--------+--------+--------+
| PK1 | Col1 | Col2 | Date |
+-------+--------+--------+--------+
| 1 | a | e | 10/01 |
| 1 | b | e | 11/01 |
| 2 | c | e | 10/01 |
| 2 | d | f | 11/01 |
+-------+--------+--------+--------+
Output:
+--------------+--------------+---------------+--------------+--------+
| PrimaryKey | ColumnName | BeforeValue | AfterValue | Date |
+--------------+--------------+---------------+--------------+--------+
| 1 | Col1 | a | b | 11/01 |
| 2 | Col1 | c | d | 11/01 |
| 2 | Col2 | e | f | 11/01 |
+--------------+--------------+---------------+--------------+--------+
Any help appreciated.
解决方案
这是一些有点笨拙但似乎有效的代码,基本上对于每一行,我都尝试找到具有不同值的较早行。这会进行两次,一次用于 Col1,一次用于 Col2。
为了使它工作,我必须添加一个唯一的 PK 字段,我不知道您是否有,您可以轻松地将其作为识别字段添加到您的真实表或用于计算的表中。
declare @TestTable table (PK int, PK1 int, Col1 varchar(1), Col2 varchar(1), [Date] date)
insert into @TestTable (PK, PK1, Col1, Col2, [Date])
select 1, 1, 'a', 'e', '10 Jan 2018'
union all select 2, 1, 'b', 'e', '11 Jan 2018'
union all select 3, 2, 'c', 'e', '10 Jan 2018'
union all select 4, 2, 'd', 'f', '11 Jan 2018'
select T1.[Date], T1.PK1, 'Col1', T2.Col1, T1.Col1
from @TestTable T1
inner join @TestTable T2 on T2.PK = (
select top 1 PK
from @TestTable T21
where T21.PK1 = T1.PK1 and T21.Col1 != T1.Col1 and T21.[Date] < T1.[Date]
order by T21.[Date] desc
)
union all
select T1.[Date], T1.PK1, 'Col2', T3.Col2, T1.Col2
from @TestTable T1
inner join @TestTable T3 on T3.PK = (
select top 1 PK
from @TestTable T31
where T31.PK1 = T1.PK1 and T31.Col2 != T1.Col2 and T31.[Date] < T1.[Date]
order by T31.[Date] desc
)
order by [Date], PK1
推荐阅读
- python - 如何使用 Python 代码(不是 CLI)获取 TensorFlow Estimator 版本
- node.js - 在云端使用 node.js 对加密文件进行 GPG 解密
- neural-network - 理解Pytorch中LSTMCell的后向机制
- discord - 如何为 Discord 机器人制作每台服务器、可更改的前缀
- c - 用户空间程序中的“易失性”是否倾向于表示错误?
- regex - VBScript 正则表达式无法获取状态码
- python - 蟒蛇| 这个字符串中的什么是制动新线?
- php - Why is my PHP code overriding my HTML code?
- .net - 对于 .NET Web 应用程序,是否可以将 Apache HTTPD 作为 Web 服务器,将 IIS 作为应用程序服务器?
- html - Css:如何将高度设置为父母宽度的 100%