Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Probleme de requete

6 réponses
Avatar
Ralf Meuser
Bonjour


Je suis trop nulle pour cette requête.
Les totaux des montant sont incorrect, mais je ne sais pas pourquoi.

Je vous remercie d'avance pour votre aide.

salutations
Ralf


/* table commande */
create table #table_a (no_commande int, no_client int)
insert into #table_a (no_commande,no_client) values (1,10)
insert into #table_a (no_commande,no_client) values (2,11)
insert into #table_a (no_commande,no_client) values (3,14)
insert into #table_a (no_commande,no_client) values (4,10)
insert into #table_a (no_commande,no_client) values (5,10)
/* table detail d'une commande */
create table #table_b (no_commande int, ligne int, poids int)
insert into #table_b (no_commande,ligne,poids) values (1,1,10)
insert into #table_b (no_commande,ligne,poids) values (1,2,5)
insert into #table_b (no_commande,ligne,poids) values (1,3,2)
insert into #table_b (no_commande,ligne,poids) values (1,4,8)
insert into #table_b (no_commande,ligne,poids) values (2,1,15)
insert into #table_b (no_commande,ligne,poids) values (3,1,110)
insert into #table_b (no_commande,ligne,poids) values (4,1,200)
insert into #table_b (no_commande,ligne,poids) values (3,1,500)
/* table facturation */
create table #table_c (no_facture int, no_commande int, montant int)
insert into #table_c (no_facture,no_commande,montant) values (700,1,10)
insert into #table_c (no_facture,no_commande,montant) values (701,2,11)
insert into #table_c (no_facture,no_commande,montant) values (702,3,14)
insert into #table_c (no_facture,no_commande,montant) values (703,4,10)
insert into #table_c (no_facture,no_commande,montant) values (704,5,10)
/* voici ma requete*/
select a.no_client,sum(b.poids),sum(c.montant)
from #table_a a
left outer join #table_b b on b.no_commande=a.no_commande
left outer join #table_c c on c.no_commande=a.no_commande
group by a.no_client

6 réponses

Avatar
Robert Plagnard
Bonjour,

Les requêtes SQL avec des jointures restent des produits cartésien
d'ensemble. Dans le cas présenté le montant est compté plusieurs fois. Il
faut raisonner en terme de triplet (a, b, c) générés. En fait à chaque fois
qu'il y a correspondance entre A et B (et entre A et C) on compte C dans le
total, donc on compte le total de la commande pour chaque ligne !!.
Il faut faire deux requêtes :

select a.no_client,sum(b.poids)
from #table_a a
left outer join #table_b b on b.no_commande=a.no_commande
group by a.no_client

et

select a.no_client,sum(c.montant)
from #table_a a
left outer join #table_c c on c.no_commande=a.no_commande
group by a.no_client

Cordialement
--
Robert Plagnard
Communauté Francophone des Professionnels FoxPro
Pour un développement durable...
http://www.atoutfox.net



"Ralf Meuser" a écrit :

Bonjour


Je suis trop nulle pour cette requête.
Les totaux des montant sont incorrect, mais je ne sais pas pourquoi.

Je vous remercie d'avance pour votre aide.

salutations
Ralf


/* table commande */
create table #table_a (no_commande int, no_client int)
insert into #table_a (no_commande,no_client) values (1,10)
insert into #table_a (no_commande,no_client) values (2,11)
insert into #table_a (no_commande,no_client) values (3,14)
insert into #table_a (no_commande,no_client) values (4,10)
insert into #table_a (no_commande,no_client) values (5,10)
/* table detail d'une commande */
create table #table_b (no_commande int, ligne int, poids int)
insert into #table_b (no_commande,ligne,poids) values (1,1,10)
insert into #table_b (no_commande,ligne,poids) values (1,2,5)
insert into #table_b (no_commande,ligne,poids) values (1,3,2)
insert into #table_b (no_commande,ligne,poids) values (1,4,8)
insert into #table_b (no_commande,ligne,poids) values (2,1,15)
insert into #table_b (no_commande,ligne,poids) values (3,1,110)
insert into #table_b (no_commande,ligne,poids) values (4,1,200)
insert into #table_b (no_commande,ligne,poids) values (3,1,500)
/* table facturation */
create table #table_c (no_facture int, no_commande int, montant int)
insert into #table_c (no_facture,no_commande,montant) values (700,1,10)
insert into #table_c (no_facture,no_commande,montant) values (701,2,11)
insert into #table_c (no_facture,no_commande,montant) values (702,3,14)
insert into #table_c (no_facture,no_commande,montant) values (703,4,10)
insert into #table_c (no_facture,no_commande,montant) values (704,5,10)
/* voici ma requete*/
select a.no_client,sum(b.poids),sum(c.montant)
from #table_a a
left outer join #table_b b on b.no_commande=a.no_commande
left outer join #table_c c on c.no_commande=a.no_commande
group by a.no_client







