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

smiletosky 2003-09-19 04:29:34
!!!!
...全文
420 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
内容概要:本文介绍了一种基于飞蛾扑火算法(Moth-Flame Optimization, MFO)的三维路径规划方法,并提供了完整的Matlab代码实现。该方法受自然界中飞蛾围绕火焰旋转飞行行为的启发,通过模拟飞蛾在搜索空间中围绕多个“火焰”(即潜在最优解)进行螺旋式搜索的机制,实现对复杂三维空间中最优路径的高效寻优。算法在路径规划过程中充分考虑了起点、终点、障碍物分布及路径长度、安全性等约束条件,能够有效避开障碍物并生成平滑、短距离且低能耗的飞行路径。文中通过仿真实验验证了该算法在三维空间路径规划中的可行性与优越性,适用于无人机、机器人等智能体的自主导航任务。; 适合人群:具备一定Matlab编程基础和优化算法背景的科研人员、自动化与人工智能方向的研究生及工程技术人员。; 使用场景及目标:①应用于无人机、无人车、水下潜航器等智能体在复杂三维环境中的自主路径规划;②用于对比研究不同群智能优化算法(如PSO、GWO、WOA等)在路径规划中的性能差异;③为智能导航系统的设计与仿真提供算法支持和技术参考。; 阅读建议:建议读者结合Matlab代码深入理解算法实现细节,重点关注飞蛾位置更新、火焰选择机制及路径约束处理部分,可通过调整参数或引入其他优化策略进一步提升算法性能,并尝试将其拓展至动态环境或多目标路径规划问题中。

34,875

社区成员

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

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