我在数据库中有一个带有主键的表,还有一个“第二个”键。第二个键可以在表中多次出现相同的值,但通常我只想返回该第二个键的最新行。我有一个现有的查询在下面工作,但我觉得它非常难看,应该有一个更简单的方法来做这个而不是创建一个表变量,通过一个循环,并在每次传递时插入表变量循环。我这么做太难了吗?
declare @RowCnt int
declare @MaxRows int
declare @secondID as uniqueidentifier
DECLARE @retList TABLE(
firstGUID uniqueidentifier,
secondGUID uniqueidentifier,
name nvarchar(50),
DateCreated datetime
)
select @RowCnt = 1
declare @Import table (rownum int IDENTITY (1, 1) Primary key NOT NULL , secondGUID uniqueidentifier)
insert into @Import (secondGUID) SELECT DISTINCT dbo.TestTable.secondGUID FROM dbo.TestTable
select @MaxRows=count(*) from @Import
while @RowCnt <= @MaxRows
begin
select @secondID=secondGUID from @Import where rownum = @RowCnt
INSERT INTO @retList
SELECT TOP (1) firstGUID,secondGUID,name,datecreated
FROM dbo.TestTable
WHERE dbo.TestTable.secondGUID = @secondID
ORDER BY DateCreated Desc
Set @RowCnt = @RowCnt + 1
END
select * from @retList
编辑: 例如,假设表中有这些值
firstGUID secondGUID Name DateCreated
EAD50999-E9B1-43F0-9FA6-615405FA5A9A 6163B6ED-6AF4-494E-ACE6-184F4804847B Test1 2014-04-11 15:12:36.303
A9645486-1021-4E98-92AC-1205CC3FB9D3 6163B6ED-6AF4-494E-ACE6-184F4804847B Test2 2014-04-10 15:21:46.087
DEE375BB-BFAF-44BE-AC64-06D7702E2ACB 3BD0A2F0-4E44-43B9-BD24-003B518609C7 Test3
2014-04-11 15:22:37.097
我只希望返回Test1和Test3行。
答案 0 :(得分:2)
您可以使用SQLServer的分析功能:
select firstGUID, secondGUID, name, datecreated
from (select t.*,
rank() over (partition by secondGUID order by datecreated desc) r
from TestTable t) ilv
where r=1
答案 1 :(得分:1)
我并非100%确定我理解您的要求,但听起来您只想选择包含最大DateCreated的行。这样做的常用方法是使用group by子句的子选择连接,例如:
select tt.*
from TestTable tt
join (
select firstguid, max(DateCreated) as maxdate
from TestTable
group by firstguid
) gtmp on tt.firstguid = gtmp.firstguid and tt.dateCreated = gtmp.maxdate