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

Problème avec GROUP BY

11 réponses
Avatar
Utilisateur1
Bonjour

Soit une vue qui genère une synthèse de chaque commande en fournissant les
champs suivants :
- commande_id
- client_id
- date_livraison
- soustotal_marchandise_horsremise_htva
- remise
- soustotal_marchandise_htva
- frais_de_port_htva

Je cherche à obtenir le total du sous-total htva des marchandises, la
moyenne de la remise et le total des frais de port, pour chaque livraison.
S'il y a plusieurs commandes pour un même client le même jour, on considère
comme étant une seule livraison.

J'avai fait ceci sur postgres :

SELECT date_livraison,
client_id,
COUNT(date_livraison) AS nombre_livraison,
SUM(soustotal_marchandise_htva) AS total_marchandises_htva,
AVG(remise) AS moyenne_remises,
SUM(frais_de_port_htva) AS total_frais_de_port_htva
FROM vue_commande_synthese
GROUP BY date_livraison, client_id;

Mais ca m'affiche quand même 2 livraisons pour deux commandes à la même date
pour le même client...

Ou est l'erreur dans ma logique ?

Merci d'avance !

1 réponse

1 2
Avatar
Fred Brouard - SQLpro
Bonjour,

votre requête fonctionne parfaitement sous MS SQL Server et donne le
résultat suivant :
com._id client_id date_liv remise soust frais
------- ----------- ----------- ------- ------ ------
1 1000 2008-05-12 .00 150.00 12.50
2 1005 2008-05-12 10.00 660.00 12.50
3 1000 2008-05-17 .00 77.50 12.50
4 1005 2008-05-14 .00 125.75 12.50
5 1005 2008-05-12 .00 339.00 12.50


Est-ce bien le résultat que vous voulez ?

Sinon, si PG vous donne une ligne supplémentaire, c'est un bug !

A +

--
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.sqlspot.com *************************



Utilisateur1 a écrit :
Bonjour

Voici un jeu de test pour mon probleme :

CREATE TEMPORARY TABLE vue_commande_synthese
(
commande_id integer PRIMARY KEY NOT NULL,
client_id INTEGER NOT NULL,
date_livraison DATE,
remise NUMERIC(5,2) NOT NULL,
soustotal_marchandise_htva NUMERIC(9,2),
frais_de_port_htva NUMERIC(9,2)
);

INSERT INTO vue_commande_synthese (commande_id, client_id, date_livraison,
remise, soustotal_marchandise_htva, frais_de_port_htva)
VALUES (1, 1000, TO_DATE('12/05/2008', 'DD/MM/YYYY'), 0, 150, 12.50);

INSERT INTO vue_commande_synthese (commande_id, client_id, date_livraison,
remise, soustotal_marchandise_htva, frais_de_port_htva)
VALUES (2, 1005, TO_DATE('12/05/2008', 'DD/MM/YYYY'), 10, 660, 12.50);

INSERT INTO vue_commande_synthese (commande_id, client_id, date_livraison,
remise, soustotal_marchandise_htva, frais_de_port_htva)
VALUES (3, 1000, TO_DATE('17/05/2008', 'DD/MM/YYYY'), 0, 77.50, 12.50);

INSERT INTO vue_commande_synthese (commande_id, client_id, date_livraison,
remise, soustotal_marchandise_htva, frais_de_port_htva)
VALUES (4, 1005, TO_DATE('14/05/2008', 'DD/MM/YYYY'), 0, 125.75, 12.50);

INSERT INTO vue_commande_synthese (commande_id, client_id, date_livraison,
remise, soustotal_marchandise_htva, frais_de_port_htva)
VALUES (5, 1005, TO_DATE('12/05/2008', 'DD/MM/YYYY'), 0, 339, 12.50);

Et voici ma requete qui ne fonctionne pas (retourne 2 livraisons au lieu de
1 pour le client 1005 au 12/05/2008)

SELECT date_livraison,
client_id,
COUNT(date_livraison) AS nombre_livraison,
SUM(soustotal_marchandise_htva) AS total_marchandises_htva,
AVG(remise) AS moyenne_remises,
SUM(frais_de_port_htva) AS total_frais_de_port_htva
FROM vue_commande_synthese
GROUP BY date_livraison, client_id
ORDER BY date_livraison ASC;

date_livraison | client_id | nombre_livraison | total_marchandises_htva |
moyenne_remises | total_frais_de_port_htva
----------------+-----------+------------------+-------------------------+------------------------+--------------------------
2008-05-12 | 1005 | 2 | 999.00 |
5.0000000000000000 | 25.00
2008-05-12 | 1000 | 1 | 150.00 |
0.00000000000000000000 | 12.50
2008-05-14 | 1005 | 1 | 125.75 |
0.00000000000000000000 | 12.50
2008-05-17 | 1000 | 1 | 77.50 |
0.00000000000000000000 | 12.50
(4 lignes)

Je ne comprend plus rien !

"Ph. B." a écrit dans le message
de news: 48305443$0$28782$
Bonjour,

La colonne "date_livraison" ne serait-elle pas de type datetime avec la
partie heure différente?

Philippe.




1 2