在 SPARQL 中过滤投影表达式

Filter projected expressions in SPARQL

这是一个示例查询:

PREFIX  dc:  <http://purl.org/dc/elements/1.1/> 
PREFIX  ns: <http://example.org/ns#> 
SELECT  ?title (?p AS ?fullPrice) (?fullPrice*(1-?discount) AS ?customerPrice) 
WHERE {
  ?x ns:price ?p .    
  ?x dc:title ?title .     
  ?x ns:discount ?discount
}

结果为:

| title              | fullPrice | customerPrice |
| "The Semantic Web" |        23 |         17.25 |
| "SPARQL Tutorial"  |        42 |         33.6  |

我只想显示 customerPrice > 20。

我在查询末尾尝试了 HAVING (?customerPrice > 20),但似乎看不到投影表达式。

还有其他方法吗?

将计算变量从 SELECT 列表移动到查询模式内的 BIND 子句中。然后你可以在变量上使用 FILTER

SELECT ?title ?fullPrice ?customerPrice
WHERE {
    ?x ns:price ?fullPrice.
    ?x dc:title ?title.
    ?x ns:discount ?discount
    BIND (?fullPrice * (1-?discount) AS ?customerPrice)
    FILTER (?customerPrice > 20)
}