1

I have a problem here, it looks easy but because I am not really familiar with Sparal queries I could not find a solution for it.

PREFIX onto: <http://dbpedia.org/ontology/>
PREFIX resu: <http://dbpedia.org/resource/>
PREFIX yago: <http://dbpedia.org/class/yago/>

SELECT 

count(?MontrealC) as ?Montrea
count(?TorontoC) as ?Toronto
count(?OttawaC) as ?Ottawa
count(?VancouverC) as ?Vancouver


WHERE
{
{?MontrealC rdf:type yago:HotelsInMontreal} UNION
{?TorontoC rdf:type yago:HotelsInToronto} UNION
{?OttawaC rdf:type yago:HotelsInOttawa} UNION
{?VancouverC a onto:Hotel .
{?VancouverC onto:location resu:Vancouver} UNION
{?VancouverC onto:location ?street .
?street a yago:StreetsInVancouver}} UNION
{?VancouverC a yago:HotelsInBritishColumbia .
{?VancouverC onto:location resu:Vancouver} UNION
{?VancouverC onto:location ?street.
?street a yago:StreetsInVancouver}
}}

Once you apply this query in any dbpedia endpoint you will get a result shown in tow rows and four columns as the following:

Montreal    Toronto     Ottawa    Vancouver
8           28          5         10

The problem is I need them to be in tow Columns and four rows same as the following:

Montreal    Toronto
8           28 
Ottawa    Vancouver
5         10

Is that something possible in SPARQL queries?

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
MR BIG
  • 163
  • 1
  • 1
  • 6
  • You say you want four rows but what you posted there only has one row. – cygri Mar 21 '12 at 20:18
  • Sorry it is may problem. I have got an answer in where else says that this can not be happened in SPARQL. – MR BIG Mar 21 '12 at 21:27
  • What are you trying to accomplish with all those unions? Can you explain what your query is asking for? There's almost certainly a cleaner way to write it… Also, while Virtuoso (the SPARQL endpoint used by DBpedia) may accept this query, it's not legal SPARQL as written. It's worthwhile to run your queries through the validator at sparql.org. – Joshua Taylor May 20 '14 at 21:00

1 Answers1

3

That is not possible with one SPARQL query. It shouldn't be a big problem however, as you already get the right data and you just want to have a different representation of it. Can you give more context on why you need the data this way, how many of those queries you ask and if you do it manually or embedded in some program (if yes, which programming language do you use)?

If you really want it this way, split it in two queries and append the results:

Query 1

PREFIX onto: <http://dbpedia.org/ontology/>
PREFIX resu: <http://dbpedia.org/resource/>
PREFIX yago: <http://dbpedia.org/class/yago/>

SELECT 

count(?MontrealC) as ?Montreal
count(?TorontoC) as ?Toronto

WHERE
{
{?MontrealC rdf:type yago:HotelsInMontreal} UNION
{?TorontoC rdf:type yago:HotelsInToronto}
}

Query 2

PREFIX onto: <http://dbpedia.org/ontology/>
PREFIX resu: <http://dbpedia.org/resource/>
PREFIX yago: <http://dbpedia.org/class/yago/>

SELECT 

count(?OttawaC) as ?Ottawa
count(?VancouverC) as ?Vancouver

WHERE
{
{?OttawaC rdf:type yago:HotelsInOttawa} UNION
{?VancouverC a onto:Hotel .
{?VancouverC onto:location resu:Vancouver} UNION
{?VancouverC onto:location ?street .
?street a yago:StreetsInVancouver}} UNION
{?VancouverC a yago:HotelsInBritishColumbia .
{?VancouverC onto:location resu:Vancouver} UNION
{?VancouverC onto:location ?street.
?street a yago:StreetsInVancouver}
}}

Merge

  • save the results as query1.csv and query2.csv
  • in a terminal, type "cat query1.csv query2.csv > query.csv"

    $ cat query1.csv query2.csv
    "Montrea","Toronto"
    8,28
    "Ottawa","Vancouver"
    5,10
    
scotthenninger
  • 3,921
  • 1
  • 15
  • 24
Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118