Changing Results Returned

Changing data returned

You can always change the data that is returned by performing string or numeric operations on the data.

For example:

cypher
MATCH (m:Movie)<-[:ACTED_IN]-(p:Person)
WHERE m.title CONTAINS 'Toy Story' AND
p.died IS NULL
RETURN m.title AS movie,
p.name AS actor,
p.born AS dob,
date().year - p.born.year AS ageThisYear

This query returns the actors in each Toy Story movie and their age if they are still living according to our graph. We add data to each line by calculating the actor’s age.

Here is an example where we concatenate string data returned:

cypher
MATCH (m:Movie)<-[:ACTED_IN]-(p:Person)
WHERE m.title CONTAINS 'Toy Story' AND
p.died IS NULL
RETURN 'Movie: ' + m.title AS movie,
p.name AS actor,
p.born AS dob,
date().year - p.born.year AS ageThisYear

Conditionally changing data returned

Cypher has a CASE clause that you can specify to compute the data returned which may be different from what is in the graph.

Here is an example:

cypher
MATCH (m:Movie)<-[:ACTED_IN]-(p:Person)
WHERE p.name = 'Henry Fonda'
RETURN m.title AS movie,
CASE
WHEN m.year < 1940 THEN 'oldies'
WHEN 1940 <= m.year < 1950 THEN 'forties'
WHEN 1950 <= m.year < 1960 THEN 'fifties'
WHEN 1960 <= m.year < 1970 THEN 'sixties'
WHEN 1970 <= m.year < 1980 THEN 'seventies'
WHEN 1980 <= m.year < 1990 THEN 'eighties'
WHEN 1990 <= m.year < 2000 THEN 'nineties'
ELSE  'two-thousands'
END
AS timeFrame

In this query we transform the data returned to reflect the timeframe for the movie.

Check your understanding

1. What age?

We want to return information about actors who acted in the Toy Story movies. We want to return the age that an actor will turn this year or that the actor died.

How would you complete this query?

Once you have selected your option, click the Check Results query button to continue.

cypher
MATCH (m:Movie)<-[:ACTED_IN]-(p:Person)
WHERE m.title CONTAINS 'Toy Story'
RETURN m.title AS movie,
p.name AS actor,
p.born AS dob,
/*select:CASE
WHEN p.died IS NULL THEN date().year - p.born.year
WHEN p.died IS NOT NULL THEN "Died"
END*/
AS ageThisYear
  • WHEN p.died IS NULL THEN date().year - p.born.year WHEN p.died IS NOT NULL THEN "Died"

  • CASE WHEN p.died IS NULL THEN date().year - p.born.year WHEN p.died IS NOT NULL THEN "Died" END

  • IF p.died IS NULL THEN date().year - p.born.year IF p.died IS NOT NULL THEN "Died"

  • CHOOSE WHEN p.died IS NULL THEN date().year - p.born.year WHEN p.died IS NOT NULL THEN "Died" END`

Hint

What keyword is used to conditionally return values based upon a property value?

Solution

The answer is CASE WHEN p.died IS NULL THEN date().year - p.born.year WHEN p.died IS NOT NULL THEN "Died" END

2. Keywords used for conditionally returning values

What keywords can you use in a RETURN clause to conditionally return a value?

  • ✓ CASE

  • ✓ WHEN

  • ✓ ELSE

  • ❏ IF

  • ✓ END

Hint

All of the valid keywords above are part of the CASE statement. You can read more about expressions in the Neo4j documentation.

Solution

The correct answers are CASE, WHEN, ELSE and END

Summary

In this lesson, you learned how you can add to or modify results returned.

In the next challenge, you will write queries to customize results returned.