dbTalk Databases Forums  

get value of last item in column?

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss get value of last item in column? in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
andreas.hei@googlemail.com
 
Posts: n/a

Default get value of last item in column? - 03-07-2007 , 11:00 AM






How can I get the value of the last Item in a coums?
SELECT COLUMN FROM DATABASE ORDER BY COLUMN DESC?
something with Fields(0).value?

Thank you in advance


Reply With Quote
  #2  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: get value of last item in column? - 03-07-2007 , 11:54 AM






You can use TOP 1 to return only 1 row (based on the ORDER BY), something
like this:

SELECT TOP 1 COLUMN FROM TABLE ORDER BY COLUMN DESC

Also, MIN(COLUMN) and MAX(COLUMN) may work depending on how you define last
item, like:

SELECT MAX(COLUMN) FROM TABLE

Regards,

Plamen Ratchev
http://www.SQLStudio.com



Reply With Quote
  #3  
Old   
andreas.hei@googlemail.com
 
Posts: n/a

Default Re: get value of last item in column? - 03-12-2007 , 02:54 AM



Thank you very much, but my question is more like how to get the exact
VALUE of the field (not the minimum or maximum)?

is there a command/query for it?




On 7 mar, 18:54, "Plamen Ratchev" <Pla... (AT) SQLStudio (DOT) com> wrote:
Quote:
You can use TOP 1 to return only 1 row (based on the ORDER BY), something
like this:

SELECT TOP 1 COLUMN FROM TABLE ORDER BY COLUMN DESC

Also, MIN(COLUMN) and MAX(COLUMN) may work depending on how you define last
item, like:

SELECT MAX(COLUMN) FROM TABLE

Regards,

Plamen Ratchevhttp://www.SQLStudio.com



Reply With Quote
  #4  
Old   
Greg D. Moore \(Strider\)
 
Posts: n/a

Default Re: get value of last item in column? - 03-12-2007 , 04:44 AM



<andreas.hei (AT) googlemail (DOT) com> wrote

Quote:
Thank you very much, but my question is more like how to get the exact
VALUE of the field (not the minimum or maximum)?
Not sure what you mean by the exact value of the field. How do you define
last item in column.

Generally that means the min or max value.

If you can give us some example data perhaps we can give a better solution?


Quote:
is there a command/query for it?




On 7 mar, 18:54, "Plamen Ratchev" <Pla... (AT) SQLStudio (DOT) com> wrote:
You can use TOP 1 to return only 1 row (based on the ORDER BY), something
like this:

SELECT TOP 1 COLUMN FROM TABLE ORDER BY COLUMN DESC

Also, MIN(COLUMN) and MAX(COLUMN) may work depending on how you define
last
item, like:

SELECT MAX(COLUMN) FROM TABLE

Regards,

Plamen Ratchevhttp://www.SQLStudio.com




--
Greg Moore
SQL Server DBA Consulting
Email: sql (at) greenms.com http://www.greenms.com




Reply With Quote
  #5  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: get value of last item in column? - 03-12-2007 , 05:59 AM



In addition to Greg's comments, this is why you can use TOP 1 and then based
on the ORDER BY define what you need to be selected in the result set.

Or maybe you mean the last inserted row... But a table is unordered set. You
would have to use a column that can determine what the last inserted row is
(the latest ID, date/time stamp, etc.).

Plamen Ratchev
http://www.SQLStudio.com




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.