'SPARQL returning empty result when passing MIN(?date1) from subquery into outer query, with BIND((YEAR(?minDate) - YEAR(?date2)) AS ?diffDate)

<This question is now resolved, see comment by Valerio Cocchi>

I am trying to pass a variable from a subquery, that takes the minimum date of a set of dates ?date1 belonging to ?p and passes this to the outer query, which then takes another date ?date2 belonging to ?p (there can be at most 1 ?date2 for every ?p) and subtracts ?minDate from ?date2 to get an integer value for the number of years between. I am getting a blank value for this, i.e. ?diffDate returns no value.

I am using Fuseki version 4.3.2. Here is an example of the query:

SELECT ?p ?minDate ?date2 ?diffDate 
  {        
  ?p a abc:P;
    abc:hasAnotherDate ?date2.      
    BIND((YEAR(?minDate) - YEAR(?date2)) AS ?diffDate)      
  {
  SELECT ?p (MIN(?date1) as ?minDate) 
  WHERE 
  { 
  ?p a abc:P;
    abc:hasDate ?date1. 

  } group by ?p 
    }                                 
  } 

and an example of the kind of result I am getting:

|-?p----|-----------------?minDate-------------|-----------------?date2------------- |?diffDate|

|<123>|20012-11-22T00:00:00"^^xsd:dateTime|2008-08-18T00:00:00"^^xsd:dateTime| |

I would expect that ?diffDate would give me an integer value. Am I missing something fundamental about how subqueries work in SPARQL?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source