Groups | Blog | Home
all groups > sql server data mining > november 2003 >

sql server data mining : Is there a Last() function in SQL-Server


Deepak Gurung
11/26/2003 9:00:43 PM
Hi All,
There is a function in Access called "Last()" which
returns the last record value in a group.
For example.
A B
-------
1 10
1 7
2 21
2 2
2 4
3 6
Here, Last() function In Access returns as follows:
A B
------
1 7 (the last value for A=1)
2 4 (the last value for A=2)
3 6 (the last value for A=3)
I need same functionality in SQL-SERVER, is it there? If
Vishal Parkar
12/2/2003 2:41:30 AM
Deepak,

There is not function like that in SQL. But to achieve similar result you need to have one more
column which will direct sql which row needs to be retrieved on the basis of this column. In
following example i've added one more column C which will act as an identity of a latest value that
has been inserted for each A's value.

Ex:

A B C
------------
1 10 1
1 7 2
2 21 1
2 2 2
2 4 3
3 6 1

--required query would be.

select a.*
from table1 a join
(select a, max(c) c
from table1 group by a) b
on a.a = b.a and a.c=b.c



--
- Vishal


AddThis Social Bookmark Button