[英]Select query on temp table without using cursor SQL
I want perform SQL query on table schema 我想在表模式上执行SQL查询
Id | BusNo | PartNo |dateAdded
1 | 437 | 2 |2014-02-28
2 | 423 | 3 |2014-03-28
3 | 423 | 3 |2014-04-28
4 | 437 | 2 |2014-03-28
5 | 452 | 1 |2014-03-29
I would like to select results top ID
order by date with where condition on BusNo
and PartNo
.Result would be like this 我想选择结果顶部
ID
按日期排序,其中BusNo
和PartNo
上的条件BusNo
将是这样的
Id | BusNo | PartNo |dateAdded
3 | 423 | 3 |2014-04-28
4 | 437 | 2 |2014-03-28
5 | 452 | 1 |2014-03-29
I tried 我试过了
select [Id]
into
from [PartUsed]
where BusNo = @busNo and [PartNo] exists (select ID from @Usertable)
@userTable
is user defined table type, but it will select all rows and I want top 1 in partNo
group order by dateAdded
. @userTable
是用户定义的表类型,但它将选择所有行,并且我希望在partNo
组中排名前1,按dateAdded
。
With cte as ( Select id,busno,partno,dateadded,
Row_Number() over( partition by partno order by dateadded desc ) as seqNum
from Partused
)
select id,busno,partno,dateadded
from cte
where seqNum=1
;with x as (
select *, row_number() over(partition by PartNo order by DateAdded desc) as rn
from PartsUsed
)
select *
from x
where x.BusNo = @busNo
and x.PartNo in (select ID from @Usertable)
and x.rn = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.