Nous souhaitons obtenir le nombre total d'entrées depuis 1979 classé par année mais en excluant les films totalisant à eux seuls plus de 10 millions d'entrées.
Pour cela, nous devons cumuler le nombre d'entrée de chaque film depuis 1979, enlever les entrées de ceux qui ont fait plus de 10 000 000 entrées puis trier le résultat par année.
Voici le code 4D correspondant à cette requête :
ARRAY INTEGER(aMovieYear;0)
ARRAY LONGINT(aSoldTickets;0)
C_LONGINT($MovieYear;$vCrtMovieYear;$i;$MinSoldTickets;$vInd)
REDUCE SELECTION([MOVIES];0)
$MovieYear:=1979
$MinSoldTickets:=10000000
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
If(aSoldTickets{$vInd}<$MinSoldTickets)
$vInd:=$vInd+1
INSERT IN ARRAY(aMovieYear;$vInd;1)
aMovieYear{$vInd}:=$vCrtMovieYear
INSERT IN ARRAY(aSoldTickets;$vInd;1)
Else
aSoldTickets{$vInd}:=0
End if
End if
aSoldTickets{$vInd}:=aSoldTickets{$vInd}+[MOVIES]Sold_Tickets
NEXT RECORD([MOVIES])
End for
If(aSoldTickets{$vInd}>=$MinSoldTickets)
DELETE FROM ARRAY(aSoldTickets;$vInd;1)
DELETE FROM ARRAY(aMovieYear;$vInd;1)
End if
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;$MinSoldTickets)
$MovieYear:=1979
$MinSoldTickets:=10000000
Begin SQL
SELECT Year_of_Movie, SUM(Sold_Tickets)
FROM MOVIES
WHERE Year_of_Movie >= :$MovieYear
GROUP BY Year_of_Movie
HAVING SUM(Sold_Tickets) < :$MinSoldTickets
ORDER BY 1
INTO :aMovieYear, :aSoldTickets;
End SQL
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 INTEGER(aMovieYear;0)
ARRAY LONGINT(aSoldTickets;0)
C_LONGINT($MovieYear;$MinSoldTickets)
$MovieYear:=1979
$MinSoldTickets:=10000000
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+" HAVING SUM(Sold_Tickets) < :$MinSoldTickets"
$tQueryTxt:=$tQueryTxt+" ORDER BY 1"
SQL EXECUTE($tQueryTxt;aMovieYear;aSoldTickets)
SQL LOAD RECORD(SQL all records)
SQL LOGOUT
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 :
C_TEXT($tQueryTxt)
ARRAY INTEGER(aMovieYear;0)
ARRAY LONGINT(aSoldTickets;0)
C_LONGINT($MovieYear;$MinSoldTickets;$vCrtMovieYear;$vInd;$i)
REDUCE SELECTION([MOVIES];0)
$MovieYear:=1979
$MinSoldTickets:=10000000
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
If(aSoldTickets{$vInd}<$MinSoldTickets)
$vInd:=$vInd+1
INSERT IN ARRAY(aMovieYear;$vInd;1)
aMovieYear{$vInd}:=$vCrtMovieYear
INSERT IN ARRAY(aSoldTickets;$vInd;1)
Else
aSoldTickets{$vInd}:=0
End if
End if
aSoldTickets{$vInd}:=aSoldTickets{$vInd}+[MOVIES]Sold_Tickets
NEXT RECORD([MOVIES])
End for
If(aSoldTickets{$vInd}>=$MinSoldTickets)
DELETE FROM ARRAY(aSoldTickets;$vInd;1)
DELETE FROM ARRAY(aMovieYear;$vInd;1)
End if
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 INTEGER(aMovieYear;0)
ARRAY LONGINT(aSoldTickets;0)
C_LONGINT($MovieYear;$MinSoldTickets)
$MovieYear:=1979
$MinSoldTickets:=10000000
$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+" HAVING SUM(Sold_Tickets) < :$MinSoldTickets"
$tQueryTxt:=$tQueryTxt+" ORDER BY 1"
$tQueryTxt:=$tQueryTxt+" INTO :aMovieYear, :aSoldTickets;"
Begin SQL
EXECUTE IMMEDIATE :$tQueryTxt;
End SQL
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 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 HAVING.