473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Getting bottom N records from DB table to Datatable

VMI
How can I get the bottom N records from an Access table and store them in my
DataTable?
For example, in my Access table with 2000 records, if I want to display
records 151-200 (with ID as PK), my query
would be:

"select top 50 * from audit where ID > 150 order by ID asc"

Since ID is type AutoNumber, I'll see records 151-200. But once I'm
displaying them, how can I go back to seeing 101-150? I tried:

"select top 50 * from audit where ID <= 150 order by ID desc" and it'll show
me the correct data, but in the wrong order (from 150-101).

Thanks.
Nov 16 '05 #1
3 2254
"VMI" <vo******@yahoo.com> wrote in
news:Of**************@TK2MSFTNGP10.phx.gbl:
How can I get the bottom N records from an Access table and
store them in my DataTable?
For example, in my Access table with 2000 records, if I want to
display records 151-200 (with ID as PK), my query
would be:

"select top 50 * from audit where ID > 150 order by ID asc"

Since ID is type AutoNumber, I'll see records 151-200. But once
I'm displaying them, how can I go back to seeing 101-150? I
tried:

"select top 50 * from audit where ID <= 150 order by ID desc"
and it'll show me the correct data, but in the wrong order (from
150-101).


To see records 101-150, can't you just do "select top 50 * from audit
where ID > 100 order by ID asc"?
Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #2
Okay guys, issuing a random Id, such as 100 when helping this guy
really isn't going to help. We all know that database primary keys
can disappear and leave holes, that he might be doing an additional
filter on top of the entire thing, or all sorts of other issues. The only
real way to do this is very expensive, and I'm sure that is what he
is trying to avoid.

You have:
1. Get the rowcount
2. Subtract your desired final records
3. Run the query with this row value.
4. Run the query using a not int clause with the ids retrieved in the previous
step.

Super expensive. And if you need to do this arbitrarily then it won't be a top
N at all, but a ROWCOUNT set, which is much slower than a top N in many
cases, especially on partially sorted or almost sorted heaps. If the table is
indexed,
then or not often update, then eh, you can write optimizations for the entire
process.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
news:Xn**********************************@207.46.2 48.16...
"VMI" <vo******@yahoo.com> wrote in
news:Of**************@TK2MSFTNGP10.phx.gbl:
How can I get the bottom N records from an Access table and
store them in my DataTable?
For example, in my Access table with 2000 records, if I want to
display records 151-200 (with ID as PK), my query
would be:

"select top 50 * from audit where ID > 150 order by ID asc"

Since ID is type AutoNumber, I'll see records 151-200. But once
I'm displaying them, how can I go back to seeing 101-150? I
tried:

"select top 50 * from audit where ID <= 150 order by ID desc"
and it'll show me the correct data, but in the wrong order (from
150-101).


To see records 101-150, can't you just do "select top 50 * from audit
where ID > 100 order by ID asc"?
Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

Nov 16 '05 #3
The following should work and is quite cheap:

SELECT *
FROM
(
SELECT TOP 50 * FROM audit
ORDER BY ID DESC
) T1
ORDER BY ID

Though, I would perform a simple query and do the sorting in a DataView.

-Felix

"Justin Rogers" <Ju****@games4dotnet.com> wrote in message news:<eQ**************@TK2MSFTNGP11.phx.gbl>...
Okay guys, issuing a random Id, such as 100 when helping this guy
really isn't going to help. We all know that database primary keys
can disappear and leave holes, that he might be doing an additional
filter on top of the entire thing, or all sorts of other issues. The only
real way to do this is very expensive, and I'm sure that is what he
is trying to avoid.

You have:
1. Get the rowcount
2. Subtract your desired final records
3. Run the query with this row value.
4. Run the query using a not int clause with the ids retrieved in the previous
step.

Super expensive. And if you need to do this arbitrarily then it won't be a top
N at all, but a ROWCOUNT set, which is much slower than a top N in many
cases, especially on partially sorted or almost sorted heaps. If the table is
indexed,
then or not often update, then eh, you can write optimizations for the entire
process.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
news:Xn**********************************@207.46.2 48.16...
"VMI" <vo******@yahoo.com> wrote in
news:Of**************@TK2MSFTNGP10.phx.gbl:
How can I get the bottom N records from an Access table and
store them in my DataTable?
For example, in my Access table with 2000 records, if I want to
display records 151-200 (with ID as PK), my query
would be:

"select top 50 * from audit where ID > 150 order by ID asc"

Since ID is type AutoNumber, I'll see records 151-200. But once
I'm displaying them, how can I go back to seeing 101-150? I
tried:

"select top 50 * from audit where ID <= 150 order by ID desc"
and it'll show me the correct data, but in the wrong order (from
150-101).


To see records 101-150, can't you just do "select top 50 * from audit
where ID > 100 order by ID asc"?
Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: JC | last post by:
Hi, I have a database that imports 4 reports to my "data" table, each report has its own identifier. Then I have a table with Analysts with the report identifier that they are to be assigned to. ...
4
by: Darrel | last post by:
I'm creating a table that contains multiple records pulled out of the database. I'm building the table myself and passing it to the page since the table needs to be fairly customized (ie, a...
3
by: suresh | last post by:
frenz, i need to disable the add new record mode in datarid. i just want to modify the existing records in the grid...but i dont want to add new records..how do i do that? -suresh
12
by: jaYPee | last post by:
I have currently using a dataset to access my data from sql server 2000. The dataset contains 3 tables that is related to each other. parent/child/grandchild relationship. My problem is it's very...
11
by: Siv | last post by:
Hi, I seem to be having a problem with a DataAdapter against an Access database. My app deletes 3 records runs a da.update(dt) where dt is a data.Datatable. I then proceed to update a list to...
4
by: darrel | last post by:
I can grab a random number in vb.net like this: Dim RandomClass As New Random Dim RandomNumber As Integer RandomNumber = RandomClass.Next(1, 26) However, what I want is a random number. Short...
4
by: aqua404 | last post by:
I know this has been discussed, but I can't find a resolution. I have a subform on a form. The table with the data for the main form has 15,000 records. I am opening and then setting...
1
by: lawton | last post by:
Source: this is an access 2003 question My knowledge level: reading books, internet, and trial & error; no formal training I'm trying to get a running sum of what's filtered in a subform which is...
2
by: kurtzky | last post by:
i created a form that should function as follows: i will enter a number in a textbox..then it should query from the database all the records which has that number..these records will have a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.