从不同的表中选择最大字段

时间:2015-06-29 13:01:15

标签: outsystems

我尝试在不选择LastUpdatedOn部分的最大值的情况下运行查询。有用。但我不知道如何从特定项目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'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

2 个答案:

答案 0 :(得分:1)

好的,您是否只需要选择一个日期(最近一次)?或者你需要选择三个日期(每个日期最近)?我认为如果你需要最近的日期(只有一个日期),你可以使用查询。

让我举个例子(我不知道如何在这里画一张桌子,所以......耐心等待):

项目表:
Id:1
编号:1
名称:One_Proy

Project_participant表:
ProjectID:1
用户ID:1

用户表:
Id:1
用户名:lmessi
LasLogIn:有一天

问题表:
ProjectId:1
LastUpdateOn:昨天

文件表:
专案编号:1个
LastUpdateOn:LastWeek

指令表:
ProjectId:1
LastUpdateOn:今天

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

答案 1 :(得分:0)

我认为应该在每个" 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

我认为它应该有用。