简体   繁体   中英

Need to rank the data in SQL Server

在此处输入图像描述

From the input data, I need to rank the id . The data is order by time ascending.

Example: id number 421492036 has two record with code '05' and is ordered by time ascending.

The ID 421492036 has same code 05 and is the first two records in the table.

So its rank should be 1 in Output 1 table.

In Output 2 table we need to take the id 421492036 first record based on time.

Is it possible to get like this?

According to the SQL Server Documentation , DENSE_RANK() is available on all supported versions of SQL Server. If you upgrade to one of the current versions you can run the query:

select max(id), max(code), max(time), rk
from (
  select t.*, dense_rank() over(order by time) as rk from t
) x
group by rk

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM