选择第一条记录问题

liuhengping 2008-03-25 02:49:09

0.1,a1,b1,c1,时间1
0.1,a2,b2,c3,时间2
0.1,a3,b3,c3,时间3
0.2,x1,y1,z1,时间4
0.2,x2,y2,z2,时间5
....
我想得到如下结果(其中时间1在0.1中最大,时间5在0.2中最大):
0.1,a1,b1,c1,时间1
0.2,x2,y2,z2,时间5
....
请问SQL数据代码怎么写啊
...全文
114 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangchao1982 2008-03-25
  • 打赏
  • 举报
回复
M是不同名字的最大值的个数总和.太饶嘴了.
wangchao1982 2008-03-25
  • 打赏
  • 举报
回复
6楼方法可行,从思路上来说也很巧妙
但是,这个方法的时间复杂度很大应该是N*N.数据量小,这个方法还行,如果数据量非大,那么就需要考虑分步查询结合临时表/表表变量了.也就是人们通常意义上大"笨方法":先查出每个名称的最大值集合,然后再跟最大值集合进行比较.这个的时间复杂度应该是N*M
N是所有记录的总数
M是不同的名字的个数
而且M<=N
dawugui 2008-03-25
  • 打赏
  • 举报
回复
create table tb(name varchar(10), col2 varchar(10), col3 varchar(10), col4 varchar(10), 时间 varchar(10))
insert into tb values('0.1','a1','b1','c1','时间1')
insert into tb values('0.1','a2','b2','c3','时间2')
insert into tb values('0.1','a3','b3','c3','时间3')
insert into tb values('0.2','x1','y1','z1','时间4')
insert into tb values('0.2','x2','y2','z2','时间5')
go

--取第一次出现的记录
select a.* from tb a where 时间 = (select top 1 时间 from tb where name = a.name) order by a.name
/*
name col2 col3 col4 时间
---------- ---------- ---------- ---------- ----------
0.1 a1 b1 c1 时间1
0.2 x1 y1 z1 时间4
*/

--取最大
select a.* from tb a where 时间 = (select max(时间) from tb where name = a.name) order by a.name
/*
name col2 col3 col4 时间
---------- ---------- ---------- ---------- ----------
0.1 a3 b3 c3 时间3
0.2 x2 y2 z2 时间5

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

--取最小
select a.* from tb a where 时间 = (select min(时间) from tb where name = a.name) order by a.name
/*
name col2 col3 col4 时间
---------- ---------- ---------- ---------- ----------
0.1 a1 b1 c1 时间1
0.2 x1 y1 z1 时间4

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

drop table tb
dawugui 2008-03-25
  • 打赏
  • 举报
回复
[Quote=引用楼主 liuhengping 的帖子:]

0.1,a1,b1,c1,时间1
0.1,a2,b2,c3,时间2
0.1,a3,b3,c3,时间3
0.2,x1,y1,z1,时间4
0.2,x2,y2,z2,时间5
....
我想得到如下结果(其中时间1在0.1中最大,时间5在0.2中最大):
0.1,a1,b1,c1,时间1
0.2,x2,y2,z2,时间5
....
请问SQL数据代码怎么写啊
[/Quote]

select a.* from tb a where 时间 = (select top 1 时间 from tb where col1 = a.col1) order by a.col1
-狙击手- 2008-03-25
  • 打赏
  • 举报
回复
select col,max(sj) as sj
from ta
group by col
昵称被占用了 2008-03-25
  • 打赏
  • 举报
回复
select * from tab a
where not exists (
select 1 from tab
where col1=a.col1
and col5>a.col5
)
wzy_love_sly 2008-03-25
  • 打赏
  • 举报
回复
--按某一字段分组取最大(小)值所在行的数据(2007-10-23于浙江杭州)
/*
数据如下:
name val memo
a 2 a2(a的第二个值)
a 1 a1--a的第一个值
a 3 a3:a的第三个值
b 1 b1--b的第一个值
b 3 b3:b的第三个值
b 2 b2b2b2b2
b 4 b4b4
b 5 b5b5b5b5b5
*/
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a', 2, 'a2(a的第二个值)')
insert into tb values('a', 1, 'a1--a的第一个值')
insert into tb values('a', 3, 'a3:a的第三个值')
insert into tb values('b', 1, 'b1--b的第一个值')
insert into tb values('b', 3, 'b3:b的第三个值')
insert into tb values('b', 2, 'b2b2b2b2')
insert into tb values('b', 4, 'b4b4')
insert into tb values('b', 5, 'b5b5b5b5b5')
go

--一、按name分组取val最大的值所在行的数据。
--方法1:
select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
--方法3:
select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
/*
name val memo
---------- ----------- --------------------
a 3 a3:a的第三个值
b 5 b5b5b5b5b5
*/

--二、按name分组取val最小的值所在行的数据。
--方法1:
select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
--方法3:
select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 1 b1--b的第一个值
*/

--三、按name分组取第一次出现的行所在的数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
/*
name val memo
---------- ----------- --------------------
a 2 a2(a的第二个值)
b 1 b1--b的第一个值
*/

--四、按name分组随机取一条数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 5 b5b5b5b5b5
*/

--五、按name分组取最小的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
a 2 a2(a的第二个值)
b 1 b1--b的第一个值
b 2 b2b2b2b2
*/

--六、按name分组取最大的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name
/*
name val memo
---------- ----------- --------------------
a 2 a2(a的第二个值)
a 3 a3:a的第三个值
b 4 b4b4
b 5 b5b5b5b5b5
*/
wzy_love_sly 2008-03-25
  • 打赏
  • 举报
回复
select * from 表 t where not exists(
select 1 from 表 where 时间1=t.时间1 and 时间5>t.时间5
)

22,294

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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