I'm kind of fried today, but a quick fix would be to spread this out over 2 queries & a little munging in the Tcl layer:
set selection [ns_db select $db "select distinct root_msg_id from bboard"]
foreach rmi [blahblah $selection] {
ns_write [database_to_tcl_string $db "
select * from (
select message from bboard where root_msg_id = $rmi
order by posting_date desc
) where rownum = 1"
]
}
or whatever the pgSQL variant of rownum is
Did you already go over something like this?
You could probably combine the queries, but, as I said, I'm not all here today. I'm also a little unclear on your requirement here - could you elaborate on your final paragraph? ALL & ONE are sort of opposite in my mind; also 'them' I'm a little unclear on - is 'them' all the posts in a thread, or do you want to order the set of most recent posts?