# Re: Minimum/Maximum values

```Jon Crump wrote:
> Dear all,
>
> Another naive question from the peanut gallery about date arithmetic (or
> arithmetic in general)
>
> Given a graph:
>
> :bob a :Person;
>     :byear "1962".
> :carol a :Person;
>     :byear "1963".
> :ted a :Person;
>     :byear "1964".
> :alice a :Person;
>     :byear "1965".
>
> :Smiths a :Family;
>     :children :bob, :carol, :ted, :alice.
>
>
> How to find the minimum and maximum birthyears represented in family
> Smith? I've gotten this far:
>
> SELECT ?date
> WHERE{ ?family :children ?child .
>     ?child :byear ?date .
>     OPTIONAL{{
>         {?earlier :byear ?edate . FILTER (?edate < ?date)}
>         {?later :byear ?ldate . FILTER (?ldate > ?date)}
>     }}
>     FILTER (!bound(?later))
> }
>
> But this binds both earliest and latest to ?date. To make the comparison
> in the filters I have to compare ?edate and ?ldate to a common variable
> ?date, but what I'd like my query to return is the earliest date bound
> to ?start and the latest bound to ?end.

Hi John,

I think the key observation here is that you're trying to find two
different children. (They might be the same child twice for an only
child situation, but for the purposes of the query, it's two children.)

So let's rewrite your query with that in mind:

SELECT ?start ?end
WHERE {
?family :children ?oldest, ?youngest .
?oldest :byear ?start .
OPTIONAL { ?older :byear ?earlier . FILTER (?earlier < ?start) }
?youngest :byear ?end .
OPTIONAL { ?younger :byear ?later . FILTER (?later > ?end}
FILTER(!bound(?earlier) && !bound(?later))
}

(I haven't tested this, so apologies if it's not quite right, but I hope
it gets the idea across.)

Note that aggregate functions such as MIN and MAX are one potential
topic that the newly re-started SPARQL Working Group may end up tackling
in a new round of specification, which would make queries like this much
easier to express.

If you are associated with a W3C member organization and would like to
join the working group, please send me an email and let me know.

If you are not interested or able to join the working group but would
like to follow the group's work and contribute input to the future
direction of SPARQL, please do one or more of the following:

+ monitor the group's progress at http://www.w3.org/2009/sparql/wiki/
(site still being assembled)
+ monitor occasional announcements at the Semantic Web Activity News
blog - http://www.w3.org//2001/sw/anews/

(In general, the -comments mailing list is the best way for community
members to make their voice heard to the working group.)

best,
Lee

> Something obvious here that I'm missing. Suggestions?
>
> Thanks,
> Jon
>
>
```

Received on Saturday, 28 February 2009 07:53:08 UTC