Conditional Expressions

AnzoGraph supports CASE expressions, which enable you to add IF/THEN logic to a query. A CASE expression evaluates a series of conditions for the triples that you specify and returns the matching result expression. You can use CASE expressions wherever expressions are valid in SPARQL queries.

CASE Syntax

There are two variations of CASE statements: simple and generic. Use the simple form to compare the results of an expression with a series of tests and return a result when a test returns true. Use the generic form when evaluating a larger range of tests where you want to test multiple boolean expressions.

Simple Form

CASE expression 
WHEN expression1 THEN result1
WHEN expression2 THEN result2
[ ... ]
[ ELSE result ]
END

Generic Form

CASE WHEN condition1 THEN result1
WHEN condition2 THEN result2
[ ... ]
[ ELSE result ]
END

Examples

The following example uses a CASE statement to determine and report on whether the top 20 events (with the most tickets sold) sold out.

SELECT ?event ?venue ?seats
(( CASE WHEN (?seats <= (sum(?qty))) then "yes"
        WHEN (?seats > (sum(?qty))) then "no"
   END ) as ?sold_out)
FROM <tickit>
WHERE {
  ?sales <qtysold> ?qty .
  ?sales <eventid> ?eventid .
  ?eventid <eventname> ?event .
  ?eventid <venueid> ?venueid .
  ?venueid <venuename> ?venue .
  ?venueid <venueseats> ?seats.
}
GROUP BY ?event ?venue ?seats ?qty
ORDER BY desc(?qty)
LIMIT 20
event                 | venue                        | seats | sold_out
----------------------+------------------------------+-------+----------
Kansas                | Safeco Field                 | 47116 | no
Spoon                 | Dolphin Stadium              | 74916 | no
Armando Manzanero     | Texas Stadium                | 65595 | no
Zombies               | Lambeau Field                | 72922 | no
Sunday in the Country | Edward Jones Dome            | 66965 | no
Simple Plan           | Hubert H. Humphrey Metrodome | 64035 | no
Marc Anthony          | Georgia Dome                 | 71149 | no
Hannah Montana        | Monster Park                 | 69843 | no
Black Crowes          | Yankee Stadium               | 52325 | no
Hot Tuna              | Turner Field                 | 50091 | no
Dave Stewart          | Dolphin Stadium              | 74916 | no
Missy Higgins         | Great American Ball Park     | 42059 | no
Mark Knopfler         | Edward Jones Dome            | 66965 | no
Billy Idol            | Rogers Centre                | 50516 | no
Earth Wind and Fire   | McAfee Coliseum              | 63026 | no
Rolling Stones        | PETCO Park                   | 42445 | no
Rolling Stones        | Dolphin Stadium              | 74916 | no
Montgomery Gentry     | Safeco Field                 | 47116 | no
Jason Mraz            | Lambeau Field                | 72922 | no
Oasis                 | ARCO Arena                   | 42350 | no
20 rows
Related Topics