| 1 |
<HTML> |
| 2 |
|
| 3 |
<HEAD> |
| 4 |
<TITLE>AQL - MovieDB Example queries</TITLE> |
| 5 |
<LINK REL="stylesheet" TYPE="text/css" HREF="../aql.css" TITLE="Style sheet for AQL docs"> |
| 6 |
</HEAD> |
| 7 |
<BODY> |
| 8 |
|
| 9 |
<H2>AQL - MovieDB Example queries</H2> |
| 10 |
|
| 11 |
|
| 12 |
<H3>Simple listing of objects</H3> |
| 13 |
|
| 14 |
<DL> |
| 15 |
<DT><H4>1. list all movies</H4> |
| 16 |
<DD><CODE>select all from class Movie</CODE> |
| 17 |
<DD><CODE>select all class Movie</CODE> (the same, just shorter) <BR> |
| 18 |
<DD><P>List all objects in the class <I>movie</I>. |
| 19 |
<BR> |
| 20 |
Note the system is not case sensitive when referring to |
| 21 |
database related terms, such as class names or tags. |
| 22 |
<BR><A HREF="result1.txt">results</A> |
| 23 |
</P> |
| 24 |
|
| 25 |
|
| 26 |
<DT><H4>2. list all persons</H4> |
| 27 |
<DD><CODE>select p from p in class Person</CODE> |
| 28 |
<DD><P>A locator-variable <TT>p</TT> is defined to loop over the key-values in class <I>Person</I>. |
| 29 |
The query works exactly like the (1), but this query can be extended easily, |
| 30 |
as <TT>p</TT> can be referred to for tag-dereferencing, column-wise sorting etc.. |
| 31 |
<BR><A HREF="result2.txt">results</A> |
| 32 |
</P> |
| 33 |
</DL> |
| 34 |
|
| 35 |
<HR> |
| 36 |
|
| 37 |
<H3>Tag dereferencing to access object attributes</H3> |
| 38 |
|
| 39 |
<DL> |
| 40 |
<DT><H4>3. list all movies and their directors</H4> |
| 41 |
<DD><CODE>select m, m->Director from m in class Movie</CODE> |
| 42 |
<DD><P>The <U>from-clause</U> defines an iterator variable <TT>m</TT> that loops over all key-values of class <I>movie</I>. |
| 43 |
<BR> |
| 44 |
The <U>select-clause</U> defines two <U>select-fields</U> - <I>m</I> is just the object value (shown as it's name) |
| 45 |
and <TT>m->Director</TT> is the object value, which is stored with the tag <TT>Directory</TT> |
| 46 |
in object in the class <I>Movie</I>. |
| 47 |
<BR><A HREF="result3.txt">results</A> |
| 48 |
</P> |
| 49 |
|
| 50 |
|
| 51 |
<DT><H4>4. list all movies and their directors and possibly their real names</H4> |
| 52 |
<DD><CODE>select m, m->Director, m->Director->Real_name from m in class Movie</CODE> |
| 53 |
<DD><P>Basically the same as (3), but the result of <TT>m->Director</TT> has been dereferenced to |
| 54 |
get the <TT>Real_name</TT> tag value for the particular object from the <I>Person</I> super-class. |
| 55 |
<BR><A HREF="result4.txt">results</A> |
| 56 |
</P> |
| 57 |
|
| 58 |
|
| 59 |
<DT><H4>5. list authors of books on which movies are based</H4> |
| 60 |
<DD><CODE>select a, a->Real_name from m in class Movie, a in m->Based_on->Author</CODE> |
| 61 |
<DD><P>The <U>from-clause</U> defines two iterators - <TT>m</TT> loops over the <I>Movie</I> class |
| 62 |
and <TT>a</TT> which takes the value of the <I>Person</I>-object, that is stored with the |
| 63 |
<TT>Based_on</TT>-object (in class <I>Book</I>) for movies that are based on novels or plays. |
| 64 |
<BR> |
| 65 |
The <U>select-clause</U> defines two result columns which report the <I>Author</I> |
| 66 |
objects for such books. The second column shows how that object can be dereferenced further |
| 67 |
to gain access to tags inside the <I>Author</I> objects. |
| 68 |
<BR> |
| 69 |
Note that is isn't important where the dereferencing is done (select or from-clause). |
| 70 |
The above query is the same as |
| 71 |
</P> |
| 72 |
|
| 73 |
<DD><CODE>select m->Based_on->Author, m->Based_on->Author->Real_name from m in class Movie</CODE> |
| 74 |
|
| 75 |
<DD><P>which performs the same without the need for an extra variable <TT>a</TT>, but duplicates some query elements. |
| 76 |
<BR><A HREF="result5.txt">results</A> |
| 77 |
</P> |
| 78 |
|
| 79 |
<DT><H4>6. list all people and all their relations</H4> |
| 80 |
<DD><CODE>select p, p->Relations[2] from p in class Person</CODE> |
| 81 |
<DD><P>This query makes use of <U>tag-positioning</U>. |
| 82 |
All values fields have a position relating to their tag. So far we've dereferenced |
| 83 |
tags to retrieve their values, e.g. <TT>m->Based_on</TT>. This notation is in fact translated |
| 84 |
to <TT>m->Based_on[1]</TT> to say that we're interested in the data element which is one to the right |
| 85 |
of the tag. A look at the <A HREF="wspec/models.wrm">models</A> shows a different arrangement |
| 86 |
for the Relations data in the <I>Person</I> class, where the real data is positioned |
| 87 |
2 levels to the right of the <TT>Relations</TT> tag. |
| 88 |
<BR> |
| 89 |
The bracketed number 2 means to skip the first level of data (some people have a <TT>Spouse</TT> |
| 90 |
or other tag in the database) and only report the value to the right of that tag. |
| 91 |
<BR><A HREF="result6.txt">results</A> |
| 92 |
</P> |
| 93 |
|
| 94 |
|
| 95 |
<DT><H4>7. list all people and their spouses</H4> |
| 96 |
<DD><CODE>select p, p->Relations[Spouse] from p in class Person</CODE> |
| 97 |
<DD><P>This query positioned the tag-value by name rather than number. |
| 98 |
The level2 tag notation at <TT>Relations</TT> is resolved only at the |
| 99 |
<TT>Spouse</TT> tag in this case. This query is equivalent to : |
| 100 |
</P> |
| 101 |
<DD><CODE>select p, p->Spouse from p in class Person</B></CODE> |
| 102 |
|
| 103 |
<P>which shows that the user does not have to specify the exact path |
| 104 |
from the root of the object. The name for a tag has to be unique within |
| 105 |
a class, so there is always only one possible path from the root |
| 106 |
to a named tag in the object. |
| 107 |
<BR><A HREF="result7.txt">results</A> |
| 108 |
</P> |
| 109 |
</DL> |
| 110 |
|
| 111 |
<HR> |
| 112 |
|
| 113 |
<H3>Conditional expressions in the where-clause</H3> |
| 114 |
|
| 115 |
<DL> |
| 116 |
<DT><H4>8. list movies that have a director with movie name and directors name</H4> |
| 117 |
<DD><CODE>select m, d from m in class Movie, d in m->Director where exists d</CODE> |
| 118 |
<DD><P>The <U>where-clause</U> puts a condition on the preceding |
| 119 |
<U>from-clause</U>. The AQL keyword <EM>exists</EM> is a boolean construct, |
| 120 |
which returns TRUE if a value exists, and FALSE if no value can be found. |
| 121 |
<BR> |
| 122 |
The result table has no blank fields in the |
| 123 |
second column as all those movies without a value for <TT>d</TT> (i.e. <TT>m->Director</TT>) |
| 124 |
have been omitted. |
| 125 |
<BR><A HREF="result8.txt">results</A> |
| 126 |
</P> |
| 127 |
|
| 128 |
|
| 129 |
<DT><H4>9. list the names of cast members in "True Romance"</H4> |
| 130 |
<DD><CODE>select m->Cast from m in class Movie where m = "True Romance"</CODE> |
| 131 |
<DD><P>The query iterates the variable <TT>m</TT> over the <I>Movie</I> class, but only |
| 132 |
activates the <U>select-clause</U> (which reports the cast of a movie) |
| 133 |
if the value of the Movie iterator is "True Romance". |
| 134 |
<BR> |
| 135 |
Although <TT>m</TT> takes object-typed value, we can compare them by their |
| 136 |
text-value (i.e. the object's name). The objects name has to be quoted for that matter. |
| 137 |
<BR> |
| 138 |
The same results could be achieved by looping over all persons, and only selecting those |
| 139 |
that have starred in "True Romance". |
| 140 |
</P> |
| 141 |
<DD><CODE>select p from p in class Person where p->Stars_in = "True Romance"</CODE> |
| 142 |
<DD><P>This shows an interesting aspect of AQL - It is possible to get the same result using totally different queries. |
| 143 |
Other ways of obtaining the same results will be shown later. |
| 144 |
<BR><A HREF="result9.txt">results</A> |
| 145 |
</P> |
| 146 |
|
| 147 |
|
| 148 |
<DT><H4>10. list the movies that have been released after 1990, i.e. in 1991 and thereafter</H4> |
| 149 |
<DD><CODE>select m, year from m in class Movie, year in m->Release[Date] where year > `1990</CODE> |
| 150 |
<DD><P>This condition compares two value of <I>DateType</I>. A date like 1990 would easily |
| 151 |
be confused with an integer, and therefore literal date-values have to be preceded |
| 152 |
with the reverse tick <TT>`</TT>. |
| 153 |
<BR> |
| 154 |
The <U>where-clause</U> also has the effect of restricting the listing of <TT>m</TT>-values |
| 155 |
to those where a release-date is stored in the database, which |
| 156 |
implies the <TT>where exists ->year</TT> condition. |
| 157 |
<BR> |
| 158 |
The iterator <TT>year</TT> could also be defined as <TT>m->Date</TT>, but the |
| 159 |
above construct was chosen for readability. See example (7). |
| 160 |
<BR><A HREF="result10.txt">results</A> |
| 161 |
</P> |
| 162 |
|
| 163 |
<DT><H4>11. list all movies for which no release year information is stored</H4> |
| 164 |
<DD><CODE>select m from m in class Movie where not exists m->Release[Date]</CODE> |
| 165 |
<DD><P>The <EM>not</EM> keyword negates the value of the <TT>exists m->Release[Date]</TT> |
| 166 |
condition. |
| 167 |
<BR><A HREF="result11.txt">results</A> |
| 168 |
</P> |
| 169 |
|
| 170 |
</DL> |
| 171 |
|
| 172 |
<HR> |
| 173 |
|
| 174 |
<H3>Date functions</H3> |
| 175 |
|
| 176 |
<DL> |
| 177 |
<DT><H4>12. list all people with their age</H4> |
| 178 |
<DD><CODE>select p, yeardiff(p->Date_of_Birth, today) from p in class Person</CODE> |
| 179 |
<DD><P>Calculations with values of type <I>Int</I> or <I>Float</I> are easily written |
| 180 |
using the standard operators +, -, / and *. For <I>DateType</I> values this |
| 181 |
is not so easy. A set of functions is provided to calculate the |
| 182 |
difference between two dates at various levels. |
| 183 |
<BR> |
| 184 |
This query extract the year portion from two dates and subtracts those |
| 185 |
to compute the age. The result of those date-functions is an integer. |
| 186 |
<BR> |
| 187 |
The AQL keyword <EM>today</EM> will be rendered to |
| 188 |
<TT>`1999-12-06</TT> |
| 189 |
(or whatever the current date is) during evaluation. |
| 190 |
The word <EM>today</EM> is therefore a valid date-literal value. |
| 191 |
<BR><A HREF="result12.txt">results</A> |
| 192 |
</P> |
| 193 |
|
| 194 |
<DT><H4>13. for all people over 55, list their name and age</H4> |
| 195 |
<DD><CODE>select p, yeardiff(p->Date_of_Birth, today) from p in class Person where yeardiff(p->Date_of_Birth, today) > 55</CODE> |
| 196 |
<DD><P>Same as (12), but restricts the list to people with ages greater than 55. <BR> |
| 197 |
Note that only values for <TT>p</TT> can be included for which the <TT>Born</TT> tag exists. See example (10), |
| 198 |
where a similar behavior occurred. |
| 199 |
<BR><A HREF="result13.txt">results</A> |
| 200 |
</P> |
| 201 |
</DL> |
| 202 |
|
| 203 |
<P>Possible date-functions are :-</P> |
| 204 |
<UL> |
| 205 |
<LI><EM>yeardiff</EM> |
| 206 |
<LI><EM>monthdiff</EM> |
| 207 |
<!-- <LI><EM>weekdiff</EM> ** not implemented yet ** --> |
| 208 |
<LI><EM>daydiff</EM> |
| 209 |
<LI><EM>hourdiff</EM> |
| 210 |
<LI><EM>mindiff</EM> |
| 211 |
<LI><EM>secdiff</EM> |
| 212 |
</UL> |
| 213 |
|
| 214 |
|
| 215 |
<P>These functions will return a sensible result of the date/time value |
| 216 |
specifies the appropriate portion that the function requests. |
| 217 |
</P> |
| 218 |
|
| 219 |
<P>No sensible result will be returned for <TT>monthdiff (m->Release[Date], `1996)</TT>. |
| 220 |
The date-function <EM>monthdiff</EM> can only produce a good result if |
| 221 |
both dates contain month-details. Some movies in fact have release dates with |
| 222 |
month information, but the date literal <TT>'1996</TT> does not. |
| 223 |
The result of evaluating <EM>monthdiff</EM> will therefore always produce NULL. |
| 224 |
</P> |
| 225 |
<P>All date-functions return an integer value, which is positive |
| 226 |
if left argument is the smaller (i.e. earlier) date. |
| 227 |
</P> |
| 228 |
|
| 229 |
|
| 230 |
<HR> |
| 231 |
|
| 232 |
<H3>Table functions</H3> |
| 233 |
|
| 234 |
<DL> |
| 235 |
<DT><H4>14. list all people and how many movies they've starred in</H4> |
| 236 |
<DD><CODE>select p, count(select p->Stars_in) from p in class Person</CODE> |
| 237 |
<DD><P>The <U>table-function</U> <EM>count</EM> acts on the sub-query <TT>select p->Stars_in</TT> |
| 238 |
and returns an integer value for all objects where the <TT>Stars_in</TT>-tag exists. It simply counts |
| 239 |
the number of times the <TT>Stars_in</TT>-tag is contained in each object <TT>p</TT>. |
| 240 |
<BR> |
| 241 |
<STRONG>Note</STRONG>, |
| 242 |
that <TT>count (p->Stars_in)</TT> is not valid. Table-functions |
| 243 |
only operate over valid table-expressions. And the minimal valid table |
| 244 |
expression to list all values of the <TT>Stars_in</TT> tag needs the |
| 245 |
keyword <EM>select</EM>. |
| 246 |
<BR><A HREF="result14.txt">results</A> |
| 247 |
</P> |
| 248 |
|
| 249 |
<DT><H4> 15. list all movies with more than 3 cast members</H4> |
| 250 |
<DD><CODE>select m from m in class Movie where count(select m->Cast) > 3</CODE> |
| 251 |
<DD><P>This time the table-function is evaluated in the <U>where-clause</U>, which |
| 252 |
compares it's integer result with the number 3. |
| 253 |
<BR><A HREF="result15.txt">results</A> |
| 254 |
</P> |
| 255 |
|
| 256 |
|
| 257 |
<DT><H4> 16. calculate the average age of all the actors</H4> |
| 258 |
<DD><CODE>select avg(select yeardiff(p->Date_of_Birth, now) from p in class Actor)</CODE> |
| 259 |
<DD><P>The <EM>avg</EM> table function acts on the return-value of another table expression. |
| 260 |
The inner <U>select-query</U> returns a list of ages, over which the average value |
| 261 |
is computed. |
| 262 |
<BR> |
| 263 |
Such complex queries should be constructed inside-out. First try out the inner |
| 264 |
query and see that it returns the desired result. Then wrap that result |
| 265 |
up in the final table-function. |
| 266 |
<BR><A HREF="result16.txt">results</A> |
| 267 |
</P> |
| 268 |
|
| 269 |
|
| 270 |
<DT><H4>17. get the latest movie release date</H4> |
| 271 |
<DD><CODE>select max(select m->Date from m in class Movie)</CODE> |
| 272 |
<DD><P>Similar to (16), <EM>max</EM> acts on a list of dates. |
| 273 |
<BR><A HREF="result17.txt">results</A> |
| 274 |
</P> |
| 275 |
</DL> |
| 276 |
|
| 277 |
<P>Possible table-functions are :-</P> |
| 278 |
<UL> |
| 279 |
<LI><EM>count</EM> (all tables) |
| 280 |
<LI><EM>min</EM> (Int, Float, DateType) <!-- ** should allow Strings ** --> |
| 281 |
<LI><EM>max</EM> (Int, Float, DateType) |
| 282 |
<LI><EM>avg</EM> (Int, Float) |
| 283 |
<LI><EM>sum </EM> (Int, Float) |
| 284 |
<LI><EM>first</EM> (all tables) |
| 285 |
<LI><EM>las</EM>t (all tables) |
| 286 |
</UL> |
| 287 |
|
| 288 |
<P>All table functions will work on either accumulated object |
| 289 |
attributes <SUP>(14),(15)</SUP> or on the table |
| 290 |
values of whole <U>select-queries</U><SUP>(16),(17)</SUP>. All |
| 291 |
such tables expression have to produce a table with only one column, |
| 292 |
and in most cases they have to be of a specific value-type for the <U>table-function</U> to work. |
| 293 |
</P> |
| 294 |
|
| 295 |
<HR> |
| 296 |
|
| 297 |
<H3>Scalar variables</H3> |
| 298 |
|
| 299 |
<DL> |
| 300 |
<DT><H4>18. how many movies are stored</H4> |
| 301 |
<DD><CODE>$x := count(select all class movie)</CODE> |
| 302 |
<DD><P>Scalar variable have a perl-like '$' syntax and can assume just one value. |
| 303 |
This value is usually the return value of a table-function expression. |
| 304 |
A variable assignment is treated as a whole query expression in itself. |
| 305 |
<BR><A HREF="result18.txt">results</A> |
| 306 |
<P> |
| 307 |
The output will be a table with one column and one row containing the single |
| 308 |
return value that the variable <TT>$x</TT> will assume. |
| 309 |
The query is therefore equivalent to |
| 310 |
<BR><CODE>select count(select all class movie)</CODE> |
| 311 |
</P> |
| 312 |
|
| 313 |
<DT><H4>19. list movies with an higher than average rating</H4> |
| 314 |
<DD><CODE>$x := avg(select m->Rating from m in class Movie); <BR> |
| 315 |
select m, r from m in class Movie, r in m->Rating where r > $x</CODE> |
| 316 |
<DD><P>The first query assigns a scalar variable to be the average rating of movie (ignoring |
| 317 |
movie for which no such value is stored). |
| 318 |
The second query then uses this value to list all movies for which the rating value is higher |
| 319 |
that that. |
| 320 |
<BR> |
| 321 |
This demonstrates how queries can be concatenated using the semi-colon ';'. Usually |
| 322 |
one or more queries will calculate variables, whereas the last query is a table expression |
| 323 |
that uses those variables to generate the output. |
| 324 |
<BR><A HREF="result19.txt">results</A> |
| 325 |
<P> |
| 326 |
The same query could be composed without a scalar variable, but the difference |
| 327 |
in execution time is quite startling : |
| 328 |
<BR> |
| 329 |
<CODE>select m, r from m in class Movie, <BR> |
| 330 |
r in m->Rating <BR> |
| 331 |
where r > avg(select m1->Rating from m1 in class Movie)</CODE> |
| 332 |
<BR> |
| 333 |
The first query would only run <EM>avg</EM> once (to get the value for <TT>$x</TT>) |
| 334 |
and finish in a time linearly related to the number of movies. |
| 335 |
The second query will execute the nested query inside the <EM>avg</EM> function in every |
| 336 |
iteration cycle of <TT>m</TT> from the outer query. |
| 337 |
The execution time is therefore quadratically related to the number of movies. |
| 338 |
<P> |
| 339 |
This is an example how queries can optimized by arranging it in a clever way. |
| 340 |
</P> |
| 341 |
|
| 342 |
</DL> |
| 343 |
|
| 344 |
<HR> |
| 345 |
|
| 346 |
<H3>Table variables</H3> |
| 347 |
|
| 348 |
<DL> |
| 349 |
<DT><H4>20. show release date of first "James Bond" film</H4> |
| 350 |
<DD><CODE>@tmp := select first(select m->Date) <BR> |
| 351 |
from m in class Movie, a in m->Cast<BR> |
| 352 |
where a[1] = "James Bond" order by :1;<BR> |
| 353 |
select min(@tmp):1</CODE> |
| 354 |
<DD><P>This query is structured into two parts. The first query lists the release dates of all films |
| 355 |
with the character named "James Bond". The nature of the model for the <I>Release</I> tag means |
| 356 |
that multiple release dates can be stored. The table-functions <EM>first</EM> ensures we're only listing |
| 357 |
one release date per movie. The results are sorted and assigned to the table variable <TT>@tmp</TT>. |
| 358 |
<BR> |
| 359 |
The second part of the query uses the table function <EM>min</EM> to extract the smallest value |
| 360 |
from the first column of the given table. This will be the earliest release date of any bond film. |
| 361 |
<BR><A HREF="result20.txt">results</A> |
| 362 |
</P> |
| 363 |
|
| 364 |
<DT><H4>21. characters that appeared in more than 1 movie listed with the movies, sorted by the movie primary release date</H4> |
| 365 |
<DD><CODE>@characters := select c, <BR> |
| 366 |
count(select m1 from m1 in class Movie where m1->Cast[2] = c), <BR> |
| 367 |
m, first(select m->Release[Date]) <BR> |
| 368 |
from m in class Movie, c in m->Cast[2];<BR> |
| 369 |
select character::row:1, movie::row:3, date::row:4 <BR> |
| 370 |
from row in @characters <BR> |
| 371 |
where row:2 > 1 <BR> |
| 372 |
order by :character, :date</CODE> |
| 373 |
<DD><P>The table-variable feature is used again to separate the work into two parts. The first query |
| 374 |
does all the work, whereas the second query, just selects specific fields and does the sorting. |
| 375 |
We will learn later how this can be written all in one query. |
| 376 |
<BR> |
| 377 |
The second query demonstrates a row-variable. The iterator <TT>row</TT> is declared as a cursor |
| 378 |
on the table <TT>@characters</TT>. In the select-clause we extract fields 1, 3 and 4 from that row |
| 379 |
whereas the where-clause uses field 2 for the comparison. |
| 380 |
<BR> |
| 381 |
The select-clause also renames the select-fields, which makes the query more readable. |
| 382 |
Those aliased names ('<TT>character</TT>', '<TT>movie</TT>' and '<TT>date</TT>') can then be used for the sort-clause |
| 383 |
to refer to select-fields by their name. |
| 384 |
<BR><A HREF="result21.txt">results</A> |
| 385 |
<P> |
| 386 |
The use of table-variables sometimes stems from the way people compose |
| 387 |
queries by incrementally building them up from less complex queries. |
| 388 |
Their use is not always necessary though. Here is the same example, |
| 389 |
but without table-variables :<BR> |
| 390 |
|
| 391 |
<CODE>select c, m, mdate<BR> |
| 392 |
from m in class Movie, c in m->Cast[2],<BR> |
| 393 |
num in count(select m1 from m1 in class Movie where m1->Cast[2] = c)<BR> |
| 394 |
where num > 1,<BR> |
| 395 |
mdate in first(select m->Release[Date])<BR> |
| 396 |
order by :c, :mdate</CODE> |
| 397 |
<P> |
| 398 |
See example (29) for more on splitting queries into two parts and table-variables. |
| 399 |
</P> |
| 400 |
</DL> |
| 401 |
|
| 402 |
<HR> |
| 403 |
|
| 404 |
<H3>Bond-movie examples</H3> |
| 405 |
|
| 406 |
<DL> |
| 407 |
|
| 408 |
<DD><P>The database contains a particular dataset, which is useful for testing |
| 409 |
and learning. It contains the last 20 James Bond movies. Some have little detail |
| 410 |
stored about them, but the one feature that identifies them is the character name |
| 411 |
"James Bond", which is stored with the particular Bond-actor.<BR> |
| 412 |
The following examples will demonstrate a range of features which get |
| 413 |
progressively more complicated.</P> |
| 414 |
|
| 415 |
<DT><H4>22. list bond movies</H4> |
| 416 |
<DD><CODE>select m from m in class Movie where m->Cast[2] = "James Bond"</CODE> |
| 417 |
<DD><P>In all Bond-movies there is a character called "James Bond". |
| 418 |
The character name is the data-element to the right of the actors name under the <I>Cast</I>-tag |
| 419 |
in the <I>Movie</I>-object. |
| 420 |
<BR><A HREF="result22.txt">results</A> |
| 421 |
</P> |
| 422 |
|
| 423 |
<DT><H4>23. list bond movies together with all of the cast</H4> |
| 424 |
<DD><CODE>select m, m->Cast from m in class Movie where m->Cast[2] = "James Bond"</CODE> |
| 425 |
<DD><P>The where-clause is important : it applies to the iterator <TT>m</TT>, |
| 426 |
and will be evaluated once per movie. So the list of possible |
| 427 |
values for m is restricted to Bond-movies. |
| 428 |
<BR> |
| 429 |
The select-clause will then evaluate <TT>m->Cast</TT> again for all |
| 430 |
of those movies, and therefore report all cast-members of those movies, |
| 431 |
not just the ones playing "James Bond". |
| 432 |
This may not always be what you want, so compare with (24). |
| 433 |
<BR><A HREF="result23.txt">results</A> |
| 434 |
</P> |
| 435 |
|
| 436 |
|
| 437 |
<DT><H4>24. list bond movies together with all of the cast</H4> |
| 438 |
<DD><CODE>select m, a from m in class Movie, a in m->Cast where a[1] = "James Bond"</CODE> |
| 439 |
<DD><P>The subtle difference to (23) is important. The where-clause now applies |
| 440 |
directly to the iterator <TT>a</TT> (for "actor"). It will only be true if |
| 441 |
the given actor <TT>a</TT> is playing "James Bond", and because the |
| 442 |
select-clause reports the value of <TT>a</TT>, only the Bond-actors |
| 443 |
are shown. |
| 444 |
<BR> |
| 445 |
<STRONG>Note</STRONG>, how we get the value for the character name in this query. |
| 446 |
The iterator <TT>m->Cast</TT> s automatically rewritten as |
| 447 |
<TT>m->Cast[1]</TT> (the first data-element to the right is reported by default). |
| 448 |
Therefore we only have to step one more position further to the right to get |
| 449 |
to the second field, which is the character name. |
| 450 |
<BR><A HREF="result24.txt">results</A> |
| 451 |
</P> |
| 452 |
<P>If the query was written as |
| 453 |
</P> |
| 454 |
<DD><CODE>select m, m->Cast from m in class Movie, a in m->Cast where a[1] = "James Bond"</CODE> |
| 455 |
<P>the result would be the same as <A HREF="result23.txt">(23)</A>, because the |
| 456 |
extra declaration of <TT>a</TT> is just used in the where-clause and |
| 457 |
is basically the same as referring to <TT>m->Cast[2]</TT> directly. |
| 458 |
</P> |
| 459 |
|
| 460 |
<DT><H4>25. list bond films with the James Bond actor and list all the movies that actor has starred in</H4> |
| 461 |
<DD><CODE>select m, a, a->Stars_in from m in class Movie, a in m->Cast where a[1] = "James Bond"</CODE> |
| 462 |
<DD><P>Basically the same as (24), but with an extra select field which lists all movies by the actor <I>a</I>. |
| 463 |
<BR><A HREF="result25.txt">results</A> |
| 464 |
</P> |
| 465 |
|
| 466 |
<DT><H4>26. list all James Bond actors and the total number of movies they've starred in</H4> |
| 467 |
<DD><CODE>select a, count(select m1 from m1 in class Movie where m1->Cast = a) <BR> |
| 468 |
from m in class Movie, a in m->Cast where a[1] = "James Bond"</CODE> |
| 469 |
<DD><P>This query makes use of a nested sub-query. For every bond actor as determined by the outer query, |
| 470 |
the nested query loops over all movies again finding the ones which where the cast includes the actor |
| 471 |
<TT>a</TT> from the outer query. |
| 472 |
<BR><A HREF="result26.txt">results</A> |
| 473 |
</P> |
| 474 |
|
| 475 |
<DT><H4>27. list all James Bond actors and how many non bond-films they've starred in</H4> |
| 476 |
<DD><CODE>select a, count(select m1 from m1 in class Movie, <BR> |
| 477 |
a1 in m1->Cast where a1 = a and a1[1] != "James Bond") <BR> |
| 478 |
from m in class Movie, <BR> |
| 479 |
a in m->Cast where a[1] = "James Bond"</CODE> |
| 480 |
<DD><P>The inner is similar to the one in (26), but the where-clause is more elaborate |
| 481 |
to count only the films where the current actor <TT>a</TT> (from the outer scope) |
| 482 |
played someone that wasn't called "James Bond". |
| 483 |
<BR><A HREF="result27.txt">results</A> |
| 484 |
</P> |
| 485 |
|
| 486 |
<DT><H4>28. for all Bond actors list their films where they weren't James Bond and who they played in those films</H4> |
| 487 |
<DD><CODE>select a, otherm, otherc<BR> |
| 488 |
from m in class Movie, <BR> |
| 489 |
a in m->Cast <BR> |
| 490 |
where a[1] = "James Bond", <BR> |
| 491 |
otherm in a->Stars_in, a1 in otherm->Cast<BR> |
| 492 |
where a = a1,<BR> |
| 493 |
otherc in a1[1] <BR> |
| 494 |
where otherc != "James Bond"</CODE> |
| 495 |
<DD><P>We don't need a nested query here to count the number of movies for each Bond-actor. |
| 496 |
The first where-clause restricts the value of <TT>a</TT> to only those actors who have played |
| 497 |
"James Bond". The iterator <TT>otherm</TT> then find all their movies (which at this stage |
| 498 |
still includes all their bond movies as well). |
| 499 |
The last clause then gets their character names from all those movies and excludes "James Bond". |
| 500 |
This will exclude all actors who only played James Bond. |
| 501 |
<BR><A HREF="result28.txt">results</A> |
| 502 |
</P> |
| 503 |
</DL> |
| 504 |
|
| 505 |
<HR> |
| 506 |
|
| 507 |
<H3>Tricks and pitfalls</H3> |
| 508 |
|
| 509 |
<DL> |
| 510 |
|
| 511 |
<DD><P>All of the "James Bond" examples have been expressed so far in just one query. The query in example (28) |
| 512 |
grows to considerable complexity. The section on table-variables showed how some queries are better |
| 513 |
split up into two or more queries. A final query can then combine the results as shown in example (22). |
| 514 |
</P> |
| 515 |
|
| 516 |
<DT><H4>29. list actors from Bond-films that didn't play "James Bond"</H4> |
| 517 |
<DD><CODE>@a := select m->Cast from m in class Movie <BR> |
| 518 |
where m->Cast[2] = "James Bond";<BR> |
| 519 |
@b := select c from m in class Movie, <BR> |
| 520 |
c in m->Cast, <BR> |
| 521 |
otherm in c->Stars_in where c[1] = "James Bond";<BR> |
| 522 |
@a diff @b</CODE> |
| 523 |
<DD><P>The table <TT>@a</TT> lists all actors from movies where a character was called "James Bond". |
| 524 |
The second table <TT>@b</TT> will list all those actors who actually |
| 525 |
played the "James Bond" character. That leaves us to report the difference of the two |
| 526 |
table using the <EM>diff</EM> table-function. |
| 527 |
<BR> |
| 528 |
The use of table-variables can be avoided because the <EM>diff</EM> keyword can be placed |
| 529 |
between any two expressions which return a table value, i.e. two select-from-where queries : |
| 530 |
</P> |
| 531 |
<CODE> |
| 532 |
select m->Cast from m in class Movie <BR> |
| 533 |
where m->Cast[2] = "James Bond"<BR> |
| 534 |
diff<BR> |
| 535 |
select c from m in class Movie, <BR> |
| 536 |
c in m->Cast, <BR> |
| 537 |
otherm in c->Stars_in where c[1] = "James Bond" |
| 538 |
</CODE> |
| 539 |
<BR><A HREF="result29a.txt">results</A> |
| 540 |
<P>The trick of the two queries which are combines by diff, leave no ambiguity. |
| 541 |
Spot the difference in this query, where everything is expressed as just one select-from-where query : |
| 542 |
</P> |
| 543 |
<CODE> |
| 544 |
select othera <BR> |
| 545 |
from m in class Movie<BR> |
| 546 |
where m->Cast[2] = "James Bond", <BR> |
| 547 |
othera in m->Cast<BR> |
| 548 |
where othera[1] != "James Bond" |
| 549 |
</CODE> |
| 550 |
<BR><A HREF="result29b.txt">results</A> |
| 551 |
<P>At first sight it seems to do the right thing. We select only the movies <TT>m</TT> where a character |
| 552 |
is called "James Bond". The variable <TT>othera</TT> then lists all actors again for only those movies |
| 553 |
and we check that we only report those who didn't play "James Bond". |
| 554 |
So how is it different ? |
| 555 |
</P> |
| 556 |
<P>The line "Sophie Marceau" is missing from the result. Closer inspection of the data |
| 557 |
reveals that no character name is stored for her role in "The world is not enough". The second where-clause |
| 558 |
on <TT>othera</TT> therefore has no value for <TT>othera[1]</TT> to be used in the comparison. |
| 559 |
The comparison will evaluate to NULL and no result is shown for "Sophie Marceau". |
| 560 |
</P> |
| 561 |
<P> |
| 562 |
See the section on <A HREF="../overview.shtml#NULL_VALUES">NULL values</A>. |
| 563 |
</P> |
| 564 |
</DL> |
| 565 |
|
| 566 |
<HR> |
| 567 |
Queries designed by Fred Wobus.<BR> |
| 568 |
The Sanger Centre, 06/Dec/1999 |
| 569 |
|
| 570 |
<ADDRESS><A HREF="mailto:[email protected]">[email protected]</A></ADDRESS> |
| 571 |
|
| 572 |
</BODY> |
| 573 |
</HTML> |
| 574 |
|