Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL QUERY

Author  Topic 

harshilshah
Starting Member

8 Posts

Posted - 2012-12-26 : 04:18:23
I have some 70 database entries in table weight

weight
------
45.6
45.34
23.56
.
.
.
...
.....
....
66.78
34.67
78.99

I want to display the last 10 entries. Can any1 help me with the query.IM woeking with sql 5.5

asif372
Posting Yak Master

100 Posts

Posted - 2012-12-26 : 04:43:24
Select
TOP (10)
Weight
From
TableName
ORDER BY
Weight DESC
Go to Top of Page

harshilshah
Starting Member

8 Posts

Posted - 2012-12-26 : 04:56:35
SQL error: function tablename.top does not exist... what to do now
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-12-26 : 05:03:59
quote:
Originally posted by harshilshah

SQL error: function tablename.top does not exist... what to do now


Are you using sql server? I dont think so seeing the error message

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2012-12-26 : 05:10:09
quote:
Originally posted by harshilshah

I have some 70 database entries in table weight
weight
------
45.6
45.34
23.56
.
.
.
...
.....
....
66.78
34.67
78.99

I want to display the last 10 entries. Can any1 help me with the query.IM woeking with sql 5.5



If you are using MySQL 5.5, then use
SELECT * FROM TableName ORDER BY weight DESC LIMIT 10;

--
Chandu
Go to Top of Page

harshilshah
Starting Member

8 Posts

Posted - 2012-12-26 : 05:24:26
@bandi : the query give me the values in descending order. I dont want it to be displayed in descending/ascending order,I want it to be displayed the same as it is in stored in table.

@visakh16 : im using sql 5.5 not sql server:)
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2012-12-26 : 05:31:25
quote:
Originally posted by harshilshah

@bandi : the query give me the values in descending order. I dont want it to be displayed in descending/ascending order,I want it to be displayed the same as it is in stored in table.

@visakh16 : im using sql 5.5 not sql server:)


Then try this:

SELECT * FROM TableName LIMIT 10;

This is SQL Server related forum.. You can get quick responses by posting your queries in MySql related forums




--
Chandu
Go to Top of Page

harshilshah
Starting Member

8 Posts

Posted - 2012-12-26 : 05:36:32
@chandu : ur query above gives me top 10 values, I want the bottom 10. because when a new value is inserted it is stored at the bottom of the table.
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2012-12-26 : 06:22:55
quote:
Originally posted by harshilshah

@chandu : ur query above gives me top 10 values, I want the bottom 10. because when a new value is inserted it is stored at the bottom of the table.


Try this...
Method1:
SELECT fieldlist FROM table LIMIT (SELECT COUNT(*) FROM table)-10 ,10
If the above gives error, find the total number of rows and replace this part ((SELECT COUNT(*) FROM table)) with that count........

Method2:
If there is auto incremented column( or created time column) in that table you can write as follows:
SELECT columnName FROM TableName ORDER BY AuoIncColName DESC LIMIT 10




--
Chandu
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-12-26 : 12:35:20
More to the point, tables are unordered sets. There is no concept of first or last without applying an ORDER BY clause. If you can't do that, then you are out of luck. Granted, different version of SQL might have some "hidden" column that you might be able to use, but that is not a good design.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-12-26 : 23:06:25
quote:
Originally posted by harshilshah

@bandi : the query give me the values in descending order. I dont want it to be displayed in descending/ascending order,I want it to be displayed the same as it is in stored in table.

@visakh16 : im using sql 5.5 not sql server:)



then you're in wrong forum.

This is MS SQL server forum and we deal with t-sql mostly

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -