I need to select different fields from different databases. One of the field is selecting the maximum of the LastUpdateOn datetime field from different tables.
{Project},{Document},{Instruction},{Document}, {User}, {ProjectParticipant} are the different tables
SELECT {Project}.[Number],{Project}.[Name],{User}.[Last_Login],{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 {InstructionType} ON {Project}.[Id] = {Instruction}.[ProjectId]
INNER JOIN {Question} ON {Project}.[Id] = {Question}.[ProjectId]
SELECT MAX(LastUpdatedOn) AS max_LastUpdatedOn
FROM
(
SELECT LastUpdatedOn FROM {Question}
UNION ALL
SELECT LastUpdatedOn FROM {Document}
UNION ALL
SELECT LastUpdatedOn FROM {Instruction}
) A;
GROUP BY {Project}.[Number],
{Project}.[Name],
{User}.[Last_Login]
{A}.[max_LastUpdatedOn]
ORDER BY {Project}.[Number]
But getting an error saying {A} is an unknown entity. I am not sure what is wrong here. Tried using the
SELECT {Project}.[Number],{Project}.[Name],{User}.[Last_Login],
(SELECT MAX(LastUpdatedOn) as max_LastUpdatedOn
FROM
(
SELECT {Question}.[LastUpdatedOn] where {Project}.[Id] = {Question}.[ProjectId]
UNION ALL
Select {Document}.[LastUpdatedOn] where {Project}.[Id] = {Document}.[ProjectId]
UNION ALL
SELECT {Instruction}.[LastUpdatedOn] where {Project}.[Id] = {Instruction}.[ProjectId]
)
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 {Question} ON {Project}.[Id] = {Question}.[ProjectId]
INNER JOIN {Document} ON {Project}.[Id] = {Document}.[ProjectId]
INNER JOIN {Instruction} ON {Project}.[Id] = {Instruction}.[ProjectId]
GROUP BY {Project}.[Number],
{Project}.[Name],
{User}.[Last_Login],
A.[max_LastUpdatedOn]
ORDER BY {Project}.[Number]
Getting an error like The multi-part identifier "A.max_LastUpdatedOn" could not be bound.
{A}
is not an OutSystems Entity, therefore you should not use curly braces to refer it. Curly braces are used to translate from the meta model name in design time to the physical table name in the database.
Since A
is a simple alias of the inner select statement, just using A
should be enough to make it work. For example:
SELECT {Project}.[Number],{Project}.[Name],{User}.[Last_Login],A.[max_LastUpdatedOn]
UPDATE
Regarding the error about The multi-part identifier could not be bound
, it has to do with grouping by something that is not in the from
/join
sources of the query.
I haven’t tried this code below, but something like this should do the trick:
(also, see here more ways on how to select the SQL MAX of multiple columns?)
SELECT {Project}.[Number], {Project}.[Name], {User}.[Last_Login],
CASE
WHEN {Question}.[LastUpdatedOn] >= {Document}.[LastUpdatedOn] AND {Question}.[LastUpdatedOn] >= {Instruction}.[LastUpdatedOn]
THEN {Question}.[LastUpdatedOn]
WHEN {Document}.[LastUpdatedOn] >= {Instruction}.[LastUpdatedOn] THEN {Document}.[LastUpdatedOn]
ELSE {Instruction}.[LastUpdatedOn]
END AS 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 {Question} ON {Project}.[Id] = {Question}.[ProjectId]
INNER JOIN {Document} ON {Project}.[Id] = {Document}.[ProjectId]
INNER JOIN {Instruction} ON {Project}.[Id] = {Instruction}.[ProjectId]
GROUP BY {Project}.[Number],
{Project}.[Name],
{User}.[Last_Login]
ORDER BY {Project}.[Number]