M
muskie
I have a table with 6 columns: ID, Name, Date1, Date2, Date3, Date4
Each of the date fields is a different date, and they are not in
chronological order...that is, Date1 could be earlier or later that
Date2 and so on.
I need to pull the top ten records sorted by the most recent dates,
whether that be in Date1, Date2, Date3, or Date4.
I've tried a union join (below), where I join the data in four select
statements, giving each Date column a common FieldName....then I select
from that, sorting by date....then I select from that using distinct to
get only one of each record. If I don't use distinct, I get records
showing up more than once.
This however produces a recordset that is no longer ordered by LDate.
Any other ideas on how to go about this?
Thanks!
Matt
"SELECT DISTINCT TOP 10 ID, Name FROM (" &_
"SELECT * FROM (" &_
"SELECT Name,ID,Date1 AS LDate FROM TheTable" &_
" UNION " &_
"SELECT Name,ID,Date2 AS LDate FROM TheTable" &_
" UNION " &_
"SELECT Name,ID,Date3 AS LDate FROM TheTable" &_
" UNION " &_
"SELECT Name,ID,Date4 AS LDate FROM TheTable" &_
") ORDER BY LDate DESC" &_
");"
Each of the date fields is a different date, and they are not in
chronological order...that is, Date1 could be earlier or later that
Date2 and so on.
I need to pull the top ten records sorted by the most recent dates,
whether that be in Date1, Date2, Date3, or Date4.
I've tried a union join (below), where I join the data in four select
statements, giving each Date column a common FieldName....then I select
from that, sorting by date....then I select from that using distinct to
get only one of each record. If I don't use distinct, I get records
showing up more than once.
This however produces a recordset that is no longer ordered by LDate.
Any other ideas on how to go about this?
Thanks!
Matt
"SELECT DISTINCT TOP 10 ID, Name FROM (" &_
"SELECT * FROM (" &_
"SELECT Name,ID,Date1 AS LDate FROM TheTable" &_
" UNION " &_
"SELECT Name,ID,Date2 AS LDate FROM TheTable" &_
" UNION " &_
"SELECT Name,ID,Date3 AS LDate FROM TheTable" &_
" UNION " &_
"SELECT Name,ID,Date4 AS LDate FROM TheTable" &_
") ORDER BY LDate DESC" &_
");"