Sql 为另一个表中的每个Id从表中选择Top 1

Sql 为另一个表中的每个Id从表中选择Top 1,sql,sql-server,tsql,Sql,Sql Server,Tsql,表结构为: create table fruit ( id int identity(1,1), name varchar(max) ) create table fruit_allocation ( id int identity(1,1), fruit_id int references fruit(id), customer_id int references store(id), amount float, ) create table measurement

表结构为:

create table fruit (
  id int identity(1,1),
  name varchar(max)
)

create table fruit_allocation (
  id int identity(1,1),
  fruit_id int references fruit(id),
  customer_id int references store(id),
  amount float,
)

create table measurement (
  fruit_allocation_id int references fruit_allocation(id),
  measurement_date datetime,
  measurement float,
)
每个水果可以分配给多个客户,创建水果分配记录。每个水果分配记录可以有多个测量值

我想为给定水果id的每个水果分配选择最新的测量值

到目前为止,我有以下几点:

select * 
  from measurement 
 where fruit_allocation_id in (select id 
                                 from fruit_allocation 
                                where fruit_id = 10)

这将返回该水果的所有度量值,我只想为每个水果分配返回一个度量值。

创建一个子查询以查找每个分配的最新度量值,然后连接到该子查询,就像它是一个实表一样

select * from measurement meas
join
        (
        SELECT  fruit_allocation_id,
                MAX(measurement_date) as max_date
        FROM    measurement meas2
        JOIN    fruit_allocation alloc
        ON      alloc.id = meas2.fruit_allocation_id
        where fruit_id = 10
        ) max_meas
on      meas.fruit_allocation_id = max_mes.fruit_allocation_id
and     meas.measurement_date = max_meas.max_date
你可以


假设您使用的是SQLServer2005+

With RankedMeasurements As
    (
    Select M.fruit_allocation_id
        , M.measurement_date
        , M.measurement
        , Row_Number() Over ( Partition By M.fruit_allocation_id 
                                Order By M.measurement_date Desc ) As Rnk
    From measurement As M
    Where Exists    (
                    Select 1
                    From fruit_allocation As FA1
                    Where FA1.id = M.fruit_allocation_id
                        And FA1.fruit_id = 10
                    )   
    )
Select RM.fruit_allocation_id
    , RM.measurement_date
    , RM.measurement
From RankedMeasurements As RM
Where Rnk = 1
选择* 从测量m 其中测量日期=从测量m1中选择前1个测量日期 其中m1.fruit\u allocation\u id=m.fruit\u allocation\u id 按测量值订购\u日期说明 和 选择id中的水果分配id 从水果分配
如果fruit_id=3

您可能会发现关于应用与加入的讨论很有趣:@Don只有在加入有效的情况下,vs才适用。在这种情况下,联接无法计算出相关的TOP 1。但是,感谢您链接到BOL并提供了一个很好的阅读,有更多创造性的方法可以使用连接来解决此问题。请看我的答案:不过,了解APPLY很有趣,在这种情况下,它听起来可能更有效。顺便说一句,BOL?这是一本“学习”的书吗?@Don-B ooks O n L iner的缩写,但它似乎不太重,可以从[水果分配]获取数据。。。
With RankedMeasurements As
    (
    Select M.fruit_allocation_id
        , M.measurement_date
        , M.measurement
        , Row_Number() Over ( Partition By M.fruit_allocation_id 
                                Order By M.measurement_date Desc ) As Rnk
    From measurement As M
    Where Exists    (
                    Select 1
                    From fruit_allocation As FA1
                    Where FA1.id = M.fruit_allocation_id
                        And FA1.fruit_id = 10
                    )   
    )
Select RM.fruit_allocation_id
    , RM.measurement_date
    , RM.measurement
From RankedMeasurements As RM
Where Rnk = 1