取相同username中id最大的行

ltpinpin783 2008-01-21 03:02:09

create table Test (id int,username varchar(255),A varchar(20),B varchar(20))
insert into Test
select 1,'wlh','aa','bb' union
select 2,'wlh','aaa','bbb' union
select 3,'wlh','aaa','bbbb' union
select 4,'lsl','aaaa','bb'

要的结果是:
ID USERNAME A B
3 wlh aaa bbbb
4 lsl aaaa bb
=====================================================
相同username的取max(id)
...全文
243 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovehilton 2008-07-15
  • 打赏
  • 举报
回复
接分
sp4 2008-01-23
  • 打赏
  • 举报
回复
上一个条件写反了,呵呵
Select * From Test a Where Not Exists(
Select 1 From Test Where UserName = a.UserName
And Id> a.Id)
sp4 2008-01-23
  • 打赏
  • 举报
回复
Select * From Test a Where Not Exists(
Select 1 From Test Where UserName = a.UserName
And a.Id>Id)
pt1314917 2008-01-22
  • 打赏
  • 举报
回复

select * from test a where id in(select max(id) from test where username=a.username)
--或者:
select * from test a where id in(select top 1 id from test where username=a.username order by id)
--或者
select * from test a where
not exists(select 1 from test where username=a.username and id>a.id)
---又或者
select * from test a
where (select count(1) from test where username=a.username and id>=a.id)=1

........方法太多,不一一列举了。。。
parss 2008-01-22
  • 打赏
  • 举报
回复

这个我用的多,拿出来分享一下吧




select * from test a where a.id in (select max(b.id) from test b where a.username=b.username)
penglewen 2008-01-22
  • 打赏
  • 举报
回复

declare @Test table (id int,username varchar(255),A varchar(20),B varchar(20))
insert into @Test
select 1,'wlh','aa','bb' union
select 2,'wlh','aaa','bbb' union
select 3,'wlh','aaa','bbbb' union
select 4,'lsl','aaaa','bb'
select * from @test where id in(
select max(id) from @Test group by username)
wyb0026 2008-01-22
  • 打赏
  • 举报
回复
帮顶
fcuandy 2008-01-22
  • 打赏
  • 举报
回复
散分贴
bqb 2008-01-22
  • 打赏
  • 举报
回复
楼主这贴花了你100分,亏大了!
威尔亨特 2008-01-22
  • 打赏
  • 举报
回复
select t.* from Test t where id in (select max(id) from Test where USERNAME =t.USERNAME )
ojuju10 2008-01-21
  • 打赏
  • 举报
回复


create table Test (id int,username varchar(255),A varchar(20),B varchar(20))
insert into Test
select 1,'wlh','aa','bb' union
select 2,'wlh','aaa','bbb' union
select 3,'wlh','aaa','bbbb' union
select 4,'lsl','aaaa','bb'

--1
select * from test a
where not exists(select 1 from test b where a.username=b.username and a.id<b.id)

--2
select * from test a
where id in (select max(id) from test b where a.username=b.username )

--3
select a.* from test a,(select username,max(id) as id from test group by username ) b
where a.username=b.username and a.id=b.id
dobear_0922 2008-01-21
  • 打赏
  • 举报
回复
create table Test (id int,username varchar(255),A varchar(20),B varchar(20))
insert into Test
select 1,'wlh','aa','bb' union
select 2,'wlh','aaa','bbb' union
select 3,'wlh','aaa','bbbb' union
select 4,'lsl','aaaa','bb'

select * from test a where not exists(select 1 from test b where a.username=b.username and a.id<b.id)
/*
id username
----------- ------------------
3 wlh
4 lsl

(2 row(s) affected)
*/

drop table test
-狙击手- 2008-01-21
  • 打赏
  • 举报
回复
create table Test (id int,username varchar(255),A varchar(20),B varchar(20))
insert into Test
select 1,'wlh','aa','bb' union
select 2,'wlh','aaa','bbb' union
select 3,'wlh','aaa','bbbb' union
select 4,'lsl','aaaa','bb'

select *
from test t
where not exists( select 1 from test where t.username = username and id > t.id)
/*

id username A B
----------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------- --------------------
3 wlh aaa bbbb
4 lsl aaaa bb

(所影响的行数为 2 行)
*/
drop table test
Andy-W 2008-01-21
  • 打赏
  • 举报
回复
up:
select a.* from test a where not exists(select 1 from test where username = a.username and id > a.id)

pengxuan 2008-01-21
  • 打赏
  • 举报
回复
select * from Test t where not exists(select 1 from test where username=t.username and id>t.id)
dawugui 2008-01-21
  • 打赏
  • 举报
回复
create table Test (id int,username varchar(255),A varchar(20),B varchar(20))
insert into Test
select 1,'wlh','aa','bb' union
select 2,'wlh','aaa','bbb' union
select 3,'wlh','aaa','bbbb' union
select 4,'lsl','aaaa','bb'
--方法1:
select a.* from test a where id = (select max(id) from test where username = a.username) order by a.username
/*
id userusername A B
----------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------- --------------------
3 wlh aaa bbbb
4 lsl aaaa bb

(所影响的行数为 2 行)
*/
--方法2:
select a.* from test a where not exists(select 1 from test where username = a.username and id > a.id)
/*
id userusername A B
----------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------- --------------------
3 wlh aaa bbbb
4 lsl aaaa bb

(所影响的行数为 2 行)
*/
--方法3:
select a.* from test a,(select username,max(id) id from test group by username) b where a.username = b.username and a.id = b.id order by a.username
/*
id userusername A B
----------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------- --------------------
3 wlh aaa bbbb
4 lsl aaaa bb

(所影响的行数为 2 行)
*/
--方法4:
select a.* from test a inner join (select username , max(id) id from test group by username) b on a.username = b.username and a.id = b.id order by a.username
/*
id userusername A B
----------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------- --------------------
3 wlh aaa bbbb
4 lsl aaaa bb

(所影响的行数为 2 行)
*/
--方法5
select a.* from test a where 1 > (select count(*) from test where username = a.username and id > a.id ) order by a.username
/*
id userusername A B
----------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------- --------------------
3 wlh aaa bbbb
4 lsl aaaa bb

(所影响的行数为 2 行)
*/
drop table test

