从SQL Server中的单个行获取最大ID行 [英] get max ID row from individual rows in sql server

查看:551
本文介绍了从SQL Server中的单个行获取最大ID行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下数据,其中多行具有相同的数据,可以通过ID进行识别.

I Have data like below with multiple rows with same data which can be identified by ID.

我需要如下数据.对于每个重复记录集,仅获取单个最大ID值就可以通过获取单个最大ID来完成

I Need the data like below. Get only individual max ID value for every set of duplicate records with can be done by taking individual max ID

您能帮我吗?

推荐答案

这应该对您有帮助


create table #sample (type char(1), date datetime, Id bigint)
insert into #sample values('A', '5/22/2019 4:33', 1065621)
insert into #sample values('A', '5/22/2019 4:33', 1065181)
insert into #sample values('A', '5/22/2019 4:33', 1064212)
insert into #sample values('B', '11/7/2017 1:07', 540180)
insert into #sample values('B', '11/7/2017 1:07', 540179)
insert into #sample values('B', '11/7/2017 1:07', 540177)

select * from #sample

select [type], [date], max(id)
from #sample
group by [type], [date]

select distinct [type], [date], max(id) over(partition by  [type], [date] )
from #sample

Drop table #sample

这篇关于从SQL Server中的单个行获取最大ID行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