问题
Following this qeustion: Summer in Greece with SPARQL, since my memory runs out when executing this query, I would like to constrain the query between two regional units, but I can't group them:
SELECT * #?municipality (?bwCount1+?bwCount2 as ?bwCount)
WHERE {
{
SELECT (COUNT(?bw) as ?bwCount1) WHERE
{
?regional_unit geo:έχει_επίσημο_όνομα "ΠΕΡΙΦΕΡΕΙΑΚΗ ΕΝΟΤΗΤΑ ΗΡΑΚΛΕΙΟΥ" .
?municipality1 geo:ανήκει_σε ?regional_unit .
?municipality1 geo:έχει_γεωμετρία ?geometry .
?bw geos:hasGeometry ?bw_geo .
?bw_geo geos:asWKT ?bw_geo_wkt .
FILTER(strdf:within(?geometry, ?bw_geo_wkt)) .
?bw unt:has_concie_0 ?concie_0 .
FILTER(?concie_0 > 40)
}
}
UNION
{
SELECT (COUNT(?bw) as ?bwCount2) WHERE
{
?regional_unit geo:έχει_επίσημο_όνομα "ΠΕΡΙΦΕΡΕΙΑΚΗ ΕΝΟΤΗΤΑ ΛΕΣΒΟΥ" .
?municipality2 geo:ανήκει_σε ?regional_unit .
?municipality2 geo:έχει_γεωμετρία ?geometry .
?bw geos:hasGeometry ?bw_geo .
?bw_geo geos:asWKT ?bw_geo_wkt .
FILTER(strdf:within(?geometry, ?bw_geo_wkt)) .
?bw unt:has_concie_0 ?concie_0 .
FILTER(?concie_0 > 40)
}
}
}
#GROUP BY ?municipality
#ORDER BY DESC(?bwCount)
What am I missing?
回答1:
You may again be confusing yourself with the sub-selects, and this could be causing inefficient use of memory. I see a couple of way of getting the results I believe you want (it is a bit unclear). The first is by using UNION
:
SELECT (COUNT(?bw1) as ?bwCount1) (COUNT(?bw2) as ?bwCount2)
WHERE {
?municipality1 geo:ανήκει_σε ?regional_unit .
?municipality1 geo:έχει_γεωμετρία ?geometry .
{
?regional_unit geo:έχει_επίσημο_όνομα "ΠΕΡΙΦΕΡΕΙΑΚΗ ΕΝΟΤΗΤΑ ΗΡΑΚΛΕΙΟΥ" .
?bw2 geos:hasGeometry ?bw_geo .
}
UNION
{
?regional_unit geo:έχει_επίσημο_όνομα "ΠΕΡΙΦΕΡΕΙΑΚΗ ΕΝΟΤΗΤΑ ΛΕΣΒΟΥ" .
?bw1 geos:hasGeometry ?bw_geo .
}
?bw_geo geos:asWKT ?bw_geo_wkt .
FILTER(strdf:within(?geometry, ?bw_geo_wkt)) .
?bw unt:has_concie_0 ?concie_0 .
FILTER(?concie_0 > 40)
}
A best practice here is to not repeat triple patterns inside the UNION
unless they are part of the disjunction computation.
You could also do this with a group by
on ?regional_unit:
SELECT (COUNT(?bw) as ?bwCount1)
WHERE {
VALUES ?name {"ΠΕΡΙΦΕΡΕΙΑΚΗ ΕΝΟΤΗΤΑ ΗΡΑΚΛΕΙΟΥ" "ΠΕΡΙΦΕΡΕΙΑΚΗ ΕΝΟΤΗΤΑ ΛΕΣΒΟΥ"}
?municipality1 geo:ανήκει_σε ?regional_unit .
?municipality1 geo:έχει_γεωμετρία ?geometry .
?regional_unit geo:έχει_επίσημο_όνομα ?name .
?bw geos:hasGeometry ?bw_geo .
?bw_geo geos:asWKT ?bw_geo_wkt .
FILTER(strdf:within(?geometry, ?bw_geo_wkt)) .
?bw unt:has_concie_0 ?concie_0 .
FILTER(?concie_0 > 40)
} GROUP BY ?regional_unit
来源:https://stackoverflow.com/questions/36530519/summer-in-greece-vol-2