读书人

这个SQL语句该如何写

发布时间: 2011-12-29 22:09:38 作者: rapoo

求助这个SQL语句该怎么写
表名:abc
字段:a(char),b(int),c(int)

表中的数据

ggg 1 1
ggg 2 2
ggg 2 3
hhh 1 1
hhh 2 2

查询结果只需要 a和c字段的值,a字段的值不重复(只取b值和c值都最大的那一条记录)

也就是说,查询的结果(只显示字段a和c)应该为:
ggg 3
hhh 2

select a,c from abc where a不重复 and b最大 and c最大

Where后面应该怎么写?请帮忙,谢谢!

[解决办法]
select a,max(c) from abc group by a

[解决办法]
select a,max(c) from abc where b(此处加条件) group by a
[解决办法]
--group by 分组语句可以设置数据分组前跟分组后的条件,分别是where 跟 having

select a,max(c) from abc where 此处为分组前的条件 group by a having 此处为分组后的条件
[解决办法]
--方法一
select * from abc t where not exists(select 1 from abc where a=t.a and c> t.c)

--方法二
select * from abc t where c in (select max(c) from abc group by a)


--方法三
select * from abc t where c =(select max(c) from abc where a=t.a)



[解决办法]
SELECT A.a,Max(A.c)
FROM abc A
INNER JOIN (SELECT a,MAX(b) AS bb FROM abc GROUP BY a ) AS B ON
A.a = B.a AND A.b = B.bb
GROUP BY A.a
[解决办法]
declare @abc table
(a char(10),b int,c int)
insert into @abc
select 'ggg ', 1 , 1
union all select 'ggg ', 2 , 2
union all select 'ggg ', 2 , 3
union all select 'hhh ', 1 , 1
union all select 'hhh ' , 2 , 2


select * from @abc t
where not exists
(select 1 from @abc where a=t.a and ((b=t.b and c> t.c) or b> t.b))

----------------
ggg 23
hhh 22

[解决办法]
SELECT c.a, c.c
FROM
(
SELECT a, b, MAX(c) c
FROM @abc a2
GROUP BY a, b
) c
INNER JOIN
(
SELECT a, MAX(b) b
FROM @abc a2
GROUP BY a
) b ON c.a = b.a AND c.b = b.b
[解决办法]
select
t.*
from
abc t
where
not exists(select 1 from abc where a=t.a and (b> t.b or c> t.c))
[解决办法]
其实我的意思是查询记录的时候先满足b最大的条件,假如有多行记录,则取c最大的那一条。
-------------------
select * from abc
ggg 122
ggg 131
ggg 26
hhh 109
hhh 111
--------------------
select a,max(c) from abc where b in (select max(b) from abc group by a) group by a
ggg 1
hhh 1
应该是楼主你要的结果了
[解决办法]
看看这个
with myTemp(a,b,c) as (select a,max(b),max(c) from dbo.abc where 1=1 group by a)

select a,c from myTemp
[解决办法]
--方法一
select * from abc t where not exists(select 1 from abc where a=t.a and c> t.c)



--方法二
select * from abc t where c in (select max(c) from abc group by a)


--方法三
select * from abc t where c =(select max(c) from abc where a=t.a)

[解决办法]
select a,max(c) from abc where b = (select max(b) from abc ) group by a
[解决办法]
km_yu(雪上吹风,光着身子淋暴雨) ( ) 信誉:100
这个是正确的

wyhao() ( ) 信誉:100 2007-09-06 18:18:06 得分: 0


select a,max(c) from abc where b = (select max(b) from abc ) group by a


这个应该不能用=,要用IN
[解决办法]
create table tb
(
[a] nvarchar(64) null,
[b] int null,
[c] int null
)

insert tb
select 'ggg ',1,1 union all
select 'ggg ',2,2 union all
select 'ggg ',2,3 union all
select 'hhh ',1,1 union all
select 'hhh ',2,2
go

--select * from tb

select t1.a,max(t1.c) as c from tb as t1 inner join (
select a,max(b) as b from tb group by a) as t2 on t1.a=t2.a
group by t1.a

drop table tb
[解决办法]
declare @abc table(a varchar(11),b int ,c int)
insert into @abc select 'ggg ',1,1
insert into @abc select 'ggg ',2,2
insert into @abc select 'ggg ',2,3
insert into @abc select 'hhh ',1,1
insert into @abc select 'hhh ',2,2

select
t.*
from
@abc t
where
not exists(select 1 from @abc where a=t.a and (b> t.b or c> t.c))

/*
a b c
----------- ----------- -----------
ggg 2 3
hhh 2 2
*/
[解决办法]
SELECT A.a, B.c
FROM
--按a分组取b最大的值
(SELECT a, MAX(b) b FROM abc GROUP BY a) A

LEFT OUTER JOIN
--按a、b分组取c最大的值
(SELECT a, b, MAX(c) c FROM abc GROUP BY a, b) B

ON A.a = B.a AND A.b = B.b
[解决办法]
u can not write it in a simple sql :

select x.a, max(x.c)
from
abc as x,
( select a,max(b) as b from abc group by a ) as y
where x.a = y.a and x.b = y.b
group by x.a

读书人网 >C#

热点推荐