I have this query which grabs the max date record for three different tables. I can do a union on these tables to have them all return their respective dates together. However I am having trouble finding a way to select the max of these three records that are returned. Any ideas?
SELECT MAX (Date_submitted ) as a
FROM [AngelSQL] . [dbo]. [LSN_RESPONSES] , ACCOUNTS , LSN_ENTRIES , GB3_ASSOCIATIONS
where
LSN_RESPONSES . USER_ID = accounts . username
and LoginName = 'admin'
and TakenFromCourseId = 'TT-TC-TS-TD-Test-Test'
and LSN_RESPONSES . PARENT_ID = LSN_ENTRIES .ENTRY_ID
and LSN_RESPONSES . PARENT_ID = GB3_ASSOCIATIONS .LSN_ENTRY_ID
union
SELECT MAX ( dateEnd ) as a
FROM [AngelSQL] . [dbo]. [qti_assessment_attempt] ,dbo . ACCOUNTS, COURSES, qti_question_containers, GB3_ASSOCIATIONS
where accounts . PERSON_ID = qti_assessment_attempt .person_id
and
taken_from_course_container_id = courses. CONTAINER_ID
and
accounts .LoginName = 'admin'
and COURSES . COURSE_ID= 'TT-TC-TS-TD-Test-Test'
and qti_assessment_attempt . question_container_id = qti_question_containers. id
and qti_question_containers . entry_ID = GB3_ASSOCIATIONS .LSN_ENTRY_ID
union
SELECT MAX ( PostDate ) as a
FROM dbo . ACCOUNTS, DiscussionPost , DiscussionForum , GB3_ASSOCIATIONS
where
accounts .USERNAME = DiscussionPost. AuthorId
and
LoginName ='admin'
and
discussionpost . CourseContext = 'TT-TC-TS-TD-Test-Test'
and
DiscussionPost . ForumId = DiscussionForum .ForumId
and DiscussionForum . EntryId = GB3_ASSOCIATIONS .LSN_ENTRY_ID}
Use your query as a subquery/CTE, like this: