par
Tracker » 13 oct. 2007, 20:50
Salut,
J'avais un peu de temps, alors voilà:
Struture utilisée:
Code : Tout sélectionner
create table Produit
(
code int not null primary key auto_increment ,
nom varchar(50) not null,
prix float(53) not null,
categorie varchar(10) not null
);
insert Produit (nom, prix, categorie) values ('ABIT AB9 (Intel P965 Express) - ATX', 122.9, 'Matériel');
insert Produit (nom, prix, categorie) values ('Alim 300W ATX CE sans boitier', 13.95, 'Matériel');
insert Produit (nom, prix, categorie) values ('Textorm Boîtier 2"1/2 Hotsync', 19.95, 'Matériel');
insert Produit (nom, prix, categorie) values ('Adaptec PCI ASC 29160 SCSI160 kit', 269, 'Matériel');
insert Produit (nom, prix, categorie) values ('Eset NOD32 - Pack 5 postes (français, WINDOWS)', 179.01, 'Logiciel');
insert Produit (nom, prix, categorie) values ('Nero 8 (français, WINDOWS)',78.51, 'Logiciel');
insert Produit (nom, prix, categorie) values ('Drive Clone 2 (français, WINDOWS)', 36.9, 'Logiciel');
insert Produit (nom, prix, categorie) values ('Mandriva Linux Powerpack+ 2007 Spring (français)', 189.49, 'Logiciel');
insert Produit (nom, prix, categorie) values ('Microsoft Windows Vista Édition Familiale Premium (français)', 338.99, 'Logiciel');
insert Produit (nom, prix, categorie) values ('Pack OFFICE One 6.5 (français, WINDOWS)', 69.89, 'Logiciel');
Liste de tes requêtes:
Code : Tout sélectionner
-- # 1
select *
from produit p
inner join
( select categorie, max(prix) as prix from produit group by categorie ) cm
on p.categorie = cm.categorie
and p.prix = cm.prix
-- # 2
select *
from produit p
inner join
( select categorie, avg(prix) as prix_moyen from produit group by categorie ) cm
on p.categorie = cm.categorie
and p.prix > cm.prix_moyen
-- # 3
select categorie, count(*) as nombre, avg(prix) as moyen from produit group by categorie with rollup
Tracker.
[edit]
Pour la seconde, j'ai considéré que tu voulais les articles dont le prix était supérieur à la moyenne de leur catégorie. Si tu veux l'info par rapport à la moyenne totale:
Code : Tout sélectionner
-- # 2
select *
from produit p
inner join
( select avg(prix) as prix_moyen from produit ) cm
on p.prix > cm.prix_moyen
a+
Salut,
J'avais un peu de temps, alors voilà:
Struture utilisée:
[code]
create table Produit
(
code int not null primary key auto_increment ,
nom varchar(50) not null,
prix float(53) not null,
categorie varchar(10) not null
);
insert Produit (nom, prix, categorie) values ('ABIT AB9 (Intel P965 Express) - ATX', 122.9, 'Matériel');
insert Produit (nom, prix, categorie) values ('Alim 300W ATX CE sans boitier', 13.95, 'Matériel');
insert Produit (nom, prix, categorie) values ('Textorm Boîtier 2"1/2 Hotsync', 19.95, 'Matériel');
insert Produit (nom, prix, categorie) values ('Adaptec PCI ASC 29160 SCSI160 kit', 269, 'Matériel');
insert Produit (nom, prix, categorie) values ('Eset NOD32 - Pack 5 postes (français, WINDOWS)', 179.01, 'Logiciel');
insert Produit (nom, prix, categorie) values ('Nero 8 (français, WINDOWS)',78.51, 'Logiciel');
insert Produit (nom, prix, categorie) values ('Drive Clone 2 (français, WINDOWS)', 36.9, 'Logiciel');
insert Produit (nom, prix, categorie) values ('Mandriva Linux Powerpack+ 2007 Spring (français)', 189.49, 'Logiciel');
insert Produit (nom, prix, categorie) values ('Microsoft Windows Vista Édition Familiale Premium (français)', 338.99, 'Logiciel');
insert Produit (nom, prix, categorie) values ('Pack OFFICE One 6.5 (français, WINDOWS)', 69.89, 'Logiciel');
[/code]
Liste de tes requêtes:
[code]
-- # 1
select *
from produit p
inner join
( select categorie, max(prix) as prix from produit group by categorie ) cm
on p.categorie = cm.categorie
and p.prix = cm.prix
-- # 2
select *
from produit p
inner join
( select categorie, avg(prix) as prix_moyen from produit group by categorie ) cm
on p.categorie = cm.categorie
and p.prix > cm.prix_moyen
-- # 3
select categorie, count(*) as nombre, avg(prix) as moyen from produit group by categorie with rollup
[/code]
Tracker.
[b][edit][/b]
Pour la seconde, j'ai considéré que tu voulais les articles dont le prix était supérieur à la moyenne de leur catégorie. Si tu veux l'info par rapport à la moyenne totale:
[code]
-- # 2
select *
from produit p
inner join
( select avg(prix) as prix_moyen from produit ) cm
on p.prix > cm.prix_moyen
[/code]
a+