一句sql

jett 2002-04-01 11:54:54
table里有n个字段,a,b,c,c,e....
要求得到
select max(c) from table group by a,b
检索出的c所在行的数据

即select max(c)d,e,... from table group by a,b '但这句在语法上是错误的
...全文
37 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyxinfo 2002-04-01
  • 打赏
  • 举报
回复
a,b,c合起来应该可以确定一条记录吧:

select *
from table t1,(select a,b,max(c) c from table group by a,b) t2
where t1.a = t2.a and t1.b = t2.b and t1.c = t2.c
BrentIvan 2002-04-01
  • 打赏
  • 举报
回复
try:

Select * From tablename t1 Where Not Exists (Select 1 From tablename t2 Where t1.a = t2.a And t1.b = t2.b And t2.c > t1.c)
流星尔 2002-04-01
  • 打赏
  • 举报
回复
select * from table join (select max(c) as c from table group by a,b) c on table.c=c.c
jett 2002-04-01
  • 打赏
  • 举报
回复
a b c
1 1 2
1 1 3
2 1 3
2 1 4

会出来(1,1,3) (2,1,3) (2,1,4)
好像是错的
BrentIvan 2002-04-01
  • 打赏
  • 举报
回复
Select * From table Where c In (Select Max(c) From table Group By a, b)
jett 2002-04-01
  • 打赏
  • 举报
回复
sql server测下来有索引的情况inner join的那句快
没索引几乎差不多,但还是innerjoin 快
感谢各位参与
jett 2002-04-01
  • 打赏
  • 举报
回复
to liujianjun_(流星尔)你得是错的
不过让我想到,下面的应该可以
select * from table inner join (select max(c) as c ,a,b from table group by a,b) c
on table.c=c.c and
table.a=c.a and
table.b=c.b

to BrentIvan(Ivan)
你的是对的,但与上面那句相比那句更好呢?

34,587

社区成员

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

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