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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| CREATE DATABASE '..\Data\Base.fdb' page_size 4096 DEFAULT CHARACTER SET WIN1252;
-- ===========================
-- Création de la table 'test'
-- ===========================
create table test (
id integer generated by default as identity not null,
nid smallint not null,
nom varchar(10) not null,
prenom varchar(10) not null,
primary key (id)
);
-- =====================
-- Insertion dans 'test'
-- =====================
insert into test (nid,nom,prenom) values (1, 'Martin', 'Paul');
insert into test (nid,nom,prenom) values (2, 'Dupond', 'Olivier');
insert into test (nid,nom,prenom) values (2, 'Toto', 'Marc');
insert into test (nid,nom,prenom) values (3, 'Grandjean', 'Léon');
insert into test (nid,nom,prenom) values (3, 'Durand', 'Albert');
insert into test (nid,nom,prenom) values (3, 'Smith', 'John');
-- ================
-- Vidage de 'test'
-- ================
select *
from test;
ID NID NOM PRENOM
============ ======= ========== ==========
1 1 Martin Paul
2 2 Dupond Olivier
3 2 Toto Marc
4 3 Grandjean Léon
5 3 Durand Albert
6 3 Smith John
-- ===========
-- Requête N°1
-- ===========
select t1.*
from test as t1
inner join ( select nid,
min(id) as id
from test
group by nid
) as t2
on t2.id = t1.id;
ID NID NOM PRENOM
============ ======= ========== ==========
1 1 Martin Paul
2 2 Dupond Olivier
4 3 Grandjean Léon
-- ===========
-- Requête N°2
-- ===========
select t1.*
from test as t1
inner join ( select nid,
max(id) as id
from test
group by nid
) as t2
on t2.id = t1.id;
ID NID NOM PRENOM
============ ======= ========== ==========
1 1 Martin Paul
3 2 Toto Marc
6 3 Smith John
exit;
Appuyez sur une touche pour continuer... |