假设记录
404121 8200400049/030 3 1000034706 112701160 4 10050 4 1160
404121 8200400049/030 1 1000034706 112701160 4 10050 4 1330 如何将第三列中最大的取出来?
404121 8200400049/030 3 1000034706 112701160 4 10050 4 1160
我怀疑是SQL的逻辑有错误了
404121 8200400049/030 3 1000034706 112701160 4 10050 4 1160
404121 8200400049/030 1 1000034706 112701160 4 10050 4 1330 如何将第三列中最大的取出来?
404121 8200400049/030 3 1000034706 112701160 4 10050 4 1160
我怀疑是SQL的逻辑有错误了
后面又来一句: 如何将第三列中最大的取出来?
是什么意思?你不能举原表的数据来说明问题吗?
假设我表内容
404121 8200400049/030 3 1000034706 112701160 4 10050 4 1160
404121 8200400049/030 1 1000034706 112701160 4 10050 4 1330 得到下面的结果
404121 8200400049/030 3 1000034706 112701160 4 10050 4 1160 用max函数吗?
内容如下
a b c d
1 3 4 5
1 1 4 3
1 2 5 5
1 8 3 6当c列最大时,找b列为最大的记录也就是得到a b c d
1 2 5 5
若是则如下:
select * from ta,(select a,max(c) c,max(b) b from ta group by a) tem
where ta.a=tem.a and ta.c=tem.c and ta.b=tem.b
create table 表a(a int,b int,c int,d int)
insert 表a select 1,3,4,5
union all select 1,1,4,3
union all select 1,2,5,5
union all select 1,3,5,5
union all select 1,8,3,6
go--查询
select a.*
from 表a a join(
select b=max(b),c from 表a
where c=(select max(c) from 表a)
group by c
)b on a.b=b.b and a.c=b.c
go--删除测试
drop table 表a/*--测试结果
a b c d
----------- ----------- ----------- -----------
1 3 5 5(所影响的行数为 1 行)--*/
表a
内容如下
a b c d
1 3 4 5
1 1 4 3
1 2 5 5
1 8 3 6
2 1 5 7
2 3 5 6
2 4 4 8得到记录
a b c d
1 2 5 5
2 3 5 6下在好象得到的记录又太少了,在有的 a 列的没找见
on a.b=b.b ?
create table 表a(a int,b int,c int,d int)
insert 表a select 1,3,4,5
union all select 1,1,4,3
union all select 1,2,5,5
union all select 1,3,5,5
union all select 1,8,3,6
union all select 2,1,5,7
union all select 2,3,5,6
union all select 2,4,4,8
go--查询
select a.*
from 表a a join(
select b=max(b),b.a,b.c from 表a a join(
select a,c=max(c) from 表a group by a
)b on a.a=b.a and a.c=b.c
group by b.a,b.c
)b on a.a=b.a and a.b=b.b and a.c=b.c
go--删除测试
drop table 表a/*--测试结果
a b c d
----------- ----------- ----------- -----------
1 3 5 5
2 3 5 6(所影响的行数为 2 行)
--*/