1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| /* REQUETE 1 */
SELECT p.id_produit,p.nom,pcb.code_barre
FROM produit p
left outer join (
select id_produit, max(ISNULL(code_barre, 0)) as code_barre
from Produit_code_barre
group by id_produit
) pcb on pcb.id_produit = p.id_produit
where p.id_type_produit = 1
and p.id_produit < 5000
/* REQUETE 2 */
select p.id_produit,p.nom,pcb.code_barre from
produit p
outer apply (
select top 1 sub_pcb.id_produit, sub_pcb.code_barre
from Produit_code_barre sub_pcb
where p.id_produit=sub_pcb.id_produit
order by sub_pcb.id_produit, sub_pcb.code_barre
) pcb
where p.id_type_produit = 1
and p.id_produit < 5000
/* REQUETE 3 */
SELECT id_produit,nom,code_barre
FROM (
SELECT p.id_produit,p.nom,pcb.code_barre, RANK() OVER (PARTITION BY p.id_produit ORDER BY pcb.code_barre DESC) AS RANK
FROM produit p
LEFT OUTER JOIN Produit_code_barre pcb
ON pcb.id_produit = p.id_produit
WHERE p.id_type_produit = 1
AND p.id_produit < 5000
) T
WHERE RANK = 1 |
Partager