Select不同表的字段最大值

Select the maximum of field from different tables

我尝试 运行 没有 select 最大 LastUpdatedOn 部分的查询。有用。但我不确定如何 select 来自特定项目 ID 的文档、说明、问题的 LastUpdatedOn 的最大值。

SELECT {Project}.[Number],{Project}.[Name],{User}.[Last_Login],
(
SELECT MAX(LastUpdatedOn) AS max_LastUpdatedOn
FROM
(
SELECT {Question}.[LastUpdatedOn]
UNION ALL
SELECT {Document}.[LastUpdatedOn]
UNION ALL
SELECT {Instruction}.[LastUpdatedOn]
) A
)[max_LastUpdatedOn]

From {Project}

INNER JOIN {ProjectParticipant} ON {Project}.[Id] = {ProjectParticipant}.[ProjectId]
INNER JOIN {User} ON ({ProjectParticipant}.[UserId] = {User}.[Id] AND {User}.[Username] = @UserId)
INNER JOIN {Document} ON {Project}.[Id] = {Document}.[ProjectId]
INNER JOIN {Instruction} ON {Project}.[Id] = {Instruction}.[ProjectId]
INNER JOIN {Question}  ON {Project}.[Id] = {Question}.[ProjectId]

GROUP BY {Project}.[Number],
         {Project}.[Name],
         {User}.[Last_Login],

ORDER BY {Project}.[Number]

我收到以下错误 列 'PORTAL.OSUSR_E2R_QUESTIONS_T9.LASTUPDATEDON' 在 select 列表中无效,因为它未包含在聚合函数或 GROUP BY 子句中。

我认为应该将您的查询拆分为每个 "LastUpdateOn" 的 3 个子查询。 所以你先拿: {Project}.[Number],{Project}.[Name],{User}.[Last_Login], {Document}.[LastUpdatedOn]

第二个: {Project}.[Number],{Project}.[Name],{User}.[Last_Login], {Instruction}.[LastUpdatedOn]

完成: {Project}.[Number],{Project}.[Name],{User}.[Last_Login], {Question}.[LastUpdatedOn]

现在您可以:

SELECT RESULT.NUMBER, RESULT.NAME, RESULT.LAST_LOGIN, MAX(RESULT.LASTUPDATEON) 
  FROM (SELECT {Project}.[Number],{Project}.[Name],{User}.[Last_Login], {Question}.[LastUpdateOn] AS LastUpdateOn 
          FROM {Project}
         INNER JOIN {ProjectParticipant} ON {Project}.[Id] = {ProjectParticipant}.[ProjectId]
         INNER JOIN {User} ON ({ProjectParticipant}.[UserId] = {User}.[Id] AND {User}.[Username] = @UserId)
         INNER JOIN {Question}  ON {Project}.[Id] = {Question}.[ProjectId]
         UNION
        SELECT {Project}.[Number],{Project}.[Name],{User}.[Last_Login], {Document}.[LastUpdateOn] AS LastUpdateOn
          FROM {Project}
         INNER JOIN {ProjectParticipant} ON {Project}.[Id] = {ProjectParticipant}.[ProjectId]
         INNER JOIN {User} ON ({ProjectParticipant}.[UserId] = {User}.[Id] AND {User}.[Username] = @UserId)
         INNER JOIN {Document} ON {Project}.[Id] = {Document}.[ProjectId]
         UNION
        SELECT {Project}.[Number],{Project}.[Name],{User}.[Last_Login], {Instruction}.[LastUpdateOn] AS LastUpdateOn
          FROM {Project}
         INNER JOIN {ProjectParticipant} ON {Project}.[Id] = {ProjectParticipant}.[ProjectId]
         INNER JOIN {User} ON ({ProjectParticipant}.[UserId] = {User}.[Id] AND {User}.[Username] = @UserId)
     INNER JOIN {Instruction} ON {Project}.[Id] = {Instruction}.[ProjectId]) AS RESULT 
GROUP BY RESULT.NUMBER, RESULT.NAME, RESULT.LAST_LOGIN 
ORDER BY RESULT.NUMBER

我认为它应该有效。

好的,您只需要 select 一次约会(最近一次)吗?还是您需要 select 三个日期(每个日期中的最近日期)?我认为如果你只需要最近的日期(只有一个日期),你可以使用查询。

举个例子(我这里不知道怎么画table,耐心点):

项目table:
编号:1
数量:1
姓名:One_Proy

Project_participant table:
项目编号:1
用户ID:1

用户table:
编号:1
用户名:lmessi
LasLogIn:总有一天

问题table:
项目编号:1
最后更新时间:昨天

文档table:
ProjectId:1
最后更新时间:上周

说明table:
项目编号:1
最后更新时间:今天

查询将return:
数字:1
姓名:One_Proy
LAST_LOGIN:总有一天
MAX(LASTUPDATEON):今天