Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.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 如何从重复的行号中找到最小值?_Sql_Sql Server - Fatal编程技术网

Sql 如何从重复的行号中找到最小值?

Sql 如何从重复的行号中找到最小值?,sql,sql-server,Sql,Sql Server,我需要找到“LastUpdatedAt”的最小日期值,但只能在相同rowId之间 我制作了一个临时表“results”,其中包括“rowId”和“LastUpdatedAt”字段。我需要找到RowId的最短日期。例如,如果有两个rowId为“9”的条目,我必须找到最先发生的LastUpdatedAt。我尝试使用MINLastUpdatedAt,但它只返回整个表的最小日期 创建表格结果 罗维德, 上次更新日期\u T 插入到结果中 选择H.RowId,H.LastUpdatedAt from Me

我需要找到“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为null
我试过了,但它只找到了整个表的最小值。是否只需找到一个rowID的最短日期?

是否需要关联子句:

update MemberCarrierMap 
set CreatedAt = (select MIN(LastUpdatedAt) from #results r where r.rowId = MemberCarrierMap.rowId)
Where CreatedAt is null;

戈登,这很管用,非常感谢。