如何获取一组的最新值
How to get the latest value for a group
我从下面给出的查询中得到 15 行,获得的值也显示在下图中
SELECT * FROM [SS_MatrixLearners]
WHERE ssml_Id_learnercourse=7390 and ssml_id_matrix = 29 and ssml_Id_topic=47
order by SSML_Id_LevelKey, SSML_Id_LevelValue
对于所有这些值,我还想要 SSML_CompletedByUserName
的单个值,它在 SSML_CompletionDate
列中具有最新日期。
请帮助我如何获得该价值。仅供参考,我正在使用 c#
在 Web 表单中使用它
我想出了以下解决方案。
SELECT * into #temp FROM
( SELECT * FROM [SS_MatrixLearners] ORDER BY SSML_ID_LevelKey, SSML_Id_LevelValue) As rn
FROM [SS_MatrixLearners] where ssml_Id_learnercourse = 7390 and ssml_id_matrix = 29 and ssml_Id_topic = 47) t
WHERE rn = 1
Update #temp SET SSML_CompletedByUserName = (select top 1 ISNULL(SSML_CompletedByUserName,'')
From [SS_MatrixLearners]
WHERE ssml_Id_learnercourse=7390 and ssml_id_matrix = 29 and ssml_Id_topic=47
ORDER by SSML_CompletionDate desc
)
SELECT * from #temp order by SSML_Id_LevelKey, SSML_Id_LevelValue DROP table #temp
如果能写一些优化查询就好了
我从下面给出的查询中得到 15 行,获得的值也显示在下图中
SELECT * FROM [SS_MatrixLearners]
WHERE ssml_Id_learnercourse=7390 and ssml_id_matrix = 29 and ssml_Id_topic=47
order by SSML_Id_LevelKey, SSML_Id_LevelValue
SSML_CompletedByUserName
的单个值,它在 SSML_CompletionDate
列中具有最新日期。
请帮助我如何获得该价值。仅供参考,我正在使用 c#
我想出了以下解决方案。
SELECT * into #temp FROM ( SELECT * FROM [SS_MatrixLearners] ORDER BY SSML_ID_LevelKey, SSML_Id_LevelValue) As rn FROM [SS_MatrixLearners] where ssml_Id_learnercourse = 7390 and ssml_id_matrix = 29 and ssml_Id_topic = 47) t WHERE rn = 1 Update #temp SET SSML_CompletedByUserName = (select top 1 ISNULL(SSML_CompletedByUserName,'') From [SS_MatrixLearners] WHERE ssml_Id_learnercourse=7390 and ssml_id_matrix = 29 and ssml_Id_topic=47 ORDER by SSML_CompletionDate desc ) SELECT * from #temp order by SSML_Id_LevelKey, SSML_Id_LevelValue DROP table #temp
如果能写一些优化查询就好了