Avatar
Fred BROUARD
Et en une seule requête :

select a.no_client, sum(b.poids)
(select sum(c.montant)
from #table_a aa
left outer join #table_c c
on c.no_commandeª.no_commande
WHERE aa.no_commande = a.no_commande
group by aa.no_client)
from #table_a a
left outer join #table_b b
on b.no_commande=a.no_commande
group by a.no_client


A +


Robert Plagnard a écrit :
Bonjour,

Les requêtes SQL avec des jointures restent des produits cartésien
d'ensemble. Dans le cas présenté le montant est compté plusieurs fois. Il
faut raisonner en terme de triplet (a, b, c) générés. En fait à chaque fois
qu'il y a correspondance entre A et B (et entre A et C) on compte C dans le
total, donc on compte le total de la commande pour chaque ligne !!.
Il faut faire deux requêtes :

select a.no_client,sum(b.poids)
from #table_a a
left outer join #table_b b on b.no_commande=a.no_commande
group by a.no_client

et

select a.no_client,sum(c.montant)
from #table_a a
left outer join #table_c c on c.no_commande=a.no_commande
group by a.no_client

Cordialement




--
Frédéric BROUARD, MVP SQL Server, expert bases de données et langage SQL
Le site sur le langage SQL et les SGBDR : http://sqlpro.developpez.com
Audit, conseil, expertise, formation, modélisation, tuning, optimisation
********************* http://www.datasapiens.com ***********************
Avatar
Ralf Meuser
Salut


Merci pour votre réponse. Deux requête ça marche mais demande un gros
travail de regroupement. J'ai bien aimé la seconde solution mais elle ne
passe pas pour moi.
Je utilise MS-SQL 2000
Ça doit marcher la aussi?

Ralf


"Fred BROUARD" a écrit dans le message de news:

Et en une seule requête :

select a.no_client, sum(b.poids)
(select sum(c.montant)
from #table_a aa
left outer join #table_c c
on c.no_commandeª.no_commande
WHERE aa.no_commande = a.no_commande
group by aa.no_client)
from #table_a a
left outer join #table_b b
on b.no_commande=a.no_commande
group by a.no_client


A +


Robert Plagnard a écrit :
Bonjour,

Les requêtes SQL avec des jointures restent des produits cartésien
d'ensemble. Dans le cas présenté le montant est compté plusieurs fois. Il
faut raisonner en terme de triplet (a, b, c) générés. En fait à chaque
fois qu'il y a correspondance entre A et B (et entre A et C) on compte C
dans le total, donc on compte le total de la commande pour chaque ligne
!!.
Il faut faire deux requêtes :

select a.no_client,sum(b.poids)
from #table_a a
left outer join #table_b b on b.no_commande=a.no_commande
group by a.no_client

et

select a.no_client,sum(c.montant)
from #table_a a
left outer join #table_c c on c.no_commande=a.no_commande
group by a.no_client

Cordialement




--
Frédéric BROUARD, MVP SQL Server, expert bases de données et langage SQL
Le site sur le langage SQL et les SGBDR : http://sqlpro.developpez.com
Audit, conseil, expertise, formation, modélisation, tuning, optimisation
********************* http://www.datasapiens.com ***********************


Avatar
Fred BROUARD
Postez le DDL de vos tables ainsi qu'un jeu d'essais sous forme d'INSERT.

A +

Ralf Meuser a écrit :
Salut


Merci pour votre réponse. Deux requête ça marche mais demande un gros
travail de regroupement. J'ai bien aimé la seconde solution mais elle ne
passe pas pour moi.
Je utilise MS-SQL 2000
Ça doit marcher la aussi?

Ralf


"Fred BROUARD" a écrit dans le message de news:

Et en une seule requête :

select a.no_client, sum(b.poids)
(select sum(c.montant)
from #table_a aa
left outer join #table_c c
on c.no_commandeª.no_commande
WHERE aa.no_commande = a.no_commande
group by aa.no_client)
from #table_a a
left outer join #table_b b
on b.no_commande=a.no_commande
group by a.no_client


A +


Robert Plagnard a écrit :
Bonjour,

Les requêtes SQL avec des jointures restent des produits cartésien
d'ensemble. Dans le cas présenté le montant est compté plusieurs fois. Il
faut raisonner en terme de triplet (a, b, c) générés. En fait à chaque
fois qu'il y a correspondance entre A et B (et entre A et C) on compte C
dans le total, donc on compte le total de la commande pour chaque ligne
!!.
Il faut faire deux requêtes :

