密码:分层排序 [英] Cypher: Hierarchical Sorting

查看:127
本文介绍了密码:分层排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Cypher的新手。我能够创建一个地理网络(从世界到大洲再到国家再到地区)及其人口。



您可以使用此命令来重现它,或检查控制台的链接:



您可以看到,美国介于欧洲,中东和非洲与欧洲之间。在显示结果之前,是否可以先按照 BELONG_TO层次结构进行排序?另外,尽管我所有的数据最多只有3个小数位,但我不明白为什么SUM()命令返回疯狂的小数位。



感谢您的帮助

解决方案

当然,您应该能够按路径的长度进行排序:

  MATCH path =(n:Population)-[r:LIVE_IN]->(g1:Geo)-[:BELONG_TO * 0 ..]->( g2:Geo)
返回g2.name AS地理位置,SUM(toFloat(n.amount))AS人口
按长度(路径)订购ASC,人口DESC

我认为您由于浮点数学。我认为您应该能够做到:

  ROUND(SUM(toFloat(n.amount))* 1000.0)/ 1000.0 

编辑:



您是对的,您需要按此顺序添加路径长度:

  MATCH path =(n:Population )-[r:LIVE_IN]->(g1:Geo)-[:BELONG_TO * 1 ..]->(g2:Geo)
返回g1.name AS地理位置,SUM(toFloat(n.amount ))AS人口,长度(路径)AS路径长度
OR BY长度(路径)ASC,人口DESC

是的,您绝对正确,您的道路必须以 g2 作为世界结尾。您可以通过匹配(g2:Geo {name:'World'})来实现此目的,类似于我在评论中的建议,否则将来是否会发生要拥有更多的根节点(也许我们将在月球或火星上定居!),您可以这样做:

  MATCH path = (n:人口)-[r:LIVE_IN]->(g1:Geo)-[:BELONG_TO *]->(g2:Geo)
WHERE NOT((g2:Geo)-[:BELONG_TO] ->())
返回g1.name AS地理位置,SUM(toFloat(n.amount))AS总体,长度(路径)AS路径长度
ORDER BY长度(路径)ASC,总体DESC

这意味着我们只想要g2不属于任何东西的路径。否则, g2 可以是例如欧洲


I am new to Cypher. I was able to create a network of geographies (from the World to continents to countries to regions) and their population.

You can reproduce it with this command or check the link to the console: http://console.neo4j.org/r/dkh90c

CREATE (n1:Geo {name:'World'}),
(n2:Geo {name:'EMEA'})-[:BELONG_TO]->(n1),
(n4:Geo {name:'NORAM'})-[:BELONG_TO]->(n1),
(n5:Geo {name:'Middle East'})-[:BELONG_TO]->(n2),
(n6:Geo {name:'Africa'})-[:BELONG_TO]->(n2),
(n7:Geo {name:'Europe'})-[:BELONG_TO]->(n2),
(n8:Geo {name:'France'})-[:BELONG_TO]->(n7),
(n9:Geo {name:'Germany'})-[:BELONG_TO]->(n7),
(n10:Geo {name:'Italy'})-[:BELONG_TO]->(n7),
(n11:Geo {name:'United Kingdom'})-[:BELONG_TO]->(n7),
(n12:Geo {name:'England'})-[:BELONG_TO]->(n11),
(n13:Geo {name:'Scotland'})-[:BELONG_TO]->(n11),
(n14:Geo {name:'Wales'})-[:BELONG_TO]->(n11),
(n15:Geo {name:'Northern Ireland'})-[:BELONG_TO]->(n11),
(n16:Geo {name:'United Arab Emirates'})-[:BELONG_TO]->(n5),
(n17:Geo {name:'South Africa'})-[:BELONG_TO]->(n6),
(n18:Geo {name:'Canada'})-[:BELONG_TO]->(n4),
(n19:Geo {name:'United States of America'})-[:BELONG_TO]->(n4),
(n20:Geo {name:'Mexico'})-[:BELONG_TO]->(n4),

(:Population {year:'2014',amount:66.1})-[:LIVE_IN]->(n8),
(:Population {year:'2014',amount:81.2})-[:LIVE_IN]->(n9),
(:Population {year:'2013',amount:59.83})-[:LIVE_IN]->(n10),
(:Population {year:'2011',amount:53.01})-[:LIVE_IN]->(n12),
(:Population {year:'2011',amount:5.295})-[:LIVE_IN]->(n13),
(:Population {year:'2011',amount:3.063})-[:LIVE_IN]->(n14),
(:Population {year:'2011',amount:1.811})-[:LIVE_IN]->(n15),
(:Population {year:'2013',amount:9.346})-[:LIVE_IN]->(n16),
(:Population {year:'2013',amount:52.98})-[:LIVE_IN]->(n17),
(:Population {year:'2013',amount:35.16})-[:LIVE_IN]->(n18),
(:Population {year:'2014',amount:318.9})-[:LIVE_IN]->(n19),
(:Population {year:'2013',amount:122.3})-[:LIVE_IN]->(n20)

I am also able to calculate the total population for each geography with this command:

MATCH (n:Population)-[r:LIVE_IN]->(g1:Geo)-[:BELONG_TO*0..]->(g2:Geo)
RETURN g2.name AS Geography, SUM(toFloat(n.amount)) AS Population
ORDER BY Population DESC

However, I am not happy with the sorting of the results:

As you can see, the United States of America are inserted between EMEA and Europe. Is there a way to first sort by the 'BELONG_TO' hierarchy before displaying the results? Also, despite all my data having only up to 3 decimals, I don't understand why the SUM() command return a crazy number of decimals.

Thanks for your help.

解决方案

Sure, you should be able to sort by the length of the path:

MATCH path=(n:Population)-[r:LIVE_IN]->(g1:Geo)-[:BELONG_TO*0..]->(g2:Geo)
RETURN g2.name AS Geography, SUM(toFloat(n.amount)) AS Population
ORDER BY length(path) ASC, Population DESC

I think that you're getting lots of decimals because of floating point math. I think you should be able to do:

ROUND(SUM(toFloat(n.amount)) * 1000.0) / 1000.0

EDIT:

You're right, you need to add the path length to order by it:

MATCH path=(n:Population)-[r:LIVE_IN]->(g1:Geo)-[:BELONG_TO*1..]->(g2:Geo)
RETURN g1.name AS Geography, SUM(toFloat(n.amount)) AS Population, length(path) AS path_length
ORDER BY length(path) ASC, Population DESC 

And yes, you're definitely right that your path needs to end with g2 being the world. You can do this by matching (g2:Geo {name: 'World'}) similar to how I suggested in the comments, or if you, in the future, will happen to have more root nodes (perhaps we'll colonize the moon or Mars!), you could do this:

MATCH path=(n:Population)-[r:LIVE_IN]->(g1:Geo)-[:BELONG_TO*]->(g2:Geo)
WHERE NOT((g2:Geo)-[:BELONG_TO]->())
RETURN g1.name AS Geography, SUM(toFloat(n.amount)) AS Population, length(path) AS path_length
ORDER BY length(path) ASC, Population DESC 

That means that we only want paths where g2 doesn't belong to anything. Otherwise g2 can be, for example, Europe

这篇关于密码:分层排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