tags:

views:

86

answers:

5
+5  A: 

If you're dealing with SQL Server try

Select Top 6 RecordID, NoOfTime, Day&Time from (table) order by Day&Time DESC
cmsjr
A: 
SELECT RecordID, NoofTime, Day&Time FROM table ORDER BY Day&Time DESC LIMIT 6;

depending on your SQL engine you will probably have to put some quotes around Day&Time.

The above syntax works with Mysql and PostgreSQL, for varous syntax used for this kins of request you can have a look there select-limit

kriss
A: 

How about

SELECT TOP(6) *
FROM [Trips]
ORDER BY [Day&Time] DESC

I'm not sure what you mean by "in one line," but this will fetch the 6 most recent records.

Aaron
cmsjr beat me to it. This is essentially the same as his solution, and yes, the query may be slightly different if you're using a database other than SQL Server.
Aaron
A: 

For the same line portion, you can use a cursor if you are using SQL server 2008 (might work with 2005 I am not sure).

David Brunelle
+2  A: 

Oracle

SELECT x.* 
  FROM (SELECT t.* 
          FROM TABLE t 
      ORDER BY day&time DESC) x
 WHERE ROWNUM <= 6

SQL Server

  SELECT TOP 6 t.* 
    FROM TABLE t 
ORDER BY day&time DESC

MySQL/Postgres

  SELECT t.* 
    FROM TABLE t 
ORDER BY day&time DESC
   LIMIT 6
OMG Ponies
Mind that you really shouldn't be naming a column using the ampersand character - it will likely have to be escaped to work.
OMG Ponies
Your Oracle sample doesn't work because the ORDER BY doesn't get applied until AFTER the WHERE clause is evaluated for each row. The idiom for Oracle is "select * from (select t.* from t order by x) where rownum <= n"
kurosch
OMG Ponies
why the 'FROM TABLE' and 't.*' syntax. Isn't SQL verbose enough without using the most verbose syntax ? Also I do not used Oracle but I wonder there is no shorthand for getting only some results from result set... I guess I will google for this one.
kriss
@kriss: `t` is a table alias - it's required when accessing tables with identically named columns in order for the db to know which column to use. It also means less typing, and is a good habit to have. Regarding "some of the results from the resultset" - there's ANSI SQL, but that doesn't mean every DB implements those.
OMG Ponies