求教个sql的写法

洳夢绚染 2013-02-05 12:05:08
一个表的表结构式这样子的
a b c
1 9 a
1 8 b
1 6 c
2 9 d
2 7 e
2 1 f

第一行的abc是表头
现在想执行查询得到这样的结果
a b c
1 9 a
2 9 d
...全文
366 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
octwind 2013-02-18
  • 打赏
  • 举报
回复
引用 2 楼 chuifengde 的回复:
SQL code?1select * from [Table] a where not exists(select 1 from [Table] where a=a.a and b>a.b)
同2楼
好帅的一条鱼 2013-02-18
  • 打赏
  • 举报
回复
create table tb(a char(2),b char(2),c char(2)) 
insert into tb
 select '1','9','a' union all
 select '1','8','b' union all
 select '1','6','c' union all
 select '2','9','d' union all
 select '2','7','e' union all
 select '2','1','f' 

select * from tb where b in (select max(b) from tb group by a)

a    b    c
---- ---- ----
1    9    a 
2    9    d 

(2 行受影响)
rfq 2013-02-18
  • 打赏
  • 举报
回复
select * from table a where not exists(select * from table b where a.a=b.a and a.b<b.b)
美到心痛 2013-02-05
  • 打赏
  • 举报
回复
你沒有說明規則. 若取b=9的行用1樓. 若按a分組取每組b值最大的行2樓. 若按a分組取每組c值最小的行: select * from [Table] a where not exists(select 1 from [Table] where a=a.a and c<a.c) 這三種規則都可以得到你現在列出的結果.
chuifengde 2013-02-05
  • 打赏
  • 举报
回复
select * from [Table] a where not exists(select 1 from [Table] where a=a.a and b>a.b)
-Tracy-McGrady- 2013-02-05
  • 打赏
  • 举报
回复
select * from 表 where b='9' order by a asc
  • 打赏
  • 举报
回复
select a,max(b)b,c=(select top 1 c from tb tb2 where tb2.a=tb1.a and tb2.b=max(tb1.b)) from tb tb1 group by a
洳夢绚染 2013-02-05
  • 打赏
  • 举报
回复
引用 3 楼 sjcss 的回复:
你沒有說明規則. 若取b=9的行用1樓. 若按a分組取每組b值最大的行2樓. 若按a分組取每組c值最小的行: select * from [Table] a where not exists(select 1 from [Table] where a=a.a and c<a.c) 這三種規則都可以得到你現在列出的結果.
恩 我补充下 表tb 有abc三列 需求 按照a列的值不同分类 然后求每一类B最大的那一行的所有值
-Tracy-McGrady- 2013-02-05
  • 打赏
  • 举报
回复
一句话,就是问题没有描述清楚!
  • 打赏
  • 举报
回复
select a,b,c from (select row=row_number()over(partition by a order by getdate()),* from tb)t where row=1
w87875251l 2013-02-05
  • 打赏
  • 举报
回复
select a,max(b),max(c) from table group by a

34,575

社区成员

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

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