SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL QUERY
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

harshilshah
Starting Member

India
8 Posts

Posted - 12/26/2012 :  04:18:23  Show Profile  Reply with Quote
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

Pakistan
100 Posts

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

harshilshah
Starting Member

India
8 Posts

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

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 12/26/2012 :  05:03:59  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
2202 Posts

Posted - 12/26/2012 :  05:10:09  Show Profile  Reply with Quote
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

India
8 Posts

Posted - 12/26/2012 :  05:24:26  Show Profile  Reply with Quote
@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
Flowing Fount of Yak Knowledge

India
2202 Posts

Posted - 12/26/2012 :  05:31:25  Show Profile  Reply with Quote
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

India
8 Posts

Posted - 12/26/2012 :  05:36:32  Show Profile  Reply with Quote
@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
Flowing Fount of Yak Knowledge

India
2202 Posts

Posted - 12/26/2012 :  06:22:55  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

4612 Posts

Posted - 12/26/2012 :  12:35:20  Show Profile  Reply with Quote
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

India
52309 Posts

Posted - 12/26/2012 :  23:06:25  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000