Sql server 更改数据捕获-仅获取不同的最新更改

Sql server 更改数据捕获-仅获取不同的最新更改,sql-server,cdc,Sql Server,Cdc,我试图只从某个变更数据捕获表中查找所有不同的和最新的变更。这是桌子的快照。 我尝试使用此查询: select DISTINCT StudentUSI, sys.fn_cdc_map_lsn_to_time(__$start_lsn) TransactionTime, __$operation Operation, LastModifiedDate from cdc.Student_CT where __$operation in (1,2,4) ORDER BY StudentUSI; 选择

我试图只从某个变更数据捕获表中查找所有不同的和最新的变更。这是桌子的快照。

我尝试使用此查询:

select DISTINCT StudentUSI, sys.fn_cdc_map_lsn_to_time(__$start_lsn) TransactionTime, __$operation Operation, LastModifiedDate from cdc.Student_CT where __$operation in (1,2,4) ORDER BY StudentUSI; 选择DISTINCT StudentUSI、sys.fn\u cdc\u map\u lsn\u to\u time(\uuu$start\u lsn)TransactionTime、\uu$operation operation、LastModifiedDate 来自cdc.Student\u CT 其中(1,2,4)中的u u$运算 学生订购; 但返回的结果是:

另外,如果我尝试使用GROOUP BY,它会说:

列“cdc.Student\u CT.\u uu$start\u lsn”在选择列表中无效 因为它不包含在聚合函数中,或者 按子句分组

有没有其他方法可以让我获得有关StudentUSI值的最新变化? 谢谢大家!

使用行号()


除非使用聚合函数,否则必须按所有列分组谢谢!成功了。你能解释一下吗。我不熟悉SQL查询。row_number()将一个不断递增的数字应用于
分区by
子句中定义的组,从
order by
中定义的第一行开始。因此,在该语句中,从1开始到n的行号将应用于每个StudentUSI,从最新的LastModifiedDate开始,因为order by是降序的。在此语句末尾,where子句只为每个StudentUSI返回一行,这是最新的一行。删除
其中RN=1
,您可以看到@SkaranjitThank逻辑是如何工作的感谢您的解释,这确实清除了查询。真棒:D
select * from 
(select DISTINCT StudentUSI, sys.fn_cdc_map_lsn_to_time(__$start_lsn) TransactionTime, __$operation Operation, LastModifiedDate ,row_number() over(partition by StudentUSI order by LastModifiedDate desc) as rn
from cdc.Student_CT
where __$operation in (1,2,4))a where rn=1