select max(a),b,c,d
from 表1
group by b,c,d现在我想把表1中的f字段也显示出来,请问有在group by 里不加f的情况下可以实现吗?如下select max(a),b,c,d,f
from 表1
group by b,c,d或者有别的方法吗,100分,解决马上结帖
from 表1
group by b,c,d现在我想把表1中的f字段也显示出来,请问有在group by 里不加f的情况下可以实现吗?如下select max(a),b,c,d,f
from 表1
group by b,c,d或者有别的方法吗,100分,解决马上结帖
(爱新觉罗.毓华 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')
goselect * , px = identity(int,1,1) into tmp from tbselect 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')
goselect 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 行受影响)
*/
from ta1,(select max(a) as a from ta1) b
(
select max(a) a , b , c , d
from 表1
group by b , c , d
) m , 表 n
where m.a = n.a
~
from ta1,(select max(a) as a from ta1) b
or
select a=(select max(a) from ta1),
b,
c,
d
from ta1
select m.* , n.f from
(
select max(a) a , b , c , d
from 表1
group by b , c , d
) m , 表 n
where m.a = n.a and m.b = n.b and m.c = n.c and m.d = n.d
from 表1
group by b,c,d 或者有别的方法吗,100分,解决马上结帖---当F多行时怎么办?
from 表1
group by b,c,d
(
select max(a) a , b , c , d
from 表1
group by b , c , d
) m , 表 n
where m.a = n.a and m.b = n.b and m.c = n.c and m.d = n.d
from 表1
group by b,c,d
select max(a),b,c,d
from 表1
group by b,c,d ------------------------try:select a,f from 表1 t where not exists(select 1 from 表1 where b=t.b and c=t.c and d=t.d and a>t.a)
select t1.*,t2.f from (
select a AS max(a),b,c,d
from 表1
group by b,c,d ) t1, 表1 t2where t1.a=t2.a
and t1.b=t2.b
and t1.c=t2.c
and t1.d=t2.d
和老鬼差不多的
(
select max(a) a , b , c , d
from 表1
group by b , c , d
) m , 表 n
where checksum(m.a ,m.b,m.c,m.d)= checksum(n.a,n.b,n.c,n.d)
from 表1)
select a,b,c,d,f from t where rowid=1
select max(a),max(f),b,c,d
from 表1
group by b,c,d orselect max(a),min(f),b,c,d
from 表1
group by b,c,d
如果有重复的,情况稍微复杂一些
--取重复当中最大的f
select ww.a,ww.b,ww.c,ww.d,max(ww.f) as f from
(
select aa.*,bb.f from
(
select max(a) as a,b,c,d from tb
group by b,c,d
) aa
inner join tb bb
on aa.[a] = bb.a and aa.b=bb.b and aa.c=bb.c and aa.d=bb.d
) ww
group by ww.a,ww.b,ww.c,ww.d--取重复当中最小的f
select ww.a,ww.b,ww.c,ww.d,min(ww.f) as f from
(
select aa.*,bb.f from
(
select max(a) as a,b,c,d from tb
group by b,c,d
) aa
inner join tb bb
on aa.[a] = bb.a and aa.b=bb.b and aa.c=bb.c and aa.d=bb.d
) ww
group by ww.a,ww.b,ww.c,ww.d
select max(a),b,c,d
from 表1
group by b,c,d 现在我想把表1中的f字段也显示出来,请问有在group by 里不加f的情况下可以实现吗?如下 方法二
select max(a),b,c,d,f
from 表1
group by b,c,d
--------------------------------------------------------------------
对于方法二来说,和方法一产生的记录集区别就是,max(a),b,c,d相同,但是f会不同,这样的话就要看你的实现要求了。如果说需要保留不同的f,那样的话group by 中加f就可以了,对原程序没有影响,但记录数会比原来多。当然不想在group by中加的话,就用以max(a),b,c,d为KEY和表1再连接一次以取得f。如果不需要保留不同的f,那就要给出删减策略,比如说保留最大的f,那么只要稍微改下方法二,如下
select max(a),b,c,d,max(f)
from 表1
group by b,c,d
from 表1 t
where not exists (
select 1 from 表1
where b=t.b and c=t.c and d=t.d and a>t.a
)
--这个问题有争义就是因为f 是否有重复
--当b,c,d这一组中不管f有没有重复,a 无重复时.取a最大这一条select a,b,c,d,f from t t1
where not exists(select 1 from t where t1.b=b and t1.c=c and t1.d=d and a>t1.a )--当b,c,d这一组中f无重复(也就是f有大有小)恰好a又是相等的select a,b,c,d,f from t t1
where not exists(select 1 from t where t1.b=b and t1.c=c and t1.d=d and f>t1.f )--or
select a,b,c,d,f from t t1
where not exists(select 1 from t where t1.b=b and t1.c=c and t1.d=d and f<t1.f )
FROM (SELECT MAX(a) AS a
FROM test
GROUP BY b,c,d) AS temp INNER JOIN
test AS test ON temp.a = test.a