sql server 2008 - SQL Syntax for selecting the ID column and latest date from two tables -


i have system users can complete 2 types of tests. need list of distinct users sorted date of latest test completion.

for purpose tables may set out as:

test_users  - test_user_id (int)  test_1_results  - test_1_result_id (int)  - test_user_id (int)  - date_of_completion (datetime)  test_2_results  - test_2_result_id (int)  - test_user_id (int)  - date_of_completion (datetime) 

i can happily list of different test_user_id's using union follows:

select test_user_id test_1_results union select test_user_id test_2_results 

and sort results 1 of these tables using order date_of_completion desc not know how union or if best way proceed.

ultimately able wrap query within others like:

select * test_users test_user_id in (     //the query asking ) some_criteria 

but far haven't had luck doing while using union , not sure doing wrong.

the reason trying admin user needs able view list of has completed tests recent completions @ top.

i familiar sql, have never had use union operator before.

this should work

 select test_user_id, max(date_of_completion) doc  (       select test_user_id, date_of_completion test_1_results        union      select test_user_id , date_of_completion test_2_results   ) temptab  group test_user_id  order doc desc 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -