Hi,
I have been playing with an e-commerce sample database I migrated from SQL Server to Stardog.
I have been translating SQL queries to SPARQL and have a few of them already. However, I'm finding difficult to do things like calculating running totals, moving averages, row numbers and ranking, etc.
Before I explain the two subqueries below, here is a snippet of the model:
product -> hasCategory -> category
order -> hasCustomer -> customer
The first query is working fine. However, I need to find an alternative for the second one, where I need to return the two most recent orders for each customers in the outer query. In SQL, one of the solutions would be to use a correlated query (pass customerID to the inner query for each record of the outer query).
I have notice a few issues with SPARQL, logged below:
I see you guys have proposed Arrays:
Extending the Solution | Stardog
Query 1: Select all products that belong to the Seafood category
SELECT
?productName
?unitPrice
?unitsInStock
WHERE { # outer query
?product a :product ;
:productName ?productName ;
:unitPrice ?unitPrice ;
:unitsInStock ?unitsInStock ;
:hasCategory ?category .
{ # inner query
SELECT
?category
WHERE {
?category a :category ;
:categoryID ?categoryID ;
:name "Seafood" .
}
}
}
ORDER BY
?productName
Query 2 (doesn't work): Select the two most recent orders of each customer
SELECT DISTINCT
?customerID
?city
WHERE { # outer query
?customer a :customer ;
:customerID ?customerID ;
:city ?city ;
^:hasCustomer ?order .
{ # inner query
SELECT
?order
WHERE {
?order a :order ;
:orderID ?orderID ;
:orderDate ?orderDate ;
:hasCustomer ?customer .
}
ORDER BY
DESC(?orderDate)
LIMIT 2
}
}
ORDER BY
?customerID
?city
DESC(?orderDate)