Sélectionnez Top 1 dans une table pour chaque ID d'une autre table

La structure de la table est:

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, ) 

Chaque fruit peut être alloué à plusieurs clients en créant un logging fruit_allocation. Chaque logging fruit_allocation peut avoir plusieurs mesures.

Je veux sélectionner la dernière mesure pour chaque fruit_allocation donné un identifiant de fruit

Jusqu'à présent, j'ai ce qui suit:

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

Cela returnne toutes les mesures pour ce fruit, je veux juste returnner 1 mesure par fruit_allocation.

Vous pouvez appliquer CROSS

 select a.*, m.* from fruit_allocation a cross apply ( select top 1 * from measurement m where m.fruit_allocation_id = a.id order by m.measurement_date desc ) m where a.fruit_id = 10 

En supposant que vous utilisez SQL Server 2005+

 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 

Créez une sous-requête pour find la dernière mesure pour chaque allocation, puis joignez-la à cette sous-requête comme s'il s'agissait d'une table réelle.

 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 

select * à partir de la mesure m où measurement_date = (select top 1 measurement_date à partir de la mesure m1 où m1.fruit_allocation_id = m.fruit_allocation_id ordre par measurement_date desc) Et fruit_allocation_id in (select id de fruit_allocation où fruit_id = 3)