Soucis avec mon GROUP BY

Résolu
Bonjour à tous !

Pouvez-vous m'aider à grouper les éléments ci-dessous svp ?
J'y arrive pas même quand je met tout les éléments dans le GROUP BY
Je voudrais grouper
libelle_fr + Contenance_FR
la somme de la Quantite, du Prix_Unitaire, du Prix_Total et d Total
SELECT
F.Reference
, F.Libelle_TAG
, SUM(F.Quantite)
, SUM(F.Prix_Unitaire)
, SUM(F.Prix_Total)
, F.Remise
, SUM(F.Total)

, R.Reference
, R.Coupon
, R.Etat_Commande

, P.Libelle_FR
, P.Libelle_TAG
, P.Contenance_FR

FROM tb_shop_FA F

INNER JOIN tb_shop_recap R ON R.Reference = F.Reference
LEFT JOIN tb_produits P ON P.Libelle_TAG = F.Libelle_TAG

WHERE
R.Coupon <> '0'
AND R.Etat_Commande = 'PRÉPARATION'

GROUP BY


Merci de votre aide.

Configuration: Windows / Chrome 79.0.3945.130

5 réponses

  • Votes
  • Date
  • Date décroissante
  1. J'ai essayé et voilà le résultat avec :
    GROUP BY 

    F.Reference
    , F.Libelle_TAG
    , F.Quantite
    , F.Prix_Unitaire
    , F.Prix_Total
    , F.Remise
    , F.Total

    , R.Reference
    , R.Coupon
    , R.Etat_Commande

    , P.Libelle_FR
    , P.Libelle_TAG
    , P.Contenance_FR



    Et le résultat avec :
    	F.Reference
    , F.Libelle_TAG
    , SUM(F.Quantite)
    , SUM(F.Prix_Unitaire)
    , SUM(F.Prix_Total)
    , F.Remise
    , SUM(F.Total)

    , R.Reference
    , R.Coupon
    , R.Etat_Commande

    , P.Libelle_FR
    , P.Libelle_TAG
    , P.Contenance_FR

    SQLSTATE[42000]: Syntax error or access violation: 1056 Can't group on 'SUM(F.Quantite)'
    1. Modérateur
      dans le group by il ne doit pas y avoir les champs sur lesquels tu fais les sum() , on groupe sur les autres
  2. Donc première option, erreur sur la récupération des données...

    GROUP BY

    F.Reference
    , F.Libelle_TAG
    , F.Quantite
    , F.Prix_Unitaire
    , F.Prix_Total
    , F.Remise
    , F.Total

    , R.Reference
    , R.Coupon
    , R.Etat_Commande

    , P.Libelle_FR
    , P.Libelle_TAG
    , P.Contenance_FR



    1. Modérateur
      GROUP BY
      
        F.Reference
      , F.Libelle_TAG
      , F.Remise
      
      , R.Reference
      , R.Coupon
      , R.Etat_Commande
      
      , P.Libelle_FR
      , P.Libelle_TAG
      , P.Contenance_FR


      par ailleurs, fonctionnellement parlant, on peut avoir des doutes sur la légitimité d'un SUM(F.Prix_Unitaire)
  3. En effet, tu as raison pour le Prix_Unitaire.

    J'ai essayé ceci et j'ai toujours des erreurs, peut-être du php...
    Voici l'intégralité de mon code :
    <tbody style="border:1px solid #000000;">
    <?php try {$stat = $pdo->query("
    SELECT
    F.Reference
    , F.Libelle_TAG
    , SUM(F.Quantite)
    , F.Prix_Unitaire
    , SUM(F.Prix_Total)
    , F.Remise
    , SUM(F.Total)

    , R.Reference
    , R.Coupon
    , R.Etat_Commande

    , P.Libelle_FR
    , P.Libelle_TAG
    , P.Contenance_FR

    FROM tb_shop_FA F

    INNER JOIN tb_shop_recap R ON R.Reference = F.Reference
    LEFT JOIN tb_produits P ON P.Libelle_TAG = F.Libelle_TAG

    WHERE
    R.Coupon <> '0'
    AND R.Etat_Commande = 'PRÉPARATION'

    GROUP BY

    F.Reference
    , F.Libelle_TAG
    , F.Remise
    , F.Prix_Unitaire

    , R.Reference
    , R.Coupon
    , R.Etat_Commande

    , P.Libelle_FR
    , P.Libelle_TAG
    , P.Contenance_FR

    ORDER BY P.Libelle_FR
    ");

    while($data = $stat->fetch(PDO::FETCH_ASSOC)){
    ?>

    <tr>
    <td><?php echo $data["Libelle_FR"]." - ".$data["Contenance_FR"]; ?></td>
    <td><?php echo $data["Quantite"]; ?></td>
    <td><?php echo number_format($data["Prix_Unitaire"], 2, ',', ' '); ?></td>
    <td><?php echo number_format($data["Prix_Total"], 2, ',', ' '); ?></td>
    <td><?php echo "-".$data["Remise"]."%"; ?></td>
    <td><?php echo number_format($data["Total"], 2, ',', ' '); ?></td>
    </tr>

    <?php }}catch(PDOException $e){echo "<div class='alert alert-danger'>".$e->getMessage()."</div>";} ?>

    </tbody>


    Et les erreurs rencontrés :
    Alcool éthylique 90° - Flacon spray de 250ml	
    Notice: Undefined index: Quantite in /home/sivop/www/test-menu.php on line 97
    609,17
    Notice: Undefined index: Prix_Total in /home/sivop/www/test-menu.php on line 99
    0,00 -15%
    Notice: Undefined index: Total in /home/sivop/www/test-menu.php on line 101
    0,00
    1. @jee peeMalheureusement ça me renvoie A et B

      <?php
      $Quantite = !empty($_POST['Quantite']) ? $_POST['Quantite'] : "A";
      $Prix_Total = !empty($data['Prix_Total']) ? $data['Prix_Total'] : "B";
      ?>

      Je suis perdu :'(
    2. Modérateur
      @Sinistrusle soucis c'est probablement le nom la colonne qui est
      SUM(F.Quantite)


      il faudrait la renommer dans le select SUM(F.Quantite) AS Quantite, pareil pour les 2 autres
    3. @jee peeOui tu as bien vu ! en effet !


      Reste plus qu'à grouper malgré le groupage... le groupage ne fonctionne pas :\
      C'est peut-être dû la F.Reference, R.Reference ? Ils ne sont pas identiques dans tout les cas et ne peuvent être groupés je penses... ?
    4. Modérateur
      @Sinistrustu ne devrais avoir en select et group by que les champs affichés dans ton resultat ci-dessus, même je virerais la remise.et le PU
  4. Ca fonctionne ENFIN !!!

    Donc si je comprends bien la fonction du GROUP BY, on ne groupe QUE ce qui est identique dans mon cas présent, sinon on groupe les éléments a afficher dans le tableau
    Genre ce qui est dans le SELECT doit être présent dans le GROUP BY sauf les calculs du genre SUM et COUNT... j'espère avoir bien compris !

    Merci infiniment de ton aide Jee Pee !