It was more of a exercise about learning what I can do and how with Cypher.
If you try the query with multiple years you could try 1999, 2000, 2004,
they should all trigger a different CASE.
The full query is on StackOverflow via the link, this is the query with
corrections:
//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}] + filter(month in
regular_year WHERE month.num>2) 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 [2000] 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-1]).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-1]).days), month: m.num,
year: year.year })
CREATE UNIQUE (thisMonth)-[:LAST]->(lastDay)
)
)
--
You received this message because you are subscribed to the Google Groups
"Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.