请教一个sql语句!谢谢

maotin 2004-12-17 05:27:32
我有一个结果集:
----------------------------------------------------
ID CompanyID ProductDetailID Count
10 50 682 A
11 50 682 B
14 60 689 C
15 60 689 D
18 70 697 E
19 70 697 F
20 70 697 G
-----------------------------------------------------

我想取出CompanyID和ProductDetailID一样得,但是ID是最小得那一条记录
请问怎么写sql?
谢谢!
...全文
121 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
vinsonshen 2004-12-17
  • 打赏
  • 举报
回复
楼主帮试下这个:
select * from 表名 as a where not exists (select * from 表名 where CompanyID=a.CompanyID and ProductDetailID=a.ProductDetailID and [ID]<a.[ID])
tengjian1981 2004-12-17
  • 打赏
  • 举报
回复
select a.*,b.Count
from
(select min(ID) as id,CompanyID,ProductDetailID
from Table
group by CompanyID,ProductDetailID ) a
left join Table b on a.ID=b.ID

didoleo 2004-12-17
  • 打赏
  • 举报
回复
select * from 表 a
where not exists (select * from 表 where companyid=a.companyid and productdetailid=a.productdetailid and id<a.id)
子陌红尘 2004-12-17
  • 打赏
  • 举报
回复
select
a.*
from
tabname a
inner join
(select CompanyID,ProductDetailID ,min(ID) as ID
from tabname
group by CompanyID,ProductDetailID ) b
on
a.CompanyID = b.CompanyID and a.ProductDetailID = b.ProductDetailID and a.ID = b.ID
clx333 2004-12-17
  • 打赏
  • 举报
回复
select * from tb a where not exists (select * from tb where companyid=a.companyid and productdetailid=a.productdetailid and id>a.id)
pbsql 2004-12-17
  • 打赏
  • 举报
回复
select * from t,
(select id=min(id),CompanyID,ProductDetailID from t group by ompanyID,ProductDetailID) a
where t.id=a.id and t.CompanyID=a.CompanyID and t.ProductDetailID=a.ProductDetailID
lsxaa 2004-12-17
  • 打赏
  • 举报
回复
select a.*
from 表 a,(select min(ID) as id,CompanyID,ProductDetailID
from 表
group by CompanyID,ProductDetailID ) b
where a.id=b.id
maotin 2004-12-17
  • 打赏
  • 举报
回复
这是我想要得结果:
----------------------------------------------------
ID CompanyID ProductDetailID Count
10 50 682 A
14 60 689 C
18 70 697 E
-----------------------------------------------------

34,590

社区成员

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

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