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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
SQL>
SQL> drop table article;
Table dropped.
SQL>
SQL> alter session set nls_date_format = 'DD-MON-YYYY';
Session altered.
SQL>
SQL> create table article (materiel number, prix1 number, prix2 number, modif date);
Table created.
SQL>
SQL> insert into article values(1, 10, 11, '01-JAN-2006');
1 row created.
SQL> insert into article values(1, 11, 22, '01-APR-2006');
1 row created.
SQL> insert into article values(2, 7, 8, '20-FEB-2006');
1 row created.
SQL> commit;
Commit complete.
SQL>
SQL> --
SQL> -- requête imbriquée
SQL> --
SQL>
SQL> select materiel, prix1, prix2, modif from article a1
2 where modif = (select max(a2.modif) from article a2 where a2.materiel = a1.materiel);
MATERIEL PRIX1 PRIX2 MODIF
---------- ---------- ---------- -----------
1 11 22 01-APR-2006
2 7 8 20-FEB-2006
SQL>
SQL>
SQL> --
SQL> -- fonction analytique
SQL> --
SQL>
SQL> select * from (select materiel, prix1, prix2,
2 max(modif) over (partition by materiel) date_derniere_modif,
3 row_number() over (partition by materiel order by modif desc) rn
4 from article)
5 where rn = 1;
MATERIEL PRIX1 PRIX2 DATE_DERNIE RN
---------- ---------- ---------- ----------- ----------
1 11 22 01-APR-2006 1
2 7 8 20-FEB-2006 1
SQL>
SQL> exit |
Partager