Showing posts with label item. Show all posts
Showing posts with label item. Show all posts

Monday, March 19, 2012

get value of last item in column?

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 advanceYou 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|||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...@.SQLStudio.comwrote:

Quote:

Originally Posted by

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

|||<andreas.hei@.googlemail.comwrote in message
news:1173689649.827708.218050@.n33g2000cwc.googlegr oups.com...

Quote:

Originally Posted by

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:

Originally Posted by

>
is there a command/query for it?
>
>
>
>
On 7 mar, 18:54, "Plamen Ratchev" <Pla...@.SQLStudio.comwrote:

Quote:

Originally Posted by

>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|||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

Get Value of IDENTITY

Hi,
I need get value of IDENTITY column after a insert (of the inserted item),
hava way to do this automatic, or same function that do this?
ThanksCheck out SCOPE_IDENTITY() in the BOL.
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
"ReTF" <re.tf@.newsgroup.nospam> wrote in message
news:%23vrhmlOxFHA.3864@.TK2MSFTNGP12.phx.gbl...
> Hi,
> I need get value of IDENTITY column after a insert (of the inserted item),
> hava way to do this automatic, or same function that do this?
> Thanks
>|||3 ways
@.@.IDENTITY
IDENT_CURRENT
SCOPE_IDENTITY()
Read BOL
Rakesh
"ReTF" wrote:

> Hi,
> I need get value of IDENTITY column after a insert (of the inserted item),
> hava way to do this automatic, or same function that do this?
> Thanks
>
>|||@.@.Identity global variable should hold the value of the last generate
during an insert.
Martin
ReTF wrote:
> Hi,
> I need get value of IDENTITY column after a insert (of the inserted item),
> hava way to do this automatic, or same function that do this?
> Thanks
>|||You should use SCOPE_IDENTITY() because it is possible for a trigger to also
insert a row and generate an identity value. @.@.IDENTITY returns the last
IDENTITY value generated. IDENT_CURRENT returns the last generated IDENTITY
value for a table, but it's possible in a concurrent environment for
IDENT_CURRENT to change between the time that a row is inserted and the time
that IDENT_CURRENT is called. The best solution, therefore, is to use
SCOPE_IDENTITY() because it returns the last generated IDENTITY value within
the current scope, thus ignoring any IDENTITY values generated within
triggers.
"ReTF" <re.tf@.newsgroup.nospam> wrote in message
news:%23vrhmlOxFHA.3864@.TK2MSFTNGP12.phx.gbl...
> Hi,
> I need get value of IDENTITY column after a insert (of the inserted item),
> hava way to do this automatic, or same function that do this?
> Thanks
>

Monday, March 12, 2012

Get the Top 3 record in same table.


Hi all,

I have some questions about sql statement. Is there anyway to get top 3 record for each item in the same table? I use before 'top' but it will give me the top value base on the price. Can someone give me an advice on this?

Thanks in advance.

Example:

Table 1

Item Name Price Date
A $10.00 15 Jan 2007
A $8.50 17 May 2006
A $8.00 1 Jan 2006
A $7.80 24 Sep 2005
B $12.20 2 Jan 2007
B $12.00 10 Oct 2006


I want get the result as below base on the example table 1. (Top3 Record for each item)

A $10.00 15 Jan 2007
A $8.50 17 May 2006
A $8.00 1 Jan 2006
B $12.20 2 Jan 2007
B $12.00 10 Oct 2006

select *
from tbl t
where price in (select top 3 price from tbl x where x.item_name = t.item_name)|||SELECT [Item Name], Price, Date
FROM (SELECT ROW_Number()OVER(PARTITION BY [Item Name] ORDER BY PRICE) as RowNum,[Item Name], Price, Date
FROM top3$)AS t1
WHERE RowNum<=3|||

Hi,

It really helps me solve my problem.

Thanks alot.