dawugui 2008-01-21
  • 打赏
  • 举报
回复
create table Test (id int,username varchar(255),A varchar(20),B varchar(20))
insert into Test
select 1,'wlh','aa','bb' union
select 2,'wlh','aaa','bbb' union
select 3,'wlh','aaa','bbbb' union
select 4,'lsl','aaaa','bb'
--方法1:
select a.* from test a where id = (select max(id) from test where username = a.username) order by a.username
--方法2:
select a.* from test a where not exists(select 1 from test where username = a.username and id > a.id)
--方法3:
select a.* from test a,(select username,max(id) id from test group by username) b where a.username = b.username and a.id = b.id order by a.username
--方法4:
select a.* from test a inner join (select username , max(id) id from test group by username) b on a.username = b.username and a.id = b.id order by a.username
--方法5
select a.* from test a where 1 > (select count(*) from test where username = a.username and id > a.id ) order by a.username

drop table test

/*
id userusername A B
----------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------- --------------------
3 wlh aaa bbbb
4 lsl aaaa bb

(所影响的行数为 2 行)
*/
zefuzhang2008 2008-01-21
  • 打赏
  • 举报
回复

;with t as(
select id,username,a,b,row_number() over(PARTITION BY username order by id desc) as rowid
from test)
select id,username,a,b
from t
where rowid=1
昵称被占用了 2008-01-21
  • 打赏
  • 举报
回复
create table Test (id int,username varchar(255),A varchar(20),B varchar(20))
insert into Test
select 1,'wlh','aa','bb' union
select 2,'wlh','aaa','bbb' union
select 3,'wlh','aaa','bbbb' union
select 4,'lsl','aaaa','bb'

select * from test a
where not exists (
select 1 from test
where USERNAME=a.USERNAME
and id>a.id
)

--结果
id username A B
----------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------- --------------------
3 wlh aaa bbbb
4 lsl aaaa bb

(所影响的行数为 2 行)

拓狼 2008-01-21
  • 打赏
  • 举报
回复
select * from test a where not exists(select 1 from test b where a.username=b.username and a.id<b.id)


---------------------------------------------------------------
更多内容,详见微软BI开拓者www.windbi.com
加载更多回复(4)
内容概要:本文围绕基于三重移相控制(TPS)的双有源桥(DAB)高频隔离DC-DC变换器开展系统性研究,重点构建了其在Simulink环境下的高精度仿真模型。研究全面涵盖SPS单相移相、DPS双重重移相与TPS三重移相等多种控制策略的建模、实现与性能对比,深入分析不同模式下变换器的功率传输特性、软开关实现条件及功率回流问题,旨在提升DAB在交直流混合微电网、能量路由器、多端口柔性互联装置等场景的转换效率与动态响应能力。通过对ZVS(零电压切换)条件的精确控制与移相角参数的优化,有效降低了开关损耗,增强了系统整体能效与运稳定性。该仿真模型具有良好的可扩展性,适用于复杂电能转换系统的科研验证与工程开发。; 适合人群:电力电子、电气工程及其自动化等相关专业的硕士研究生、博士生、科研人员以及从事新能源变换器、柔性输配电系统设计的工程技术人员。; 使用场景及目标:①掌握双有源桥DAB变换器的基本工作原理及其在高频隔离场合的核心优势;②深入理解三重移相控制策略的设计机理、控制自由度分配及其在效率优化的关键作用;③构建并调试可用于科研论文撰写、项目申报或实际系统验证的高保真Simulink仿真模型,支撑理论分析与实验对比。; 阅读建议:建议结合MATLAB/Simulink平台进动手实践,重点关注主电路拓扑搭建、移相控制模块设计、驱动信号时序配置及ZVS实现条件的仿真观测,推荐通过对比SPS、DPS与TPS三种模式的稳态与动态响应曲线,深入掌握各控制策略的适用边界与优化方向。
【重要提示】本资源设置为0积分下载,若非0积分请勿轻易下载 亲爱的CSDN用户: 首先感谢你点进这个资源页面。我需要提前说明一个重要情况: 本资源原本已设置为“0积分下载”,即作者希望完全免费共享。但CSDN平台有时会根据文件的下载热度、文件大小、用户权限等因素,自动将部分资源的积分调整为非0数值(如1积分、2积分、5积分等)。这是平台系统的自动为,而非作者本人的设定。 因此,如果你当前看到该资源的下载所需积分不是0(例如显示为1、2、3……),请谨慎决定是否下载。 如果你按照非0积分支付并下载后发现资源内容不符合预期、链接失效,或者实际上该资源本应是免费的,作者无法为此承担积分损失或退还操作。强烈建议:仅在页面显示为0积分时进下载。 另外,本资源描述并未直接提供具体的下载地址或外部链接,因为它本身是一个通过CSDN官方上传通道提交的文件/内容包。如果你看到描述没有外部网盘地址,这是正常的——资源文件应通过CSDN内置的“下载”按钮获。若因平台积分显示异常导致你支付了积分,请优先联系CSDN客服咨询积分退还政策,作者没有权限修改平台自动设定的积分值。 感谢你的理解与支持。技术分享本应开放,但受限于平台规则,特此提醒如上。祝学习进步!

34,876

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