求SQL 语句

ballatong 2015-12-03 11:59:10
我已经从两者表中关联到了数据,使用的语句如下:
select FF.BatchID,ff.[Order],kk.Description,ff.CurrentProcess,ff.CurrentProcessNumber from NewProcessTracking as FF left join NewBatchTracking as KK
on ff.BatchID=KK.BatchBarcode where ff.ScheduleDate is not null

显示结果如下图:



现在想要的结果是在这个select上再过滤 BatchID重复行,取 CurrentProcessNumber 最小的,请问怎么实现?
...全文
86 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ballatong 2015-12-03
  • 打赏
  • 举报
回复
楼上正确! 谢谢, 结贴给分
spiritofdragon 2015-12-03
  • 打赏
  • 举报
回复
select 
	t.BatchID,t.[Order],t.Description,t.CurrentProcess,t.CurrentProcessNumber  
from(
	select FF.BatchID,ff.[Order],kk.Description,ff.CurrentProcess,ff.CurrentProcessNumber  
		,ROW_NUMBER()over(partition by FF.BatchID order by ff.CurrentProcessNumber) rn
	from  NewProcessTracking as FF 
	left join NewBatchTracking as KK on ff.BatchID=KK.BatchBarcode 
	where ff.ScheduleDate is not null 
	) t
where rn=1
	
ballatong 2015-12-03
  • 打赏
  • 举报
回复
楼上的报错啊 Msg 4104, Level 16, State 1, Line 3 The multi-part identifier "FF.BatchID" could not be bound. Msg 4104, Level 16, State 1, Line 3 The multi-part identifier "ff.Order" could not be bound. Msg 4104, Level 16, State 1, Line 4 The multi-part identifier "kk.Description" could not be bound. Msg 4104, Level 16, State 1, Line 5 The multi-part identifier "ff.CurrentProcess" could not be bound. Msg 4104, Level 16, State 1, Line 6 The multi-part identifier "ff.CurrentProcessNumber" could not be bound.
Neo_whl 2015-12-03
  • 打赏
  • 举报
回复

--参考:
    select     
    FF.BatchID,ff.[Order],
    kk.Description,
    ff.CurrentProcess,
    ff.CurrentProcessNumber,
    ROW_NUMBER() over(partition by Batchid order by CurrentProcessNumber)  as number
from(
    select 
    FF.BatchID,ff.[Order],
    kk.Description,
    ff.CurrentProcess,
    ff.CurrentProcessNumber,
    ROW_NUMBER() over(partition by Batchid order by CurrentProcessNumber)  as number
from  
NewProcessTracking as FF left join NewBatchTracking as KK on ff.BatchID=KK.BatchBarcode where ff.ScheduleDate is not null )t
 where number=1
卖水果的net 版主 2015-12-03
  • 打赏
  • 举报
回复

-- 大概这个样子 

select * from (
select row_number() over(partition by BatchID order by CurrentProcessNumber) rn 
from xxx 
) x 
where rn = 1 



34,593

社区成员

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

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