求sql (同一个字段相同情况下,取另一个字段最大的所有记录)

serezha 2008-11-07 03:08:11
表 reginfo (id(主键), regno, adddate)
1 001 20080101
2 001 20080107
3 002 20080102
4 002 20080101
=======
期待结果:(每个regno只取一条记录,addate最大的那条)
1 001 20080107
3 002 20080102
...全文
2311 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenchencan1 2011-08-18
  • 打赏
  • 举报
回复
这个有问题,假如adddate在表中有重复的值,则就会出现笛卡尔。。你懂得[Quote=引用 18 楼 qingmingcom 的回复:]

理解有点偏差,要的结果(同一个字段相同情况下,取另一个字段最大的所有记录)应该是:
SQL code

select r.id, r.regno, r.adddate
from reginfo r
inner join(select max(adddate) adddate from reginfo group by regno) t on t.regno=r.regno a……
[/Quote]
qingmingcom 2011-07-29
  • 打赏
  • 举报
回复
理解有点偏差,要的结果(同一个字段相同情况下,取另一个字段最大的所有记录)应该是:

select r.id, r.regno, r.adddate
from reginfo r
inner join(select max(adddate) adddate from reginfo group by regno) t on t.regno=r.regno and t.adddate = r.adddate



[Quote=引用 12 楼 qingmingcom 的回复:]

连group by分组和sum,count,min,max等聚集函数都不知道怎么用的同学伤不起呀。好像还不少。这个应该很基本基本了。加油。
[/Quote]
llshan 2011-07-27
  • 打赏
  • 举报
回复
-------oracle写法----

select * from reginfo where adddate in
(select adddate from
(select max(adddate) as adddate ,regon from reginfo
group by regno))
metalwmz 2011-07-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wufeng4552 的回复:]
SQL code
SELECT * FROM reginfo a WHERE NOT EXISTS(SELECT 1 FROM reginfo WHERE regno=a.regno AND adddate>A.adddate )
[/Quote]

这个问题真的挺经典的,我以前遇到过想了半天还是用了row_number(),wufeng4552 的解法很有意思,哈哈,证明了SQL真的挺有内涵的哈。
潇洒王子 2011-07-25
  • 打赏
  • 举报
回复
拿分走人
净丑 2011-07-24
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 qingmingcom 的回复:]

连group by分组和sum,count,min,max等聚集函数都不知道怎么用的同学伤不起呀。好像还不少。这个应该很基本基本了。加油。
[/Quote]

拔开云雾见青天!
前面的童鞋伤不起呀!!!!
qingmingcom 2011-07-23
  • 打赏
  • 举报
回复
连group by分组和sum,count,min,max等聚集函数都不知道怎么用的同学伤不起呀。好像还不少。这个应该很基本基本了。加油。
yonghua_IT 2011-07-23
  • 打赏
  • 举报
回复
顶12 楼
Horrison 2011-07-22
  • 打赏
  • 举报
回复
[Quote=引用楼主 serezha 的回复:]
表 reginfo (id(主键), regno, adddate)
1 001 20080101
2 001 20080107
3 002 20080102
4 002 20080101
=======
期待结果:(每个regno只取一条记录,addate最大的那条)
1 001 20080107
3 002 20080102
[/Quote]
这不就是分组吗?

select regno , MAX(addate) from reginfo group by regno
fancyivan 2011-07-22
  • 打赏
  • 举报
回复
挖坟的来了
[Quote=引用 9 楼 mingyicz 的回复:]

mark
[/Quote]
mingyicz 2011-07-21
  • 打赏
  • 举报
回复
mark
serezha 2008-12-02
  • 打赏
  • 举报
回复
谢谢~~
SinGooCMS 2008-11-07
  • 打赏
  • 举报
回复
select * from reginfo a
where (select count(*) from reginfo where regno=a.regno and adddate>a.adddate)=0
shenhj01 2008-11-07
  • 打赏
  • 举报
回复
select * from reginfo where regno+adddate in(select regno+max(adddate)from reginfo group by regno )
tianhuo_soft 2008-11-07
  • 打赏
  • 举报
回复

表 reginfo (id(主键), regno, adddate)
1 001 20080101
2 001 20080107
3 002 20080102
4 002 20080101
SELECT * FROM reginfo a WHERE NOT EXISTS(SELECT 1 FROM reginfo WHERE regno=a.regno AND adddate>A.adddate )
hyde100 2008-11-07
  • 打赏
  • 举报
回复
select *
from reginfo a
where not exists(select 1 from reginfo b where b.regno= a.regno and a.adddate < b.adddate)
水族杰纶 2008-11-07
  • 打赏
  • 举报
回复
SELECT * FROM reginfo a WHERE NOT EXISTS(SELECT 1 FROM reginfo WHERE regno=a.regno AND adddate>A.adddate )
Yang_ 2008-11-07
  • 打赏
  • 举报
回复
select * from reginfo a
where id=(select top 1 id from reginfo where regno=a.regno order by adddate desc,id desc)

liangCK 2008-11-07
  • 打赏
  • 举报
回复
--按某一字段分组取最大(小)值所在行的数据
(爱新觉罗.毓华 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
*/
--七,如果整行数据有重复,所有的列都相同。
/*
数据如下:
name val memo
a 2 a2(a的第二个值)
a 1 a1--a的第一个值
a 1 a1--a的第一个值
a 3 a3:a的第三个值
a 3 a3:a的第三个值
b 1 b1--b的第一个值
b 3 b3:b的第三个值
b 2 b2b2b2b2
b 4 b4b4
b 5 b5b5b5b5b5
*/
--在sql server 2000中只能用一个临时表来解决,生成一个自增列,先对val取最大或最小,然后再通过自增列来取数据。
--创建表并插入数据:
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', 1, 'a1--a的第一个值')
insert into tb values('a', 3, 'a3: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

select * , px = identity(int,1,1) into tmp from tb

select m.name,m.val,m.memo from
(
select t.* from tmp t where val = (select min(val) from tmp where name = t.name)
) m where px = (select min(px) from
(
select t.* from tmp t where val = (select min(val) from tmp where name = t.name)
) n where n.name = m.name)

drop table tb,tmp

/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 1 b1--b的第一个值

(2 行受影响)
*/
--在sql server 2005中可以使用row_number函数,不需要使用临时表。
--创建表并插入数据:
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', 1, 'a1--a的第一个值')
insert into tb values('a', 3, 'a3: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

select m.name,m.val,m.memo from
(
select * , px = row_number() over(order by name , val) from tb
) m where px = (select min(px) from
(
select * , px = row_number() over(order by name , val) from tb
) n where n.name = m.name)

drop table tb

/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 1 b1--b的第一个值

(2 行受影响)
*/

34,590

社区成员

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

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