2
我在的Neo4j下面的查詢其採用了UNION
獲得來自Neo4j的聯合的結果平均值
MATCH (u:User {userId:'1'})-[dw:DIRECTOR_WEIGHT]->(d:Person)-[:DIRECTED]->(m:Movie)
WITH m, avg(dw.weight) AS mean_dw, 0 AS mean_aw, 0 AS mean_gw
WHERE m.title = 'Bambi'
RETURN m.title, mean_dw, mean_aw, mean_gw, mean_dw + mean_aw + mean_gw AS total
UNION
MATCH (u:User {userId:'1'})-[aw:ACTOR_WEIGHT]->(a:Person)-[:ACTED_IN]->(m:Movie)
WITH m, 0 AS mean_dw, avg(aw.weight) AS mean_aw, 0 AS mean_gw
WHERE m.title = 'Bambi'
RETURN m.title, mean_dw, mean_aw, mean_gw, mean_dw + mean_aw + mean_gw AS total
UNION
MATCH (u:User {userId:'1'})-[gw:GENRE_WEIGHT]->(g:Genre)<-[:GENRE]-(m:Movie)
WITH m, 0 AS mean_dw, 0 AS mean_aw, avg(gw.weight) AS mean_gw
WHERE m.title = 'Bambi'
RETURN m.title, mean_dw, mean_aw, mean_gw, mean_dw + mean_aw + mean_gw AS total
,得到以下結果:
╒═════════╤═══════════════╤════════════════╤═════════════════╤═════════════════╕
│"m.title"│"mean_dw" │"mean_aw" │"mean_gw" │"total" │
╞═════════╪═══════════════╪════════════════╪═════════════════╪═════════════════╡
│"Bambi" │7.2916666666667│"0" │"0" │7.2916666666667 │
├─────────┼───────────────┼────────────────┼─────────────────┼─────────────────┤
│"Bambi" │"0" │0.45322110715442│"0" │0.45322110715442 │
├─────────┼───────────────┼────────────────┼─────────────────┼─────────────────┤
│"Bambi" │"0" │"0" │9.289617486338933│9.289617486338933│
└─────────┴───────────────┴────────────────┴─────────────────┴─────────────────┘
我的問題是「總」不做我想讓它做的,因爲我只希望每部電影單總(三非零權,即合計:7.29 + 0.45 + 9.28), 但我不能找到一種方法,使用這種再返回的結果。也就是說,我想能夠說說得到的mean_dw
,mean_aw
工會像
RETURN m.title, sum(total)
或
RETURN m.title, mean_dw + mean_aw + mean_gw
後,和mean_gw
分別
非常感謝你。第二版有效;第一個版本給出了一個不同的(和錯誤的)結果,但我無法從代碼中找出原因。有任何想法嗎? –
沒有想法,對於樣本數據,這兩個查詢都爲我返回相同的結果。對於第一個查詢,你可能想'RETURN value'看到每次相加在一起之前的手段,看看是否符合你的期望。 – InverseFalcon