Created
October 23, 2014 11:58
-
-
Save alacambra/966a8b967f25724808a4 to your computer and use it in GitHub Desktop.
complete cypher with foreach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Date ranges in a month | |
WITH | |
range(1, 28) as Days_28, | |
range(1, 29) as Days_29, | |
range(1, 30) as Days_30, | |
range(1, 31) as Days_31 | |
//Assign months date ranges | |
WITH | |
Days_31 as January, | |
Days_28 as February, | |
Days_29 as Leap_February, | |
Days_31 as March, | |
Days_30 as April, | |
Days_31 as May, | |
Days_30 as June, | |
Days_31 as July, | |
Days_31 as August, | |
Days_30 as September, | |
Days_31 as October, | |
Days_30 as November, | |
Days_31 as December | |
//Mapping months to days keys - num key could be avoided by changing FOREACH to: 'FOREACH(m IN range(1,length(year.months)) |' or 'FOREACH(m IN range(1, 12) |' | |
WITH [ | |
{num: 1, days: January}, | |
{num: 2, days: February}, | |
{num: 3, days: March}, | |
{num: 4, days: April}, | |
{num: 5, days: May}, | |
{num: 6, days: June}, | |
{num: 7, days: July}, | |
{num: 8, days: August}, | |
{num: 9, days: September}, | |
{num: 10, days: October}, | |
{num: 11, days: November}, | |
{num: 12, days: December} | |
] as regular_year, Leap_February | |
//Create leap year | |
WITH [regular_year[0] , {num: 2, days: Leap_February} , regular_year[2..11]] AS leap_year, regular_year | |
//Years to create are stored in years collection - anyway to move this to the top without having to add it to the end of every WITH clause prior? | |
WITH [1996] as years,leap_year,regular_year | |
//Check if year is a leap year, if so map to leap_year, if not map regular_year | |
WITH [year IN years | CASE WHEN (year%4=0 AND NOT year%100=0) THEN {year:year, months:leap_year} WHEN (year%4=0 AND year%100=0 AND year%400=0) THEN {year:year, months:leap_year} ELSE {year:year, months:regular_year} END] AS yearMap | |
//Create nodes/relationships for years/months/days | |
FOREACH(year IN yearMap | | |
MERGE (thisYear: Year {year: year.year}) | |
FOREACH(m IN year.months | | |
MERGE (thisMonth :Month { month: m.num, year: year.year }) | |
CREATE UNIQUE (thisYear)-[:HAS_MONTH]->(thisMonth) | |
MERGE (firstDay :Day { day: 1, month: m.num, year: year.year }) | |
CREATE UNIQUE (thisMonth)-[:FIRST]->(firstDay) | |
//This FOREACH line is causing a problem, if replace m.num with an integer value it works, but then only processes that month | |
FOREACH (d IN TAIL((year.months[m.num]).days) | | |
MERGE (thisDay: Day { day: d, month: m.num, year: year.year }) | |
MERGE (lastDay: Day { day: d - 1, month: m.num, year: year.year }) | |
CREATE UNIQUE(lastDay)-[:NEXT]->(thisDay) | |
) | |
MERGE (lastDay: Day { day: last((year.months[m.num]).days), month: m.num, year: year.year }) | |
CREATE UNIQUE (thisMonth)-[:LAST]->(lastDay) | |
) | |
) | |
match (n1:task) | |
with collect(n1) as tasks | |
foreach(t1 in tasks| | |
foreach(t2 in tasks| | |
MERGE (t1)-[r:linked {bl:"assignee"}]->(t2) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment