Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 1061|回复: 0

求一条查询语句,筛选符合条件数据,三个字段

[复制链接]

该用户从未签到

发表于 2011-10-29 11:01:10 | 显示全部楼层 |阅读模式
一个表中数据内容如下:

ID SN RecordTime
---------------------------------------------------
100000018 31 2011-10-28 16:52:00.307
100000009 31 2011-10-28 16:51:51.193
100000000 31 2011-10-28 16:51:42.267
100000019 32 2011-10-28 16:52:01.327
100000010 32 2011-10-28 16:51:52.207
100000001 32 2011-10-28 16:51:43.070
100000020 33 2011-10-28 16:52:02.337
100000011 33 2011-10-28 16:51:53.220
100000002 33 2011-10-28 16:51:44.090
100000021 34 2011-10-28 16:52:03.360
100000012 34 2011-10-28 16:51:54.233
100000003 34 2011-10-28 16:51:45.107
100000022 35 2011-10-28 16:52:04.370
100000013 35 2011-10-28 16:51:55.240
100000004 35 2011-10-28 16:51:46.123
100000014 36 2011-10-28 16:51:56.250
100000005 36 2011-10-28 16:51:47.137

其中ID为主键

只保留每组SN相同的项RecordTime列最大的一条
查询结果为:

100000018 31 2011-10-28 16:52:00.307
100000019 32 2011-10-28 16:52:01.327
100000020 33 2011-10-28 16:52:02.337
100000021 34 2011-10-28 16:52:03.360
100000022 35 2011-10-28 16:52:04.370
100000014 36 2011-10-28 16:51:56.250

要求只使用一条SQL语句

想了好长时间没搞定,请大家帮下忙
回复

使用道具 举报

该用户从未签到

发表于 2011-10-29 11:01:14 | 显示全部楼层
SQL code select *
from (select *,row_number()over(partition by SN order by RecordTime desc) as a from table1) t
where row=1
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2011-10-29 11:01:15 | 显示全部楼层
SQL code --> --> (Roy)生成測試數據

declare @T table([ID] int,[SN] int,[RecordTime] Datetime)
Insert @T
select 100000018,31,'2011-10-28 16:52:00.307' union all
select 100000009,31,'2011-10-28 16:51:51.193' union all
select 100000000,31,'2011-10-28 16:51:42.267' union all
select 100000019,32,'2011-10-28 16:52:01.327' union all
select 100000010,32,'2011-10-28 16:51:52.207' union all
select 100000001,32,'2011-10-28 16:51:43.070' union all
select 100000020,33,'2011-10-28 16:52:02.337' union all
select 100000011,33,'2011-10-28 16:51:53.220' union all
select 100000002,33,'2011-10-28 16:51:44.090' union all
select 100000021,34,'2011-10-28 16:52:03.360' union all
select 100000012,34,'2011-10-28 16:51:54.233' union all
select 100000003,34,'2011-10-28 16:51:45.107' union all
select 100000022,35,'2011-10-28 16:52:04.370' union all
select 100000013,35,'2011-10-28 16:51:55.240' union all
select 100000004,35,'2011-10-28 16:51:46.123' union all
select 100000014,36,'2011-10-28 16:51:56.250' union all
select 100000005,36,'2011-10-28 16:51:47.137'

Select * from @T as  a where not exists(select 1 from @T where SN=a.SN and [RecordTime]>a.[RecordTime])
/*
ID    SN    RecordTime
100000018    31    2011-10-28 16:52:00.307
100000019    32    2011-10-28 16:52:01.327
100000020    33    2011-10-28 16:52:02.337
100000021    34    2011-10-28 16:52:03.360
100000022    35    2011-10-28 16:52:04.370
100000014    36    2011-10-28 16:51:56.250
*/
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2011-10-29 11:01:16 | 显示全部楼层
SQL code --> --> (Roy)生成測試數據

declare @T table([ID] int,[SN] int,[RecordTime] Datetime)
Insert @T
select 100000018,31,'2011-10-28 16:52:00.307' union all
select 100000009,31,'2011-10-28 16:51:51.193' union all
select 100000000,31,'2011-10-28 16:51:42.267' union all
select 100000019,32,'2011-10-28 16:52:01.327' union all
select 100000010,32,'2011-10-28 16:51:52.207' union all
select 100000001,32,'2011-10-28 16:51:43.070' union all
select 100000020,33,'2011-10-28 16:52:02.337' union all
select 100000011,33,'2011-10-28 16:51:53.220' union all
select 100000002,33,'2011-10-28 16:51:44.090' union all
select 100000021,34,'2011-10-28 16:52:03.360' union all
select 100000012,34,'2011-10-28 16:51:54.233' union all
select 100000003,34,'2011-10-28 16:51:45.107' union all
select 100000022,35,'2011-10-28 16:52:04.370' union all
select 100000013,35,'2011-10-28 16:51:55.240' union all
select 100000004,35,'2011-10-28 16:51:46.123' union all
select 100000014,36,'2011-10-28 16:51:56.250' union all
select 100000005,36,'2011-10-28 16:51:47.137'

Select * from @T as  a where [RecordTime]=(select MAX([RecordTime]) from @T where SN=a.SN)
/*
ID    SN    RecordTime
100000018    31    2011-10-28 16:52:00.307
100000019    32    2011-10-28 16:52:01.327
100000020    33    2011-10-28 16:52:02.337
100000021    34    2011-10-28 16:52:03.360
100000022    35    2011-10-28 16:52:04.370
100000014    36    2011-10-28 16:51:56.250
*/

--SQL2005以上版本
select [ID],[SN],[RecordTime]
from (select *,row_number()over(partition by SN order by RecordTime desc) as row from @T) t
where row=1
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2011-10-29 11:01:21 | 显示全部楼层
Select * from temp as a where not exists(select 1 from temp where SN=a.SN and [RecordTime]>a.[RecordTime])

都掉了个括号
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2011-10-29 11:01:23 | 显示全部楼层
select *
from (select *,row_number()over(partition by SN order by RecordTime desc) as a from temp) t
where row=1

你这条SQL和大阪不一样..row_number() 函数的 列名row 你改成a了 肯定row无效了啊..
select *
from (select *,row_number()over(partition by SN order by RecordTime desc) as a from temp) t
where a=1
这样就对了
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2011-10-29 11:01:26 | 显示全部楼层
少复制括号,上面都是测试过的

1楼把脚本在3楼有更正
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2011-10-29 11:01:30 | 显示全部楼层
SQL code
select max(ID) ID,sn,max(RecordTime) RecordTime from t gorup by sn
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2025-1-22 20:52 , Processed in 0.353083 second(s), 33 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表