怎么用一条语句把我group by id的各个id的第一条一起取出来,就是对应的各个id就取一条

smiletosky 2003-09-19 04:29:34
!!!!
...全文
416 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiangchuandong 2003-09-20
  • 打赏
  • 举报
回复
假如你有三个字段a,b,c
那么这样写
select min(a),b,c from tableName group by b,c
yonghengdizhen 2003-09-20
  • 打赏
  • 举报
回复
上面的代码并没有用到类型转换.类型转换只有在复合主键由不同类型的字段构成时需要进行,这个时候将各字段转换成统一类型进行组合运算(注意,不能有损数据精度)并仍然采用上面的相关子查询方法
yonghengdizhen 2003-09-20
  • 打赏
  • 举报
回复
上面aierong(皑婀瑢-数据库XML.NET联盟会科长)
写的是针对有单一主键定义的情况.如果没有单一主键,可以通过类型转换及字符串操作虚拟一个符合主键完成关联系条件..

举例如下:
create table ai (departid int,text1 varchar,text2 varchar)
insert into ai select 1,'t','a'
insert into ai select 1,'t','b'
insert into ai select 1,'a','c'
insert into ai select 3,'h','d'
insert into ai select 4,'d','d'
insert into ai select 6,'j','f'
insert into ai select 3,'d','f'
insert into ai select 4,'h','e'
insert into ai select 6,'h','h'

select *
from ai as a
where (a.text1+a.text2)=(select top 1 (b.text1+b.text2) from ai as b where a.departid=b.departid)
yonghengdizhen 2003-09-20
  • 打赏
  • 举报
回复
一个相关子查询的应用而已..建议去翻翻这个版的精华区或者faq
luominggui 2003-09-20
  • 打赏
  • 举报
回复
对 yonghengdizhen(9.18=>警钟长鸣,勿忘国耻)的做一下修改,我想应是你想要的了!

create table ai (departid int,text1 varchar,text2 varchar)
insert into ai select 1,'t','a'
insert into ai select 1,'t','a'
insert into ai select 1,'t','b'
insert into ai select 1,'a','c'
insert into ai select 3,'h','d'
insert into ai select 4,'d','d'
insert into ai select 6,'j','f'
insert into ai select 3,'d','f'
insert into ai select 4,'h','e'
insert into ai select 6,'h','h'

select distinct *
from ai as a
where (str(a.departid)+a.text1+a.text2)=(select top 1 (str(b.departid)+b.text1+b.text2) from ai as b where a.departid=b.departid)
happy_0325 2003-09-20
  • 打赏
  • 举报
回复
select *
from ai as a
where a.empid=(select min(b.empid) from ai as b where a.departid=b.departid group by departid)
smiletosky 2003-09-20
  • 打赏
  • 举报
回复
谢谢大家,我去试验一下
zhbname 2003-09-19
  • 打赏
  • 举报
回复
用aierong(皑婀瑢-数据库XML.NET联盟会科长)的
accounant 2003-09-19
  • 打赏
  • 举报
回复
select min(empid),departid from tableName group by departid
aierong 2003-09-19
  • 打赏
  • 举报
回复
create table ai (empid int, departid int)
insert into ai select 1,1
insert into ai select 2,1
insert into ai select 3,1
insert into ai select 4,2
insert into ai select 5,2
insert into ai select 6,2

go

select *
from ai as a
where a.empid=(select min(b.empid) from ai as b where a.departid=b.departid)

这个方法啦
字段再多也没有关系
smiletosky 2003-09-19
  • 打赏
  • 举报
回复
说明一下,一条语句!
smiletosky 2003-09-19
  • 打赏
  • 举报
回复
select min(empid),departid from tableName group by departid
我上面,只是一个例子,其实字段很多,当min,max不能处理的那些字段怎么办
aierong 2003-09-19
  • 打赏
  • 举报
回复


create table ai (empid int, departid int)
insert into ai select 1,1
insert into ai select 2,1
insert into ai select 3,1
insert into ai select 4,2
insert into ai select 5,2
insert into ai select 6,2

go

select *
from ai as a
where a.empid=(select min(b.empid) from ai as b where a.departid=b.departid)

go

select min(empid)as empid,departid
from ai
group by departid

aierong 2003-09-19
  • 打赏
  • 举报
回复




create table ai (empid int, departid int)
insert into ai select 1,1
insert into ai select 2,1
insert into ai select 3,1
insert into ai select 4,2
insert into ai select 5,2
insert into ai select 6,2

go

select *
from ai as a
where a.empid=(select min(b.empid) from ai as b where a.departid=b.departid)
yujohny 2003-09-19
  • 打赏
  • 举报
回复
select min(empid),departid from tableName group by departid
cloudchen 2003-09-19
  • 打赏
  • 举报
回复
select min(empid),departid from tableName group by departid
smiletosky 2003-09-19
  • 打赏
  • 举报
回复
我好象问错拉
empid departid
1 1
2 1
3 1
4 2
5 2
6 2
我只想取
1 ,1
4,2
怎么取
wzyu 2003-09-19
  • 打赏
  • 举报
回复
from tablename as a
where id in(select top 1 id from tablename as b where a.id=b.id)
group by id

看看吧!
aierong 2003-09-19
  • 打赏
  • 举报
回复
最好把数据帖出来
看看
aierong 2003-09-19
  • 打赏
  • 举报
回复




from tablename as a
where id in(select top 1 id from tablename as b where a.id=b.id)
group by id

34,874

社区成员

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

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