0

I would like to ask if it is possible to PIVOT (SQL) data (suppose the same as 'transpose' in excel) data? Thank you

Tezra
  • 8,463
  • 3
  • 31
  • 68
  • In a relational database or spreadsheet you have tables and using `transpose` makes sense, but Neo4j uses graphs. I don't know of a way to `pivot` a graph but I do know that you can get some results as a table, and that can be transposed. I do get some of my results as tables, e.g. Labels versus Properties, and I use Java to transpose that table. – Guy Coder Nov 15 '18 at 17:13
  • Can you provide an example with sample data, to clarify what you are asking? – cybersam Nov 15 '18 at 18:51

1 Answers1

2

Cypher does not strictly support the PIVOT operation, although the WITH operation can do the exact same effect using a map.

The only why I can think of to create the (pseudo) dynamic map for the pivot is to use fromPairs from APOC.

Here is an example of what I mean, with gender thrown in to show that it should function exactly the same as PIVOT

MATCH (s:Student)
WITH s.city as city, AVG(s.score) as score, s.gender as gender
WITH gender, COLLECT([city, score]) as key_value_pair_list
// Create city_name:score map for pivot
WITH gender, apoc.map.fromPairs(key_value_pair_list) YIELD value
// Convert map values to column by key
RETURN gender, value['New York'] as 'New York', value['Huston'] as 'Huston'
Tezra
  • 8,463
  • 3
  • 31
  • 68