Nous souhaitons à présent obtenir une statistique par rapport aux nombres d'entrées : quels sont les films ayant réalisé plus d'entrées que la moyenne de tous les films. Pour exécuter cette requête en SQL, nous allons utiliser une requête incluse dans une requête, autrement dit une sous-requête.
Voici le code 4D correspondant à cette requête :
ARRAY LONGINT(aSoldTickets;0)
ARRAY TEXT(aTitles;0)
C_LONGINT($i;$vInd;$vAvgSoldTickets)
$vInd:=0
ALL RECORDS([MOVIES])
$vAvgSoldTickets:=Average([MOVIES]Sold_Tickets)
For($i;1;Records in selection([MOVIES]))
If([MOVIES]Sold_Tickets>$vAvgSoldTickets)
$vInd:=$vInd+1
INSERT IN ARRAY(aTitles;$vInd;1)
aTitles{$vInd}:=[MOVIES]Title
INSERT IN ARRAY(aSoldTickets;$vInd;1)
aSoldTickets{$vInd}:=[MOVIES]Sold_Tickets
End if
NEXT RECORD([MOVIES])
End for
ARRAY INTEGER(aMovieYear;Taille tableau(aTitles))
ARRAY TEXT(aDirectors;Taille tableau(aTitles))
ARRAY TEXT(aMedias;Taille tableau(aTitles))
ARRAY LONGINT(aNrActors;Taille tableau(aTitles))
SORT ARRAY(aTitles;aDirectors;aMovieYear;aMedias;aSoldTickets;aNrActors;>)
- En utilisant du code SQL :
ARRAY LONGINT(aSoldTickets;0)
ARRAY TEXT(aTitles;0)
Begin SQL
SELECT Title, Sold_Tickets
FROM MOVIES
WHERE Sold_Tickets > (SELECT AVG(Sold_Tickets) FROM MOVIES)
ORDER BY 1
INTO :aTitles, :aSoldTickets;
End SQL
ARRAY INTEGER(aMovieYear;Taille tableau(aTitles))
ARRAY TEXT(aDirectors;Taille tableau(aTitles))
ARRAY TEXT(aMedias;Taille tableau(aTitles))
ARRAY LONGINT(aNrActors;Taille tableau(aTitles))
SORT ARRAY(aTitles;aDirectors;aMovieYear;aMedias;aSoldTickets;aNrActors;>)
- En utilisant les commandes SQL génériques :
ARRAY LONGINT(aSoldTickets;0)
ARRAY TEXT(aTitles;0)
C_TEXT($tQueryTxt)
SQL LOGIN(SQL_INTERNAL;"";"")
$tQueryTxt:=""
$tQueryTxt:=$tQueryTxt+"SELECT Title, Sold_Tickets"
$tQueryTxt:=$tQueryTxt+" FROM MOVIES"
$tQueryTxt:=$tQueryTxt+" WHERE Sold_Tickets > (SELECT AVG(Sold_Tickets) FROM MOVIES)"
$tQueryTxt:=$tQueryTxt+" ORDER BY 1"
SQL EXECUTE($tQueryTxt;aTitles;aSoldTickets)
SQL LOAD RECORD(SQL all records)
SQL LOGOUT
ARRAY INTEGER(aMovieYear;Taille tableau(aTitles))
ARRAY TEXT(aDirectors;Taille tableau(aTitles))
ARRAY TEXT(aMedias;Taille tableau(aTitles))
ARRAY LONGINT(aNrActors;Taille tableau(aTitles))
SORT ARRAY(aTitles;aDirectors;aMovieYear;aMedias;aSoldTickets;aNrActors;>)
- En utilisant la commande CHERCHER PAR SQL :
ARRAY LONGINT(aSoldTickets;0)
ARRAY TEXT(aTitles;0)
QUERY BY SQL([MOVIES];"Sold_Tickets > (SELECT AVG(Sold_Tickets) FROM MOVIES)")
ORDER BY([MOVIES];[MOVIES]Title;>)
SELECTION TO ARRAY([MOVIES]Title;aTitles;[MOVIES]Sold_Tickets;aSoldTickets)
ARRAY INTEGER(aMovieYear;Taille tableau(aTitles))
ARRAY TEXT(aDirectors;Taille tableau(aTitles))
ARRAY TEXT(aMedias;Taille tableau(aTitles))
ARRAY LONGINT(aNrActors;Taille tableau(aTitles))
SORT ARRAY(aTitles;aDirectors;aMovieYear;aMedias;aSoldTickets;aNrActors;>)
- En utilisant la commande SQL dynamique EXECUTE IMMEDIATE :
ARRAY LONGINT(aSoldTickets;0)
ARRAY TEXT(aTitles;0)
C_TEXT($tQueryTxt)
$tQueryTxt:=""
$tQueryTxt:=$tQueryTxt+"SELECT Title, Sold_Tickets"
$tQueryTxt:=$tQueryTxt+" FROM MOVIES"
$tQueryTxt:=$tQueryTxt+" WHERE Sold_Tickets > (SELECT AVG(Sold_Tickets) FROM MOVIES)"
$tQueryTxt:=$tQueryTxt+" ORDER BY 1"
$tQueryTxt:=$tQueryTxt+" INTO :aTitles, :aSoldTickets"
Begin SQL
EXECUTE IMMEDIATE :$tQueryTxt;
End SQL
ARRAY INTEGER(aMovieYear;Taille tableau(aTitles))
ARRAY TEXT(aDirectors;Taille tableau(aTitles))
ARRAY TEXT(aMedias;Taille tableau(aTitles))
ARRAY LONGINT(aNrActors;Taille tableau(aTitles))
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 Sous-requêtes.