数据重复查询

yelang771 2009-07-30 01:26:59
数据库第一行有不同数据,
不管第一行,
其余行有相同的取其中一行
第一行要查询出来.

a ,b
1 1
2 1
3 2
4 2
5 2

得到数据:

a ,b
1 1
3 2
...全文
89 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
topest0302 2009-07-30
  • 打赏
  • 举报
回复

declare @a table (a int,b int)
insert @a select
1 , 1 union all select
2 ,1 union all select
3 ,2 union all select
4 , 2 union all select
5, 2
select min(a) as a,b
from @a
group by b
叶子 2009-07-30
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 jwdream2008 的回复:]
引用 4 楼 yelang771 的回复:
谢谢

如果数据表后面有很多列,
是不是要 group by 全列出来.

select min(a) as a,b,c,d,e,f,g
from ta
group by b,c,d,e,f,g
这样.

这样做:
SQL codeselect*from tawhere ain (selectmin(a)from tagroupby b)
[/Quote]

8楼这个in有问题:

declare @table table (a int,b int,c int ,d int)
insert into @table
select 1,1 ,4,5 union all
select 2,1 ,6,5 union all
select 3,2 ,4,3 union all
select 4,2 ,5,4 union all
select 5,2 ,4,3 union all
select 2,2 ,3,2

select * from @table
where a in (select min(a) from @table group by b)

/*
a b c d
----------- ----------- ----------- -----------
1 1 4 5
2 1 6 5
2 2 3 2
*/



叶子 2009-07-30
  • 打赏
  • 举报
回复

declare @table table (a int,b int,c int ,d int)
insert into @table
select 1,1 ,4,5 union all
select 2,1 ,6,5 union all
select 3,2 ,4,3 union all
select 4,2 ,5,4 union all
select 5,2 ,4,3

--如果字段多,这样不行
select min(a) as a,b ,c,d
from @table
group by b,c,d
/*
a b c d
----------- ----------- ----------- -----------
1 1 4 5
2 1 6 5
3 2 4 3
4 2 5 4
*/

--这样可以:
select * from @table t
where a=(select min(a) from @table where b=t.b) order by b

/*
a b c d
----------- ----------- ----------- -----------
1 1 4 5
3 2 4 3
*/
叶子 2009-07-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yelang771 的回复:]
谢谢

如果数据表后面有很多列,
是不是要 group by 全列出来.

select min(a) as a,b,c,d,e,f,g
from ta
group by b,c,d,e,f,g
这样.
[/Quote]

不行


declare @table table (a int,b int,c int ,d int)
insert into @table
select 1,1 ,4,5 union all
select 2,1 ,6,5 union all
select 3,2 ,4,3 union all
select 4,2 ,5,4 union all
select 5,2 ,4,3

--如果字段多,这样不行
select min(a) as a,b ,c,d
from @table
group by b,c,d

--这样可以:
select * from @table t
where a=(select min(a) from @table where b=t.b) order by b
izbox 2009-07-30
  • 打赏
  • 举报
回复

Create table #a(a int,b int ,c int)
insert into #a
select 1,1,1 union all
select 2,1,2 union all
select 3,2,3 union all
select 4,2,4 union all
select 5,2,5


select *
from #a a
where not exists(select a from #a where b=a.b and a.a>a)

-----------结果-----------
1 1 1
3 2 3


刚刚说错了 不好意思!
jwdream2008 2009-07-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yelang771 的回复:]
谢谢

如果数据表后面有很多列,
是不是要 group by 全列出来.

select min(a) as a,b,c,d,e,f,g
from ta
group by b,c,d,e,f,g
这样.
[/Quote]
这样做:
select * from ta 
where a in (select min(a) from ta group by b)
izbox 2009-07-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yelang771 的回复:]
谢谢

如果数据表后面有很多列,
是不是要 group by 全列出来.

select min(a) as a,b,c,d,e,f,g
from ta
group by b,c,d,e,f,g
这样.
[/Quote]

如果很多列就用这个

select *
from tb a
where a = (select a from tb where b=a.b and a.ID<tb.ID)
jwdream2008 2009-07-30
  • 打赏
  • 举报
回复

Select max(a) as a,b
from ta
group by b
或者
select min(a) as a,b
from ta
group by b
izbox 2009-07-30
  • 打赏
  • 举报
回复

select min(a) as a,b
from ta
group by b
yelang771 2009-07-30
  • 打赏
  • 举报
回复
谢谢

如果数据表后面有很多列,
是不是要 group by 全列出来.

select min(a) as a,b,c,d,e,f,g
from ta
group by b,c,d,e,f,g
这样.
xxmsuper 2009-07-30
  • 打赏
  • 举报
回复

select *
from @T A
where not exists(select 1 from @T where b=A.b and a<A.a)
Yang_ 2009-07-30
  • 打赏
  • 举报
回复
select *
from tb a
where a = (select top 1 a from tb where b=a.b)
-狙击手- 2009-07-30
  • 打赏
  • 举报
回复
select min(a) as a,b
from ta
group by b

34,571

社区成员

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

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