一个简单SQL问题

Aylazhang 2007-10-31 01:20:14
table数据如下
[code=BatchFile]A B
-------
a 2007-10-30
a 2007-10-31
b 2006-04-01
b 2008-01-01
c 1999-01-02
c 2003-04-05
c 2007-10-02[/code]

我想要得到
[code=BatchFile]a 2007-10-30
b 2006-04-01
c 1999-01-02[/code]

即:对A字段去重,选出的B字段为min(B) ,请问这样的sql怎么写。
...全文
142 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ojuju10 2007-10-31
  • 打赏
  • 举报
回复

---1
select * from tab a
where not exists(select 1 from tab where a.a=a and a.b>b )

---2
select a.* from tab a,(select a,min(b) as b from tab group by a) b
where a.a=b.a and a.b=b.b

--3
select a,(select min(b) from tab where a.a=a) as b from tab a
Andy-W 2007-10-31
  • 打赏
  • 举报
回复
错了,应该是把>改成<号。

SELECT * FROM TableName AS A
WHERE NOT EXISTS(SELECT 1 FROM TableName AS B WHERE A.A=B.A AND B.B<A.B)

Andy-W 2007-10-31
  • 打赏
  • 举报
回复
try

SELECT * FROM TableName AS A
WHERE NOT EXISTS(SELECT 1 FROM TableName AS B WHERE A.A=B.A AND B.B>A.B)


fa_ge 2007-10-31
  • 打赏
  • 举报
回复
select A,D,min(B) as B
from t
group by A
中国风 2007-10-31
  • 打赏
  • 举报
回复
select * from ta t where not exists(select 1 from ta where a=t.a and b<t.b)
internetroot 2007-10-31
  • 打赏
  • 举报
回复
select A,D,min(B) as B
from 表名
group by A
中国风 2007-10-31
  • 打赏
  • 举报
回复
select a,min(d),min(b) from t group by a
Aylazhang 2007-10-31
  • 打赏
  • 举报
回复
就是选出来的D列由 min(B)来决定
yxh198358 2007-10-31
  • 打赏
  • 举报
回复

select A,max(D) as D ,min(B) as B
from tablename
group by A
Aylazhang 2007-10-31
  • 打赏
  • 举报
回复
先谢谢。
不过,忘记了一列。
[code=BatchFile]
A D B
-------
a a1 2007-10-30
a a2 2007-10-31
b b1 2006-04-01
b b2 2008-01-01
c c1 1999-01-02
c c2 2003-04-05
c c3 2007-10-02
[/code]
选取结果
[code=BatchFile]a a1 2007-10-30
b b1 2006-04-01
c c1 1999-01-02[/code]


OracleRoob 2007-10-31
  • 打赏
  • 举报
回复

select A,min(B) as B
from 表名
group by A

34,590

社区成员

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

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