How to get the last element by date of each "type" in LINQ or TSQL

Posted by Mauro on Stack Overflow See other posts from Stack Overflow or by Mauro
Published on 2010-05-13T10:38:59Z Indexed on 2010/05/13 10:44 UTC
Read the original article Hit count: 315

Filed under:
|
|
|

Imagine to have a table defined as

CREATE TABLE [dbo].[Price](
    [ID] [int] NOT NULL,
    [StartDate] [datetime] NOT NULL,
    [Price] [int] NOT NULL
)

where ID is the identifier of an action having a certain Price. This price can be updated if necessary by adding a new line with the same ID, different Price, and a more recent date.

So with a set of a data like

ID StartDate  Price

1  01/01/2009 10
1  01/01/2010 20
2  01/01/2009 10
2  01/01/2010 20

How to obtain a set like the following?

1  01/01/2010 20
2  01/01/2010 20

© Stack Overflow or respective owner

Related posts about tsql

Related posts about query