select a.no_client,sum(b.poids)
from #table_a a
left outer join #table_b b on b.no_commande=a.no_commande
group by a.no_client

et

select a.no_client,sum(c.montant)
from #table_a a
left outer join #table_c c on c.no_commande=a.no_commande
group by a.no_client

Cordialement



--
Frédéric BROUARD, MVP SQL Server, expert bases de données et langage SQL
Le site sur le langage SQL et les SGBDR : http://sqlpro.developpez.com
Audit, conseil, expertise, formation, modélisation, tuning, optimisation
********************* http://www.datasapiens.com ***********************








--
Frédéric BROUARD, MVP SQL Server, expert bases de données et langage SQL
Le site sur le langage SQL et les SGBDR : http://sqlpro.developpez.com
Audit, conseil, expertise, formation, modélisation, tuning, optimisation
********************* http://www.datasapiens.com ***********************
Avatar
Robert Plagnard
Joli,
Faudra que j'essaye.
A+
--
Robert Plagnard
Communauté Francophone des Professionnels FoxPro
Pour un développement durable...
http://www.atoutfox.net



"Fred BROUARD" a écrit :

Et en une seule requête :

select a.no_client, sum(b.poids)
(select sum(c.montant)
from #table_a aa
left outer join #table_c c
on c.no_commandeª.no_commande
WHERE aa.no_commande = a.no_commande
group by aa.no_client)
from #table_a a
left outer join #table_b b
on b.no_commande=a.no_commande
group by a.no_client


A +


Robert Plagnard a écrit :
> Bonjour,
>
> Les requêtes SQL avec des jointures restent des produits cartésien
> d'ensemble. Dans le cas présenté le montant est compté plusieurs fois. Il
> faut raisonner en terme de triplet (a, b, c) générés. En fait à chaque fois
> qu'il y a correspondance entre A et B (et entre A et C) on compte C dans le
> total, donc on compte le total de la commande pour chaque ligne !!.
> Il faut faire deux requêtes :
>
> select a.no_client,sum(b.poids)
> from #table_a a
> left outer join #table_b b on b.no_commande=a.no_commande
> group by a.no_client
>
> et
>
> select a.no_client,sum(c.montant)
> from #table_a a
> left outer join #table_c c on c.no_commande=a.no_commande
> group by a.no_client
>
> Cordialement


--
Frédéric BROUARD, MVP SQL Server, expert bases de données et langage SQL
Le site sur le langage SQL et les SGBDR : http://sqlpro.developpez.com
Audit, conseil, expertise, formation, modélisation, tuning, optimisation
********************* http://www.datasapiens.com ***********************



Avatar
Patrice
Ne *jamais* poster à propos d'une erreur sans indiquer le texte de cette
erreur. Merci d'avance...

"Ralf Meuser" a écrit dans le message de news:
46725c59$0$9700$
Salut


Merci pour votre réponse. Deux requête ça marche mais demande un gros
travail de regroupement. J'ai bien aimé la seconde solution mais elle ne
passe pas pour moi.
Je utilise MS-SQL 2000
Ça doit marcher la aussi?

Ralf


"Fred BROUARD" a écrit dans le message de
news:
Et en une seule requête :

select a.no_client, sum(b.poids)
(select sum(c.montant)
from #table_a aa
left outer join #table_c c
on c.no_commandeª.no_commande
WHERE aa.no_commande = a.no_commande
group by aa.no_client)
from #table_a a
left outer join #table_b b
on b.no_commande=a.no_commande
group by a.no_client


A +


Robert Plagnard a écrit :
Bonjour,

Les requêtes SQL avec des jointures restent des produits cartésien
d'ensemble. Dans le cas présenté le montant est compté plusieurs fois.
Il faut raisonner en terme de triplet (a, b, c) générés. En fait à
chaque fois qu'il y a correspondance entre A et B (et entre A et C) on
compte C dans le total, donc on compte le total de la commande pour
chaque ligne !!.
Il faut faire deux requêtes :

select a.no_client,sum(b.poids)
from #table_a a
left outer join #table_b b on b.no_commande=a.no_commande
group by a.no_client

et

select a.no_client,sum(c.montant)
from #table_a a
left outer join #table_c c on c.no_commande=a.no_commande
group by a.no_client

Cordialement




--
Frédéric BROUARD, MVP SQL Server, expert bases de données et langage SQL
Le site sur le langage SQL et les SGBDR : http://sqlpro.developpez.com
Audit, conseil, expertise, formation, modélisation, tuning, optimisation
********************* http://www.datasapiens.com ***********************