Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 选择值的最新更改_Sql Server 2008 - Fatal编程技术网

Sql server 2008 选择值的最新更改

Sql server 2008 选择值的最新更改,sql-server-2008,Sql Server 2008,我有表利率: CustId Dt IntRt 1 201401 1 1 201402 2 1 201403 2 1 201404 2 1 201405 3 2 201401 1 2 201402 1 2 201403 2 2 201404 2 我想为每个客户ID选择IntRt的最后更改日期和更改内容。格式如Dt、原始IntRt、新IntRt 因此,select

我有表利率:

CustId   Dt   IntRt 
1      201401   1
1      201402   2
1      201403   2
1      201404   2
1      201405   3
2      201401   1
2      201402   1
2      201403   2
2      201404   2
我想为每个客户ID选择IntRt的最后更改日期和更改内容。格式如Dt、原始IntRt、新IntRt

因此,select将返回:

CustID  Change
1       201405,2,3
2       201403,1,2
谢谢大家!

更新:

我尝试了如下查询:

从利息中选择maxDt,其中custID=custID和IntRt IntRt

我知道这显然是错误的,因为我比较相同的值,所以它不会返回任何结果,但我不知道该怎么做

WITH cte AS (
  SELECT 
    rn = RANK() OVER (PARTITION BY CustID ORDER BY Dt DESC), *
  FROM INTERESTRATE
)
SELECT
  c1.CustId,
  c1.Dt,
  OriginalIntRt = (SELECT TOP 1 IntRt FROM cte c2 WHERE c1.CustId = c2.CustId AND c1.IntRt <> c2.IntRt ORDER BY dt DESC),
  c1.IntRt AS NewIntRt
FROM cte c1
WHERE c1.rn = 1