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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 plz help retrieving last insert record of groups

Author  Topic 

josh777
Starting Member

12 Posts

Posted - 2007-03-01 : 20:42:31
Hi people,

I have a situation in which i need to retrieve the last row inserted of a particular group record.
Ex:
Item Group Prices
A 27
A 15
A 19
B 22
B 7
C 11
D 44

I want a query to retrieve the last inserted data of each group
ie A 19
B 7
C 11
D 44
Thanks a lot for ur help

Josh


jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-03-01 : 23:29:28
"last row" implies an order. there is no order of rows in a table unless you specify one with an order by clause.

do you mean that last one in time? if so do you have a datetime column that specifies when it the row was created? or if you have an identity column, you could use that to tell which row of a particular group was entered last.


www.elsasoft.org
Go to Top of Page

mahesh_bote
Constraint Violating Yak Guru

298 Posts

Posted - 2007-03-02 : 06:36:47
quote:
Originally posted by josh777

Hi people,

I have a situation in which i need to retrieve the last row inserted of a particular group record.
Ex:
Item Group Prices
A 27
A 15
A 19
B 22
B 7
C 11
D 44

I want a query to retrieve the last inserted data of each group
ie A 19
B 7
C 11
D 44
Thanks a lot for ur help

Josh






If u have column for datetime in ur table, u can write it like
Select Col1, Col2, Max(DateCol) From Table Group By Col1, Col2
It is reqd to be Datetime col should be in table, unless u can't get the desired recordset. Or do u have pk in ur table. If Yes, it will be also helpfull. u can group ur query on that PK.

Mahesh
Go to Top of Page

josh777
Starting Member

12 Posts

Posted - 2007-03-02 : 14:07:23
Thanx but the problem is even when i have an identity is there a way i can get all the item groups with the last inserted record
Item Group Prices Serial
A 27 1
A 15 2
A 19 3
B 22 4
B 7 5
C 11 6
D 44 7

I want a query to retrieve the last inserted data of each group
ie A 19
B 7
C 11
D 44
Thanks again

josh
Go to Top of Page
   

- Advertisement -