gpt4 book ai didi

sql - 如何在 SQL 中获取最大子集的最大记录

转载 作者:搜寻专家 更新时间:2023-10-30 20:33:51 25 4
gpt4 key购买 nike

<分区>

我有以下表格:

训练发生

Id   Training Id   Due Date5    1             09/01/20186    1             09/15/20187    2             09/01/2018

Training Occurrence User

Id   Training Material Occurrence Id   User Id1    5                                 'Chad'2    5                                 'Chad'3    6                                 'Chad'4    6                                 'Chad'5    7                                 'Chad'

My query needs to get the newest Training Material Occurrence User record of the newest Training Material Occurrence table. So if I pass in the user 'Chad'

I would want to see:

Id   Occurrence Id   User     Training Id   Due Date4    6               'Chad'   1             09/15/20185    7               'Chad'   2             09/01/2018

Here is my query:

SELECT tmou.*, tmo.TrainingMaterialId, tmo.DueDate
FROM dbo.TrainingMaterialOccurrenceUser as tmou
INNER JOIN dbo.TrainingMaterialOccurrence as tmo on
tmou.TrainingMaterialOccurrenceId = tmo.Id
AND tmou.Id IN (SELECT MAX(tmou.Id)
FROM dbo.TrainingMaterialOccurrenceUser as tmou
WHERE tmou.UserId = @UserId
AND tmou.TrainingMaterialOccurrenceId IN (SELECT MAX(tmo.Id) as occurrenceId
FROM dbo.TrainingMaterialOccurrence as tmo
WHERE tmo.Id IN (Select TrainingMaterialOccurrenceId FROM dbo.TrainingMaterialOccurrenceUser as tmou1 WHERE tmou1.UserId = @UserId)
GROUP BY tmo.TrainingMaterialId)
GROUP BY tmou.TrainingMaterialOccurrenceId)

如您所见,这是一团糟。关于如何清理它的任何想法。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com