Friday, March 9, 2012

opposite query

Hi,
Need some help with a query, this is what it looks like:
SELECT TOP 10 *
FROM [dbo].[R_Reenval]
Now how do i get the bottom/last 10 records? I tried replacing the
"Top" with "Bottom", "Last"; they both dnt work.
Any help would be highly appreciated. Thanx
TOP doesn't give you the "first" rows as such concept doesn't exist. Rows are not ordered. But since
TOP is applied after ORDER BY, you can do something like:
SELECT TOP 10 col1, col2
FROM tbl
ORDER BY col
To get "bottom" based on the same column order, you can do:
SELECT TOP 10 col1, col2
FROM tbl
ORDER BY col DESC
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"amatuer" <njoosub@.gmail.com> wrote in message
news:1160549556.997163.269380@.b28g2000cwb.googlegr oups.com...
> Hi,
> Need some help with a query, this is what it looks like:
> SELECT TOP 10 *
> FROM [dbo].[R_Reenval]
> Now how do i get the bottom/last 10 records? I tried replacing the
> "Top" with "Bottom", "Last"; they both dnt work.
> Any help would be highly appreciated. Thanx
>
|||Do a reverse sort.
SELECT TOP n
{ColumnList}
FROM MyTable
ORDER BY {SortCriteria}
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
"amatuer" <njoosub@.gmail.com> wrote in message
news:1160549556.997163.269380@.b28g2000cwb.googlegr oups.com...
> Hi,
> Need some help with a query, this is what it looks like:
> SELECT TOP 10 *
> FROM [dbo].[R_Reenval]
> Now how do i get the bottom/last 10 records? I tried replacing the
> "Top" with "Bottom", "Last"; they both dnt work.
> Any help would be highly appreciated. Thanx
>
|||Thanx alot
Tibor Karaszi wrote:[vbcol=seagreen]
> TOP doesn't give you the "first" rows as such concept doesn't exist. Rows are not ordered. But since
> TOP is applied after ORDER BY, you can do something like:
> SELECT TOP 10 col1, col2
> FROM tbl
> ORDER BY col
> To get "bottom" based on the same column order, you can do:
> SELECT TOP 10 col1, col2
> FROM tbl
> ORDER BY col DESC
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "amatuer" <njoosub@.gmail.com> wrote in message
> news:1160549556.997163.269380@.b28g2000cwb.googlegr oups.com...

No comments:

Post a Comment