【求助】查询的一个小问题。

flashkit 2006-12-06 11:25:32
表如下:
A B C
1 1 2006-1-1
2 2 2006-1-1
3 3 2006-1-2
4 4 2006-1-2

按C分组后,每组取出第一条记录出来,变为下面的结果:
A B C
1 1 2006-1-1
3 3 2006-1-2
...全文
84 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
flashkit 2006-12-06
  • 打赏
  • 举报
回复
To : xluzhong(Ralph)
如果min(a), min(b)的话,有可能结果不属于同一条记录。
例如
a b c
1 5 2006-1-1
5 1 2006-1-1

你的结果:
a b c
1 1 2006-1-1

期望结果:
a b c
1 5 2006-1-1

嗯,好了,暂时到这里。谢谢大家的讨论。结了。
xluzhong 2006-12-06
  • 打赏
  • 举报
回复
declare @tb table
(
a int,
b int,
c varchar(10)
)

insert into @tb(a,b,c) values(1, 1, '2006-1-1')
insert into @tb(a,b,c) values(1, 1, '2006-1-1')
insert into @tb(a,b,c) values(1, 2, '2006-1-1')
insert into @tb(a,b,c) values(2, 1, '2006-1-1')
insert into @tb(a,b,c) values(3 , 3, '2006-1-2')
insert into @tb(a,b,c) values(3 , 3, '2006-1-2')
insert into @tb(a,b,c) values(3 , 4, '2006-1-2')
insert into @tb(a,b,c) values(4 , 3, '2006-1-2')


select min(a),min(b),c from @tb group by c
flashkit 2006-12-06
  • 打赏
  • 举报
回复
看来避免不了新建identity临时表啊。唉~~
dawugui 2006-12-06
  • 打赏
  • 举报
回复
To : dawugui(潇洒老乌龟)
那如果表中是下面这个情况呢??
A B C
1 1 2006-1-1
1 1 2006-1-1
1 2 2006-1-1
2 1 2006-1-1
3 3 2006-1-2
3 3 2006-1-2
3 4 2006-1-2
4 3 2006-1-2

select id1=identity(int,1,1),* into t1 from tb
select m.a,m.b,m.c from t1 m,
(select c,min(id1) as id1 from t1 group by c) n
where m.id1 = n.id1 and m.c = n.c
flashkit 2006-12-06
  • 打赏
  • 举报
回复
declare @tb table
(
a int,
b int,
c varchar(10)
)

insert into @tb(a,b,c) values(1, 1, '2006-1-1')
insert into @tb(a,b,c) values(1, 1, '2006-1-1')
insert into @tb(a,b,c) values(1, 2, '2006-1-1')
insert into @tb(a,b,c) values(2, 1, '2006-1-1')
insert into @tb(a,b,c) values(3 , 3, '2006-1-2')
insert into @tb(a,b,c) values(3 , 3, '2006-1-2')
insert into @tb(a,b,c) values(3 , 4, '2006-1-2')
insert into @tb(a,b,c) values(4 , 3, '2006-1-2')

select * from @tb b
where a=(select top 1 a from @tb where c=b.c)

这个是结果

a b c
1 1 2006-1-1
1 1 2006-1-1
1 2 2006-1-1
3 3 2006-1-2
3 3 2006-1-2
3 4 2006-1-2

已经违背了要求。
希望的结果是这样的:
a b c
1 1 2006-1-1
3 3 2006-1-2
flashkit 2006-12-06
  • 打赏
  • 举报
回复
To : dawugui(潇洒老乌龟)
那如果表中是下面这个情况呢??
A B C
1 1 2006-1-1
1 1 2006-1-1
1 2 2006-1-1
2 1 2006-1-1
3 3 2006-1-2
3 3 2006-1-2
3 4 2006-1-2
4 3 2006-1-2
dawugui 2006-12-06
  • 打赏
  • 举报
回复
另外一种方法

if object_id('pubs..tb') is not null
drop table tb
go

create table tb
(
a int,
b int,
c varchar(10)
)

insert into tb(a,b,c) values(1, 1, '2006-1-1')
insert into tb(a,b,c) values(2 , 2, '2006-1-1')
insert into tb(a,b,c) values(3 , 3, '2006-1-2')
insert into tb(a,b,c) values(4 , 4, '2006-1-2')

select * from tb b
where a=(select top 1 a from tb where c=b.c)

drop table tb

a b c
----------- ----------- ----------
1 1 2006-1-1
3 3 2006-1-2

(所影响的行数为 2 行)
dawugui 2006-12-06
  • 打赏
  • 举报
回复
if object_id('pubs..tb') is not null
drop table tb
go

create table tb
(
a int,
b int,
c varchar(10)
)

insert into tb(a,b,c) values(1, 1, '2006-1-1')
insert into tb(a,b,c) values(2 , 2, '2006-1-1')
insert into tb(a,b,c) values(3 , 3, '2006-1-2')
insert into tb(a,b,c) values(4 , 4, '2006-1-2')

select m.* from tb m,
(select c,min(a) as a from tb group by c) n
where m.c = n.c and m.a = n.a

drop table tb


a b c
----------- ----------- ----------
1 1 2006-1-1
3 3 2006-1-2

(所影响的行数为 2 行)
flashkit 2006-12-06
  • 打赏
  • 举报
回复
而且top 1只会出来一条记录啊。
dawugui 2006-12-06
  • 打赏
  • 举报
回复
A B C
1 1 2006-1-1
2 2 2006-1-1
3 3 2006-1-2
4 4 2006-1-2
select m.* from tb,
(select c,min(a) as a from tb group by c) n
where m.c = n.c and m.a = n.a

在你的例中取min(b)也可.
kakalution 2006-12-06
  • 打赏
  • 举报
回复
同意樓上的
flashkit 2006-12-06
  • 打赏
  • 举报
回复
有试过吗?*在聚合里面不好用吧
caixia615 2006-12-06
  • 打赏
  • 举报
回复
select top 1 * from table group by c
flashkit 2006-12-06
  • 打赏
  • 举报
回复
不知道数据量大的话,这种方法效率如何

34,590

社区成员

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

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