473,668 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SELECT-how to get only the last 20 records?

Hello,
Firstly, sorry for my english.
I have problem with creating SQL statement. I am beginner and I think that
it is very easy to do. Look -
I have to get only the last 20 records from table ABC (f.eg.) and according
to that how to create the SQL statement? F.eg. SELECT name, forename, date
FROM abc WHERE....
You know - how to finish the statement to get only 20 records lastly
inserted into the table?
Thanks and I am waiting for helpfull answers.

Jul 23 '05 #1
3 28759
I'll take a stab at the easy answer.

If there is a timestamp or identity field on this table, you could do:

select top 20 name, etc.
from abc
order by x desc

where x would be the name of the timestamp or identity field.

Without one of those fields, it gets more difficult and I'll leave that
to other experts.

Jul 23 '05 #2
On Mon, 21 Feb 2005 22:03:23 +0100, siatki wrote:
Hello,
Firstly, sorry for my english.
I have problem with creating SQL statement. I am beginner and I think that
it is very easy to do. Look -
I have to get only the last 20 records from table ABC (f.eg.) and according
to that how to create the SQL statement? F.eg. SELECT name, forename, date
FROM abc WHERE....
You know - how to finish the statement to get only 20 records lastly
inserted into the table?
Thanks and I am waiting for helpfull answers.


Hi siatki,

SQL Server won't automatically remember for you when rows were inserted.
You must have a column in your table for this, with smalldatetime or
datetime as datatype and a default of CURRENT_TIMESTA MP. See example
below:

CREATE TABLE abc
( ......
, ......
, InsertedAt smalldatetime NOT NULL DEFAULT CURRENT_TIMESTA MP
, .....
)
go

If you have such a column (or another column that provides a dependable
way to find in what order your rows were inserted), you can get the 20
most recent new rows with

SELECT TOP 20 Column1, Column2, ...., ColumnN
FROM abc
ORDER BY InsertedAt DESC

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #3
thx for answer. I thing that it is good solution. It works.
Uzytkownik "Hugo Kornelis" <hugo@pe_NO_rFa ct.in_SPAM_fo> napisal w
wiadomosci news:b3******** *************** *********@4ax.c om...
On Mon, 21 Feb 2005 22:03:23 +0100, siatki wrote:
Hello,
Firstly, sorry for my english.
I have problem with creating SQL statement. I am beginner and I think that
it is very easy to do. Look -
I have to get only the last 20 records from table ABC (f.eg.) and
according
to that how to create the SQL statement? F.eg. SELECT name, forename, date
FROM abc WHERE....
You know - how to finish the statement to get only 20 records lastly
inserted into the table?
Thanks and I am waiting for helpfull answers.


Hi siatki,

SQL Server won't automatically remember for you when rows were inserted.
You must have a column in your table for this, with smalldatetime or
datetime as datatype and a default of CURRENT_TIMESTA MP. See example
below:

CREATE TABLE abc
( ......
, ......
, InsertedAt smalldatetime NOT NULL DEFAULT CURRENT_TIMESTA MP
, .....
)
go

If you have such a column (or another column that provides a dependable
way to find in what order your rows were inserted), you can get the 20
most recent new rows with

SELECT TOP 20 Column1, Column2, ...., ColumnN
FROM abc
ORDER BY InsertedAt DESC

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)

Jul 23 '05 #4

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

Similar topics

1
5244
by: JT | last post by:
I have an input form for which I've created a "matrix" for user input. Basically, the user chooses a radio button and then through javascript, a select box is displayed to define a value for that radio option, like so: Choice: (Radio1) type: (select box1) Choice: (Radio1) type: (select box2) Choice: (Radio1) type: (select box3) Choice: (Radio1) type: (select box4) Choice: (Radio1) type: (select box5)
1
2448
by: D. Shifflett | last post by:
Hi all, I am having trouble with a program that ran fine on Python 2.0 (#0, Mar 1 2001, 01:47:55) on linux2 but will not work on Python 2.3.2 (#1, Oct 8 2003, 17:33:47) on linux2
21
5245
by: John Fabiani | last post by:
Hi, I'm a newbie and I'm attempting to learn howto create a select statement. When I use >>> string1='18 Tadlock Place' >>> cursor.execute("SELECT * FROM mytest where address = %s",string1) All works as expected. But >>> numb=10 >>> cursor.execute("SELECT * FROM mytest where clientID = %d",numb) Traceback (innermost last): File "<stdin>", line 1, in ?
0
2528
by: dregier | last post by:
attached is my full query that I am currently working on: Declare @counterday1 int Declare @counterday2 int set @counterday1 = '20' set @counterday2 = '629' Declare @CounterMin07_01 float Declare @CounterMin08_01 float Declare @CounterMin09_01 float
3
8097
by: Memduh Durmaz | last post by:
Hi, I'm using DB2 UDB 7.2. Also I'm doing some tests on SQL Server 2000 for some statements to use efectively. I didn't find any solution on Sql Server about WITH ... SELECT structure of DB2. Is there any basic structure on Sql Server like WITH ... SELECT structure?
4
1503
by: celerystick | last post by:
This is an xsl question, comp.infosystems.www.authoring.stylesheets were not able to help , here goes .... With one xml file containing repeated element <subject>: ....<subject>Accounting</subject> <subject>Business</subject>.. I can apply one stylesheet "Accountingfilter.xsl" that does this :
1
5513
by: Carl Wu | last post by:
Hi all, I am newcomer in HTML, Javascript, I want to create two select controls S1, S2. There are 3 options: ALL, A, B in S1; When select A in S1, It let you select A1, A2 in S2,
2
7982
by: Wim Roffal | last post by:
I want to insert an option into a select. I know you can do something like: - Myselect.options = new Option('Hello 3'); but that doesn't do what I want because it overwrites an existing option. I have the existing options ordered on alphabet and the new option should fit in on the right place. Of course I can move all the options that come after the new one, but I wondered if there is a smarter way to do this.
1
2966
by: serena.delossantos | last post by:
Trying to insert into a history table. Some columns will come from parameters sent to the store procedure. Other columns will be filled with a separate select statement. I've tried storing the select return in a cursor, tried setting the values for each field with a separate select. Think I've just got the syntax wrong. Here's one of my attempts: use ESBAOffsets go
6
3392
by: Apaxe | last post by:
In the database i have a table with this information: key_id =1 key_desc =43+34+22+12 I want sum the values in key_desc. Something like: SELECT key_desc FROM table But the result of the select was "111" and not "43+34+22+12".
0
8889
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8572
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8652
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7391
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2782
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1779
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.