Cypher - 订购前的不同结果

Cypher - Distinct results before ordering them

我是 Cypher 的新手,如果我错过了合乎逻辑的解决方案,我深表歉意。感谢您的帮助:)

我使用的图表制作了一个研究出版物网络及其出版日期,并与出版物中提到的分子建立了联系。 我试图大致了解每年添加的新分子数量。

我试过了:

MATCH (p:Publication)-->(m:Molecule)
RETURN DISTINCT datetime(p.releasedDate).year AS release, count(m) ORDER BY release 

这个 returns 一个包含每年不同分子数量的列表,但第一年的分子仍然有可能(并且很可能)在第二年,因为顺序似乎在 distinct 之前应用。

有没有办法先列出所有不同的分子,然后按年份排序?

再次感谢您的帮助。如果需要更多信息,请告诉我。

我试着写了几个查询来说明一些选项:

// Number of molecule mentions per publication year
MATCH (p:Publication)-->(:Molecule)
WITH datetime(p.releasedDate).year AS release, count(*)


// Number of unique molecules mentioned per publication year
MATCH (p:Publication)-->(m:Molecule)
WITH datetime(p.releasedDate).year AS release, count(distinct m)


// Refactor so we know the year a molecule first mentioned
MATCH (p:Publication)-->(m:Molecule)
WITH m, min(datetime(p.releasedDate).year) as yearIntroduced


// Number of new molecules this year, and how many times where they mentioned
MATCH (p:Publication)-->(m:Molecule{yearIntroduced:2021})
RETURN m.name, count(*) as numberOfTimesMentioned


// Same but without refactoring
MATCH (p:Publication)-->(m:Molecule)
WITH m, min(datetime(p.releasedDate).year) as yearIntroduced
WHERE yearIntroduced=2021
MATCH (p:Publication)-->(m)
RETURN m.name, count(*) as numberOfTimesMentioned

希望对您有所帮助!

Cypher 手册中的聚合函数:https://neo4j.com/docs/cypher-manual/current/functions/aggregating/