views:

152

answers:

1

how to show max time in first row and min time in second row for access using vb6

+5  A: 

What about:

SELECT time_value
  FROM (SELECT MIN(time_column) AS time_value FROM SomeTable
        UNION
        SELECT MAX(time_column) AS time_value FROM SomeTable
       )
 ORDER BY time_value DESC;

That should do the job unless there are no rows in SomeTable (or your DBMS does not support the notation).


Simplifying per suggestion in comments - thanks!

SELECT MIN(time_column) AS time_value FROM SomeTable
UNION
SELECT MAX(time_column) AS time_value FROM SomeTable
ORDER BY time_value DESC;


If you can get two values from one query, you may improve the performance of the query using:

SELECT MIN(time_column) AS min_time,
       MAX(time_column) AS max_time
  FROM SomeTable;

A really good optimizer might be able to deal with both halves of the UNION version in one pass over the data (or index), but it is quite easy to imagine an optimizer tackling each half of the UNION separately and processing the data twice. If there is no index on the time column to speed things up, that could involve two table scans, which would be much slower than a single table scan for the two-value, one-row query (if the table is big enough for such things to matter).

Jonathan Leffler
Why bother with the outer query? `SELECT MAX ... UNION ... SELECT MIN ...` should do the trick and order descending too.
Asaph
Umm...good question - not sure why, now...
Jonathan Leffler
Why anyone presume MS SQL Server when the tag on the question is "MDB"?
David-W-Fenton
There was no point in the history of this question that MDB was not in the tags. The history is: Original post: mdb; tags edited by marc_s: mdb vb6; onedaywhen retag: ms-access mdb vb6; Ben McCormack retag: ms-access mdb vb6 vba. So, you just ignored one of the tags.
David-W-Fenton
Mountains and molehills - mea culpa, but...
Jonathan Leffler