1

I'm trying to model when British football managers were hired and fired by their clubs and came up with the following model:

Tenure-[:HIRED_ON]-date1
Tenure-[:FIRED_ON]-date2
Tenure-[:MANAGER]-manager
Tenure-[:TEAM]-team`

I've got a dummy data set here -> http://console.neo4j.org/?id=nmeaef

The data modelling style is taken from -> http://docs.neo4j.org/chunked/snapshot/cypher-cookbook-path-tree.html#cookbook-return-partly-shared-path-ranges

What I'd like to do is find who the manager for a team was on a certain date and if they didn't have a manager then it should indicate that.

So on the 2nd January in my data set both clubs have a manager as neither has been fired yet and this query works ok:

START team = node:node_auto_index('name:"Chelsea" name:"Manchester United"'), 
      startDate = node:node_auto_index(name="2nd") 
MATCH startDate<-[:NEXT*0..]-day 
WITH team, startDate, COLLECT(day) AS dates 
MATCH startDate<-[:NEXT*0..]-day<-[hire:HIRED_ON]-tenure-[:MANAGER]->manager, 
      tenure-[:TEAM]->team, tenure-[?:FIRED_ON]-dateFired 
WHERE dateFired IS NULL OR NOT dateFired IN dates 
RETURN team.name, manager.name, tenure, hire.date, dateFired

But on the 5th January it doesn't work as I want because by that date the Chelsea manager has been fired so I want to indicate that they don't have a manager but instead what happens is I just don't get a row for Chelsea because I've filtered it out with the WHERE clause.

But without the WHERE clause it suggested Mourinho is manager which isn't right either since he was fired on the 3rd January.

Any ideas on how I can do what I want? Should I be modelling it differently to do that?

Cheers, Mark

4

2 回答 2

2

这是 2.0 方式,带有 case/when:

START team = node:node_auto_index('name:"Chelsea" name:"Manchester United"'), startDate = node:node_auto_index(name="5th") 
MATCH startDate<-[:NEXT*0..]-day 
WITH team, startDate, COLLECT(day) AS dates 
MATCH startDate<-[:NEXT*0..]-day<-[hire:HIRED_ON]-tenure-[:MANAGER]->manager, tenure-[:TEAM]->team, tenure-[?:FIRED_ON]-dateFired 
RETURN team.name, 
  CASE WHEN dateFired is null THEN manager.name ELSE null END as managerName, 
  CASE WHEN dateFired is null THEN tenure ELSE null END as tenure, 
  CASE WHEN dateFired is null THEN hire.date ELSE null END as hireDate

也可能有一种使用 WITH 的方法,但这很容易。

于 2013-06-09T00:40:06.777 回答
1

好主意 Freeman 先生,也许现在是时候将图表移植到使用 2.0 了!

我在这里创建了一个版本-> http://console-test.neo4j.org/?id=z3i6ns

不得不稍微改变查询来做我想做的事:

START team = node:node_auto_index('name:"Chelsea" name:"Manchester United"'), 
      startDate = node:node_auto_index(name="2nd") 
MATCH startDate<-[:NEXT*0..]-day 
WITH team, startDate, COLLECT(day) AS dates 
MATCH startDate<-[:NEXT*0..]-day<-[hire:HIRED_ON]-tenure-[:MANAGER]->manager, 
      dateFired<-[?:FIRED_ON]-tenure-[:TEAM]->team
RETURN team.name,    
   CASE WHEN dateFired is null THEN manager.name 
        WHEN dateFired IN dates THEN null 
        ELSE manager.name END as managerName,    
   CASE WHEN dateFired is null THEN tenure 
        WHEN dateFired IN dates THEN null 
        ELSE tenure END as tenure,    
   CASE WHEN dateFired is null THEN hire.date 
        WHEN dateFired IN dates THEN null 
        ELSE hire.date END as hireDate 

执行查询似乎需要相当长的时间——我将在一个真实的数据集上进行尝试——http: //en.wikipedia.org/wiki/List_of_Premier_League_managers——所以看看查询的内容会很有趣速度就像那里。

于 2013-06-09T09:53:29.607 回答