2012-03-05 28 views
2
聲明

我有以下SPARQL查詢:IF在SPARQL

PREFIX yago: <http://dbpedia.org/class/yago/> 
SELECT distinct count (?Montreal) as ?Montreal count(?Toronto) as ?Toronto 
WHERE 
{ 
{?Montreal rdf:type yago:HospitalsInMontreal} UNION {?Toronto rdf:type yago:HospitalsInToronto}. 
} 

此查詢結果:

蒙特利爾= 20 多倫多= 28

我要的是: 我想編輯查詢,而不是給予20和28,我想比較這樣的結果: 如果蒙特利爾的醫院數量大於多倫多的醫院數量那麼:

蒙特利爾= 1 多倫多= 2

如果在多倫多醫院的數量比醫院的蒙特利爾然後數目越大:

蒙特利爾= 2
多倫多= 1

我試圖此查詢,但沒有奏效:

PREFIX yago: <http://dbpedia.org/class/yago/> 
SELECT distinct count (?Montreal) as ?Montreal count(?Toronto) as ?Toronto 
WHERE 
{ 
{?Montreal rdf:type yago:HospitalsInMontreal} UNION {?Toronto rdf:type yago:HospitalsInToronto}. 
LET(?Montreal := IF(?Montreal > ?Toronto, -1, 1). 
} 

感謝

回答

5

有一個在SPARQL無不讓表達,但你並不需要一個以上查詢:

PREFIX yago: <http://dbpedia.org/class/yago/> 
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
SELECT distinct if(count(?MontrealC)>count(?TorontoC),1,2) as ?Montreal if(count(?TorontoC)>count(?MontrealC),1,2) as ?Toronto 
WHERE 
{ 
{?MontrealC rdf:type yago:HospitalsInMontreal} UNION {?TorontoC rdf:type yago:HospitalsInToronto}. 
} 
+5

新SPARQL 1.1引入了BIND這相當於在這種情況下讓。另外,爲了便於攜帶,您的項目表達式需要在它們周圍加括號:(if(count(?MontrealC)> count(?TorontoC),1,2)as?Montreal) – 2012-08-20 05:19:40