Re: How to query properly

On 03/06/13 21:51, Remo Liechti wrote:
> Hi guys,


> I want to query the number of runways per airport. So ZRH should have 3
> runways, Basel 1 of them.
>
> I use this query:
> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
> PREFIX fh: <http://swt.ffhs.ch <http://swt.ffhs.ch/>>
> SELECT ?name  (COUNT(?r) as ?runways)
> WHERE {
>    ?a rdf:type fh:airport.
>    ?a fh:name ?name.
>    ?r rdf:type fh:runway.
> }
> group by ?name

> Which results in 4 runways per airport, which is apparently not what I
> was looking for. How can I make a relation between airport and runways?
> I thought the collection takes care of that?

?r rdf:type fh:runway.

isn't connected (by variables of the same name) to anything else in your 
query.  You need to connect airport to runway.  fh:Runways does this ... 
to a list of runways.

The idiom for getting at the members of a list is:

?list rdf:rest*/rdf:first ?member .

It's worth dumping your data in N-Triples to see the triples structure - 
lists are encoded as triples.

Combining all this:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX fh: <http://swt.ffhs.ch>
SELECT ?name  (COUNT(?rw) as ?runways)
WHERE {
   ?a rdf:type fh:airport.
   ?a fh:name ?name.
   ?a fh:Runways ?listRW .
   ?listRW rdf:rest*/rdf:first ?rw .
}
GROUP BY ?name

which gives me:

--------------------------------
| name               | runways |
================================
| "Flughafen Basel"  | 1       |
| "Flughafen Zürich" | 3       |
--------------------------------

(using Apache Jena)

	Andy

Received on Tuesday, 4 June 2013 14:45:54 UTC