Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 根据一列选择不同的记录组,然后按日期时间排序_Sql_Sql Server_Tsql_Select - Fatal编程技术网

Sql 根据一列选择不同的记录组,然后按日期时间排序

Sql 根据一列选择不同的记录组,然后按日期时间排序,sql,sql-server,tsql,select,Sql,Sql Server,Tsql,Select,我想根据一列值选择不同的记录组,然后按日期时间排序 例如,我有这个数据表 +----+---------+-------+---------------------------+ | id | Name | Index | DateTime | +----+---------+-------+---------------------------+ | 3 | Index | 1 | 2016-03-03 12:03:15.353 | |

我想根据一列值选择不同的记录组,然后按日期时间排序

例如,我有这个数据表

+----+---------+-------+---------------------------+
| id | Name    | Index | DateTime                  |
+----+---------+-------+---------------------------+
|  3 | Index   | 1     | 2016-03-03 12:03:15.353   |
|  4 | Index   | 1     | 2016-03-01 12:03:15.353   |
|  5 | Middle  | 2     | 2016-03-03 12:03:15.353   |
|  6 | Little  | 3     | 2016-03-03 12:03:15.353   |
|  7 | Thumb   | 4     | 2016-03-03 12:03:15.353   |
|  8 | Thumb   | 4     | 2016-03-01 12:03:15.353   |
+----+---------+-----------------------------------+
我想得到这样的结果:

+----+---------+-------+---------------------------+
| id | Name    | Index | DateTime                  |
+----+---------+-------+---------------------------+
|  3 | Index   | 1     | 2016-03-03 12:03:15.353   |
|  5 | Middle  | 2     | 2016-03-03 12:03:15.353   |
|  6 | Little  | 3     | 2016-03-03 12:03:15.353   |
|  7 | Thumb   | 4     | 2016-03-03 12:03:15.353   |
+----+---------+-----------------------------------+

+----+---------+-------+---------------------------+
| id | Name    | Index | DateTime                  |
+----+---------+-------+---------------------------+    
|  4 | Index   | 1     | 2016-03-01 12:03:15.353   |    
|  8 | Thumb   | 4     | 2016-03-01 12:03:15.353   |
+----+---------+-----------------------------------+
考虑一个指纹系统,它保存所有指纹,如果同一个手指重新注册,它不会覆盖,我想检索最近注册的手指,如果一个手指多次重新注册,则检索它们的历史记录

我尝试了这个查询,它根据索引返回不同的记录:

SELECT a.*
FROM    Fingerprint a
INNER JOIN 
      (SELECT DISTINCT [Index], MIN(Id) as id
       FROM Fingerprint 
       GROUP BY [Index]) AS b
ON a.[Index] = b.[Index] 
    AND a.id = b.id

WHERE [RefNumber] = '00000'

ORDER BY [LastUpdatedOn] DESC
如何实现这个be sql查询


我在这里发现了一个类似的问题:


使用LINQ检索没有历史记录的不同记录。

最简单的方法可能是使用窗口功能:

SELECT [id], [Name], [Index], [DateTime]
FROM   (SELECT [id], [Name], [Index], [DateTime],
               ROW_NUMBER() OVER (PARTITION BY [Name] ORDER BY [DateTime] AS rn
        FROM   Fingerprint) f
WHERE   rn = 1

逻辑:

使用
Partition
函数根据
id
列计算秩,然后按秩排序,然后按id列排序

select [id], [Name], [Index], [DateTime] from
(
 Select 
     *, 
    ROW_NUMBER() OVER (PARTITION by Name order by id asc) as rr
 from Fingerprint
 ) t
order by rr asc, id asc
输出屏幕截图:


谢谢@DhruvJoshi,它可以工作,我可以过滤结果以选择我可以检索到的级别吗,例如,我只想检索最新的手指集或返回两个手指集,等等。是的,您可以。您可以在'ORDER BY'语句之前添加where子句,如
where rr=2
,以检索所需的行。