在密码查询中水平合并结果
Merge results horizontally in cypher queries
我有两个查询,每个查询 returns 一个 table 三列。两个 table 的前两列相似,第三列名称不同。请参阅以下内容:
MATCH (n:PI)-[r:SIMILAR_STUDY]->(p:PI) where ID(n) = 3162 RETURN ID(n) , ID(p),r.score as score1 ORDER BY r.score DESC LIMIT 5
ID(n) ID(p) score1
3162 4978 0.6666666666666666
3162 4054 0.6363636363636364
3162 4194 0.6
3162 4980 0.5555555555555556
3162 3661 0.5555555555555556
并且:
MATCH (n:PI)-[r:SIMILAR_SITE]->(p:PI) where ID(n) = 3162 RETURN ID(n) ,ID(p) ,r.score as score2 ORDER BY r.score DESC LIMIT 5
ID(n) ID(p) score2
3162 4980 0.6
3162 3183 0.5
3162 4612 0.42857142857142855
3162 4595 0.4
3162 6926 0.4
如何将这些结果组合成具有 4 列 ID(n)
、ID(p)
、score1
、score2
的唯一 table,以及如何添加一个额外的平均分数列?
这是预期的结果:
ID(n) ID(p) score1 score2 avg
3162 4980 0.5555555555555556 0.6 0.57
3162 3183 0 0.5 0.25
第二次编辑:
对两者使用一个查询怎么样?类似于:
MATCH (n:PI)-[:SIMILAR_STUDY|SIMILAR_SITE]->(t:PI)
WHERE ID(n) = 3162
WITH distinct t, n
OPTIONAL MATCH (t)<-[r_ste:SIMILAR_STUDY]-(n)
WITH n, t, COALESCE(r_ste.score, 0) as r1
OPTIONAL MATCH (t)<-[r_se:SIMILAR_SITE]-(n)
WITH ID(n) as n, ID(t) as t, r1, COALESCE(r_se.score, 0) as r2
WITH n, t, r1, r2, ((r1 + r2)/2) as avg
RETURN n, t, r1, r2, avg
OPTIONAL MATCH
允许我们处理没有匹配的情况,COALESCE
允许我们用0替换null
。
要查看示例,您可以使用:
MERGE (a:PI{ID: 3162})
MERGE (b:PI{ID: 4978})
MERGE (c:PI{ID: 4054})
MERGE (d:PI{ID: 4194})
MERGE (e:PI{ID: 4980})
MERGE (f:PI{ID: 3661})
MERGE (g:PI{ID: 3183})
MERGE (h:PI{ID: 4612})
MERGE (i:PI{ID: 4595})
MERGE (j:PI{ID: 6926})
MERGE (a)-[:SIMILAR_STUDY{score: 0.6666666}]-(b)
MERGE (a)-[:SIMILAR_STUDY{score: 0.63636}]-(c)
MERGE (a)-[:SIMILAR_STUDY{score: 0.6}]-(d)
MERGE (a)-[:SIMILAR_STUDY{score: 0.55555}]-(e)
MERGE (a)-[:SIMILAR_STUDY{score: 0.55555}]-(f)
MERGE (a)-[:SIMILAR_SITE{score: 0.6}]-(e)
MERGE (a)-[:SIMILAR_SITE{score: 0.5}]-(g)
MERGE (a)-[:SIMILAR_SITE{score: 0.428}]-(h)
MERGE (a)-[:SIMILAR_SITE{score: 0.4}]-(i)
MERGE (a)-[:SIMILAR_SITE{score: 0.4}]-(j)
和运行:
MATCH (n:PI)-[:SIMILAR_STUDY|SIMILAR_SITE]->(t:PI)
WHERE n.ID = 3162
WITH distinct t, n
OPTIONAL MATCH (t)<-[r_ste:SIMILAR_STUDY]-(n)
WITH n, t, COALESCE(r_ste.score, 0) as r1
OPTIONAL MATCH (t)<-[r_se:SIMILAR_SITE]-(n)
WITH n.ID as n, t.ID as t, r1, COALESCE(r_se.score, 0) as r2
WITH n, t, r1, r2, ((r1 + r2)/2) as avg
RETURN n, t, r1, r2, avg
其中 ID 是节点上的键。使用我添加的示例数据,我得到:
╒════╤════╤═════════╤═════╤══════════════════╕
│"n" │"t" │"r1" │"r2" │"avg" │
╞════╪════╪═════════╪═════╪══════════════════╡
│3162│4595│0 │0.4 │0.2 │
├────┼────┼─────────┼─────┼──────────────────┤
│3162│4054│0.63636 │0 │0.31818 │
├────┼────┼─────────┼─────┼──────────────────┤
│3162│4978│0.6666666│0 │0.3333333 │
├────┼────┼─────────┼─────┼──────────────────┤
│3162│4980│0.55555 │0.6 │0.5777749999999999│
├────┼────┼─────────┼─────┼──────────────────┤
│3162│3183│0 │0.5 │0.25 │
├────┼────┼─────────┼─────┼──────────────────┤
│3162│4612│0 │0.428│0.214 │
├────┼────┼─────────┼─────┼──────────────────┤
│3162│6926│0 │0.4 │0.2 │
├────┼────┼─────────┼─────┼──────────────────┤
│3162│3661│0.55555 │0 │0.277775 │
├────┼────┼─────────┼─────┼──────────────────┤
│3162│4194│0.6 │0 │0.3 │
└────┴────┴─────────┴─────┴──────────────────┘
我有两个查询,每个查询 returns 一个 table 三列。两个 table 的前两列相似,第三列名称不同。请参阅以下内容:
MATCH (n:PI)-[r:SIMILAR_STUDY]->(p:PI) where ID(n) = 3162 RETURN ID(n) , ID(p),r.score as score1 ORDER BY r.score DESC LIMIT 5
ID(n) ID(p) score1
3162 4978 0.6666666666666666
3162 4054 0.6363636363636364
3162 4194 0.6
3162 4980 0.5555555555555556
3162 3661 0.5555555555555556
并且:
MATCH (n:PI)-[r:SIMILAR_SITE]->(p:PI) where ID(n) = 3162 RETURN ID(n) ,ID(p) ,r.score as score2 ORDER BY r.score DESC LIMIT 5
ID(n) ID(p) score2
3162 4980 0.6
3162 3183 0.5
3162 4612 0.42857142857142855
3162 4595 0.4
3162 6926 0.4
如何将这些结果组合成具有 4 列 ID(n)
、ID(p)
、score1
、score2
的唯一 table,以及如何添加一个额外的平均分数列?
这是预期的结果:
ID(n) ID(p) score1 score2 avg
3162 4980 0.5555555555555556 0.6 0.57
3162 3183 0 0.5 0.25
第二次编辑:
对两者使用一个查询怎么样?类似于:
MATCH (n:PI)-[:SIMILAR_STUDY|SIMILAR_SITE]->(t:PI)
WHERE ID(n) = 3162
WITH distinct t, n
OPTIONAL MATCH (t)<-[r_ste:SIMILAR_STUDY]-(n)
WITH n, t, COALESCE(r_ste.score, 0) as r1
OPTIONAL MATCH (t)<-[r_se:SIMILAR_SITE]-(n)
WITH ID(n) as n, ID(t) as t, r1, COALESCE(r_se.score, 0) as r2
WITH n, t, r1, r2, ((r1 + r2)/2) as avg
RETURN n, t, r1, r2, avg
OPTIONAL MATCH
允许我们处理没有匹配的情况,COALESCE
允许我们用0替换null
。
要查看示例,您可以使用:
MERGE (a:PI{ID: 3162})
MERGE (b:PI{ID: 4978})
MERGE (c:PI{ID: 4054})
MERGE (d:PI{ID: 4194})
MERGE (e:PI{ID: 4980})
MERGE (f:PI{ID: 3661})
MERGE (g:PI{ID: 3183})
MERGE (h:PI{ID: 4612})
MERGE (i:PI{ID: 4595})
MERGE (j:PI{ID: 6926})
MERGE (a)-[:SIMILAR_STUDY{score: 0.6666666}]-(b)
MERGE (a)-[:SIMILAR_STUDY{score: 0.63636}]-(c)
MERGE (a)-[:SIMILAR_STUDY{score: 0.6}]-(d)
MERGE (a)-[:SIMILAR_STUDY{score: 0.55555}]-(e)
MERGE (a)-[:SIMILAR_STUDY{score: 0.55555}]-(f)
MERGE (a)-[:SIMILAR_SITE{score: 0.6}]-(e)
MERGE (a)-[:SIMILAR_SITE{score: 0.5}]-(g)
MERGE (a)-[:SIMILAR_SITE{score: 0.428}]-(h)
MERGE (a)-[:SIMILAR_SITE{score: 0.4}]-(i)
MERGE (a)-[:SIMILAR_SITE{score: 0.4}]-(j)
和运行:
MATCH (n:PI)-[:SIMILAR_STUDY|SIMILAR_SITE]->(t:PI)
WHERE n.ID = 3162
WITH distinct t, n
OPTIONAL MATCH (t)<-[r_ste:SIMILAR_STUDY]-(n)
WITH n, t, COALESCE(r_ste.score, 0) as r1
OPTIONAL MATCH (t)<-[r_se:SIMILAR_SITE]-(n)
WITH n.ID as n, t.ID as t, r1, COALESCE(r_se.score, 0) as r2
WITH n, t, r1, r2, ((r1 + r2)/2) as avg
RETURN n, t, r1, r2, avg
其中 ID 是节点上的键。使用我添加的示例数据,我得到:
╒════╤════╤═════════╤═════╤══════════════════╕
│"n" │"t" │"r1" │"r2" │"avg" │
╞════╪════╪═════════╪═════╪══════════════════╡
│3162│4595│0 │0.4 │0.2 │
├────┼────┼─────────┼─────┼──────────────────┤
│3162│4054│0.63636 │0 │0.31818 │
├────┼────┼─────────┼─────┼──────────────────┤
│3162│4978│0.6666666│0 │0.3333333 │
├────┼────┼─────────┼─────┼──────────────────┤
│3162│4980│0.55555 │0.6 │0.5777749999999999│
├────┼────┼─────────┼─────┼──────────────────┤
│3162│3183│0 │0.5 │0.25 │
├────┼────┼─────────┼─────┼──────────────────┤
│3162│4612│0 │0.428│0.214 │
├────┼────┼─────────┼─────┼──────────────────┤
│3162│6926│0 │0.4 │0.2 │
├────┼────┼─────────┼─────┼──────────────────┤
│3162│3661│0.55555 │0 │0.277775 │
├────┼────┼─────────┼─────┼──────────────────┤
│3162│4194│0.6 │0 │0.3 │
└────┴────┴─────────┴─────┴──────────────────┘