SQL请求:
客户的numClient,nomClient,numClient,numTel,订购的总金额
对于prixUnitaire所依赖的文章> 2000年,以及prixUnitaire所属的文章的总数量< 1500
订购了大量专利文章的客户
为modic文章订购的金额。
这是关联的表格
答案 0 :(得分:0)
我的理解:
您没有提到您正在使用的数据库。我使用的是PostgreSQL,但它都是标准的SQL,因此应该适用于任何标准的关系数据库。
建立
create table client
(
num_client integer,
nom_client varchar(16),
num_tel varchar(20),
constraint primary_key_client primary key (num_client)
);
create table article
(
num_article integer,
description varchar(16),
prix_unitaire decimal(9,2),
quantite_en_stock integer,
constraint primary_key_article primary key (num_article)
);
create table commande
(
num_commande integer,
date_commande date,
num_client integer,
constraint primary_key_commande primary key (num_commande),
constraint foreign_key_commande_client foreign key (num_client) references client(num_client)
);
create table ligne_commande
(
num_commande integer,
num_article integer,
quantite integer,
constraint foreign_key_ligne_commande_commande foreign key (num_commande) references commande(num_commande),
constraint foreign_key_ligne_commande_article foreign key (num_article) references article(num_article)
);
insert into client (num_client, nom_client, num_tel) values (123, 'Dubois', '1 41 17 86 62');
insert into client (num_client, nom_client, num_tel) values (456, 'Bochet', '1 35 32 65 58');
insert into article (num_article, description, prix_unitaire, quantite_en_stock) values (79362, 'chaise', 1499.00, 36);
insert into article (num_article, description, prix_unitaire, quantite_en_stock) values (83746, 'placard', 2001.00, 14);
insert into commande (num_commande, date_commande, num_client) values (10030, '2018-01-13', 123);
insert into commande (num_commande, date_commande, num_client) values (10019, '2018-01-12', 456);
insert into ligne_commande (num_commande, num_article, quantite) values (10030, 79362, 4);
insert into ligne_commande (num_commande, num_article, quantite) values (10030, 83746, 1);
insert into ligne_commande (num_commande, num_article, quantite) values (10019, 79362, 1);
insert into ligne_commande (num_commande, num_article, quantite) values (10019, 83746, 1);
insert into ligne_commande (num_commande, num_article, quantite) values (10019, 83746, 1);
查询
select
a.num_client,
a.nom_client,
a.num_tel,
dispendic.sum_quantite as dispendic_amount,
modic.sum_quantite as modic_amount
from
client a
join
commande b
on a.num_client = b.num_client
join
(
select
num_commande,
sum(quantite) as sum_quantite
from
ligne_commande w
join
article x
on w.num_article = x.num_article
where
x.prix_unitaire < 1500
group by
num_commande
) modic
on b.num_commande = modic.num_commande
join
(
select
num_commande,
sum(quantite) as sum_quantite
from
ligne_commande w
join
article x
on w.num_article = x.num_article
where
x.prix_unitaire > 2000
group by
num_commande
) dispendic
on b.num_commande = dispendic.num_commande
where
dispendic.sum_quantite < modic.sum_quantite;
结果:
num_client | nom_client | num_tel | dispendic_amount | modic_amount
------------+------------+---------------+------------------+--------------
123 | Dubois | 1 41 17 86 62 | 1 | 4
(1 row)