Michail
Michail

Reputation: 366

Looking for SQL request

I have datatable in Microsoft SQL 2012

Datatable rows

It is possible to select data values with newest key position with one request? Or maybe not with one?

Result i want should be 10,20,30,50,70

Upvotes: 0

Views: 48

Answers (3)

danish
danish

Reputation: 5610

Try this:

select data from tableName  where id in (select MAX(id) from tableName group by key)

Upvotes: 2

Chris Cameron-Mills
Chris Cameron-Mills

Reputation: 4657

You could do it using the following:

SELECT data
FROM datatable
WHERE id IN (
  SELECT MAX(ID) latest_id
  FROM datatable
  GROUP BY key
)

This picks out the latest row for each key (going by incrementing ID). Then you simply only pick these rows using the IN which excludes the non-latest rows.

Upvotes: 2

verhie
verhie

Reputation: 1318

yes, it is:

select Data from table
where Key = (select max(Key) from table)
order by ID

This will output all Data with highest Key, assuming that highest=newest

Upvotes: -2

Related Questions