Sql 如何从重复的行号中找到最小值?
我需要找到“LastUpdatedAt”的最小日期值,但只能在相同rowId之间 我制作了一个临时表“results”,其中包括“rowId”和“LastUpdatedAt”字段。我需要找到RowId的最短日期。例如,如果有两个rowId为“9”的条目,我必须找到最先发生的LastUpdatedAt。我尝试使用MINLastUpdatedAt,但它只返回整个表的最小日期 创建表格结果 罗维德, 上次更新日期\u T 插入到结果中 选择H.RowId,H.LastUpdatedAt from MemberCarrierMap M Join MemberCarrierMapHistory H on M.RowId=H.RowId 从结果中选择* RowId LastUpdatedAt 9 2010-01-21 17:07:48.640 9 2018-05-14 13:33:04.313 88 2016-11-22 14:49:13.770 更新MemberCarrierMap set CreatedAt=从结果中选择MINLastUpdatedAt 其中CreatedAt为nullSql 如何从重复的行号中找到最小值?,sql,sql-server,Sql,Sql Server,我需要找到“LastUpdatedAt”的最小日期值,但只能在相同rowId之间 我制作了一个临时表“results”,其中包括“rowId”和“LastUpdatedAt”字段。我需要找到RowId的最短日期。例如,如果有两个rowId为“9”的条目,我必须找到最先发生的LastUpdatedAt。我尝试使用MINLastUpdatedAt,但它只返回整个表的最小日期 创建表格结果 罗维德, 上次更新日期\u T 插入到结果中 选择H.RowId,H.LastUpdatedAt from Me
我试过了,但它只找到了整个表的最小值。是否只需找到一个rowID的最短日期?是否需要关联子句:
update MemberCarrierMap
set CreatedAt = (select MIN(LastUpdatedAt) from #results r where r.rowId = MemberCarrierMap.rowId)
Where CreatedAt is null;
戈登,这很管用,非常感谢。