4D v16

Utiliser la clause GROUP BY

Accueil

 
4D v16
Utiliser la clause GROUP BY

Utiliser la clause GROUP BY  


 

 

Nous souhaitons obtenir le nombre annuel total d'entrées depuis 1979. Le résultat devra être trié par année.
Voici le code 4D correspondant à cette requête :

 ARRAY INTEGER(aMovieYear;0)
 ARRAY LONGINT(aSoldTickets;0)
 C_LONGINT($MovieYear;$vCrtMovieYear;$i)
 
 REDUCE SELECTION([MOVIES];0)
 $MovieYear:=1979
 QUERY([MOVIES];[MOVIES]Year_of_Movie>=$MovieYear)
 ORDER BY([MOVIES];[MOVIES]Year_of_Movie;>)
 $vCrtMovieYear:=0
 $vInd:=Size of array(aMovieYear)
 For($i;1;Records in selection([MOVIES]))
    If([MOVIES]Year_of_Movie#$vCrtMovieYear)
       $vCrtMovieYear:=[MOVIES]Year_of_Movie
       $vInd:=$vInd+1
       INSERT IN ARRAY(aMovieYear;$vInd;1)
       aMovieYear{$vInd}:=$vCrtMovieYear
       INSERT IN ARRAY(aSoldTickets;$vInd;1)
    End if
    aSoldTickets{$vInd}:=aSoldTickets{$vInd}+[MOVIES]Sold_Tickets
    NEXT RECORD([MOVIES])
 End for
  ` Initialisation du reste des colonnes de list box pour visualiser l'information
 ARRAY TEXT(aTitles;Taille tableau(aMovieYear))
 ARRAY TEXT(aDirectors;Taille tableau(aMovieYear))
 ARRAY TEXT(aMedias;Taille tableau(aMovieYear))
 ARRAY LONGINT(aNrActors;Taille tableau(aMovieYear))

  • En utilisant du code SQL :

     ARRAY INTEGER(aMovieYear;0)
     ARRAY LONGINT(aSoldTickets;0)
     C_LONGINT($MovieYear)
     
     REDUCE SELECTION([MOVIES];0)
     $MovieYear:=1979
     Begin SQL
        SELECT Year_of_Movie, SUM(Sold_Tickets)
        FROM MOVIES
        WHERE Year_of_Movie >= :$MovieYear
        GROUP BY Year_of_Movie
        ORDER BY 1
        INTO :aMovieYear,  :aSoldTickets;
     End SQL
      ` Initialisation du reste des colonnes de list box pour visualiser l'information
     ARRAY TEXT(aTitles;Taille tableau(aMovieYear))
     ARRAY TEXT(aDirectors;Taille tableau(aMovieYear))
     ARRAY TEXT(aMedias;Taille tableau(aMovieYear))
     ARRAY LONGINT(aNrActors;Taille tableau(aMovieYear))
  • En utilisant les commandes SQL génériques :

     C_TEXT($tQueryTxt)
     ARRAY LONGINT(aSoldTickets;0)
     ARRAY INTEGER(aMovieYear;0)
     C_LONGINT($MovieYear)
     
     REDUCE SELECTION([MOVIES];0)
     $MovieYear:=1979
     SQL LOGIN(SQL_INTERNAL;"";"")
     $tQueryTxt:=""
     $tQueryTxt:=$tQueryTxt+"SELECT Year_of_Movie, SUM(Sold_Tickets)"
     $tQueryTxt:=$tQueryTxt+" FROM MOVIES"
     $tQueryTxt:=$tQueryTxt+" WHERE Year_of_Movie >= :$MovieYear"
     $tQueryTxt:=$tQueryTxt+" GROUP BY Year_of_Movie"
     $tQueryTxt:=$tQueryTxt+" ORDER BY 1"
     SQL EXECUTE($tQueryTxt;aMovieYear;aSoldTickets)
     SQL LOAD RECORD(SQL all records)
     SQL LOGOUT
      ` Initialisation du reste des colonnes de list box pour visualiser l'information
     ARRAY TEXT(aTitles;Taille tableau(aMovieYear))
     ARRAY TEXT(aDirectors;Taille tableau(aMovieYear))
     ARRAY TEXT(aMedias;Taille tableau(aMovieYear))
     ARRAY LONGINT(aNrActors;Taille tableau(aMovieYear))
  • En utilisant la commande CHERCHER PAR SQL :

     ARRAY LONGINT(aSoldTickets;0)
     ARRAY INTEGER(aMovieYear;0)
     C_LONGINT($MovieYear)
     
     REDUCE SELECTION([MOVIES];0)
     $MovieYear:=1979
     QUERY BY SQL([MOVIES];"Year_of_Movie >= :$MovieYear")
     ORDER BY([MOVIES];[MOVIES]Year_of_Movie;>)
     $vCrtMovieYear:=0
     $vInd:=Size of array(aMovieYear)
     For($i;1;Records in selection([MOVIES]))
        If([MOVIES]Year_of_Movie#$vCrtMovieYear)
           $vCrtMovieYear:=[MOVIES]Year_of_Movie
           $vInd:=$vInd+1
           INSERT IN ARRAY(aMovieYear;$vInd;1)
           aMovieYear{$vInd}:=$vCrtMovieYear
           INSERT IN ARRAY(aSoldTickets;$vInd;1)
        End if
        aSoldTickets{$vInd}:=aSoldTickets{$vInd}+[MOVIES]Sold_Tickets
        NEXT RECORD([MOVIES])
     End for
      ` Initialisation du reste des colonnes de list box pour visualiser l'information
     ARRAY TEXT(aTitles;Taille tableau(aMovieYear))
     ARRAY TEXT(aDirectors;Taille tableau(aMovieYear))
     ARRAY TEXT(aMedias;Taille tableau(aMovieYear))
     ARRAY LONGINT(aNrActors;Taille tableau(aMovieYear))
  • En utilisant la commande SQL dynamique EXECUTE IMMEDIATE :

     C_TEXT($tQueryTxt)
     ARRAY LONGINT(aSoldTickets;0)
     ARRAY INTEGER(aMovieYear;0)
     C_LONGINT($MovieYear)
     
     $MovieYear:=1979
     $tQueryTxt:=""
     $tQueryTxt:=$tQueryTxt+"SELECT Year_of_Movie, SUM(Sold_Tickets)"
     $tQueryTxt:=$tQueryTxt+" FROM MOVIES"
     $tQueryTxt:=$tQueryTxt+" WHERE Year_of_Movie >= :$MovieYear"
     $tQueryTxt:=$tQueryTxt+" GROUP BY Year_of_Movie"
     $tQueryTxt:=$tQueryTxt+" ORDER BY 1"
     $tQueryTxt:=$tQueryTxt+" INTO :aMovieYear, :aSoldTickets;"
     Begin SQL
        EXECUTE IMMEDIATE :$tQueryTxt;
     End SQL
      ` Initialisation du reste des colonnes de list box pour visualiser l'information
     ARRAY TEXT(aTitles;Taille tableau(aMovieYear))
     ARRAY TEXT(aDirectors;Taille tableau(aMovieYear))
     ARRAY TEXT(aMedias;Taille tableau(aMovieYear))
     ARRAY LONGINT(aNrActors;Taille tableau(aMovieYear))

Pour tester tous ces exemples, lancez la base "4D SQL Code Samples" et affichez la boîte de dialogue principale. Choisissez le mode d'interrogation du moteur de 4D et cliquez sur le bouton Clause GROUP BY.

 
PROPRIÉTÉS 

Produit : 4D
Thème : Prise en main

 
HISTORIQUE 

 
UTILISATION DE L'ARTICLE

4D - Référence SQL ( 4D v16)