Neo4j Graph Database Fundamentals · 课时

聚合与投影

了解如何使用聚合函数(例如 COUNT、SUM),并将数据投影为自定义格式以供分析

第 2 / 4 课11 个步骤

聚合与投影 是 CoddyKit 上的免费 Neo4j Graph Database Fundamentals 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Neo4j Graph Database Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Neo4j Graph Database Fundamentals 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Analyze Your Graph Data

Welcome to Aggregation and Projections! In graph databases, you often need to do more than just find data; you need to summarize it or shape it for analysis.

Aggregation means calculating a single result from multiple items, like counting all nodes or finding the average age. Projection is about selecting and formatting the data you want to see in your results.

Selecting Data with RETURN

The RETURN clause is how you specify exactly what data you want to see from your query. You can return nodes, relationships, or their specific properties. Let's add some example data first, then try a basic projection.

CREATE (p1:Person {name: 'Alice', age: 30})
CREATE (p2:Person {name: 'Bob', age: 25})
CREATE (p3:Person {name: 'Charlie', age: 30})
CREATE (m1:Movie {title: 'Inception', releaseYear: 2010})
CREATE (m2:Movie {title: 'Dunkirk', releaseYear: 2017})
CREATE (m3:Movie {title: 'Interstellar', releaseYear: 2014})

MATCH (p:Person)
RETURN p.name, p.age

Customizing Output with AS

The default property names might not always be clear in your results. The AS keyword lets you rename returned items to make your query output more readable and descriptive. This is part of projection.

MATCH (m:Movie)
RETURN m.title AS FilmTitle, m.releaseYear AS YearReleased

Counting All Items with COUNT(*)

Aggregation functions help you summarize data. One of the most common is COUNT(*), which tells you the total number of items (nodes, relationships, or paths) found by your MATCH pattern.

MATCH (p:Person)
RETURN COUNT(*) AS TotalPeopleCount

Counting Unique Values

What if you only want to count the unique values for a specific property? Use COUNT(DISTINCT variable.property). This is great for finding how many different ages, titles, or categories exist in your graph.

MATCH (p:Person)
RETURN COUNT(DISTINCT p.age) AS UniqueAgesCount

Summing and Averaging Numbers

For numeric properties, you can calculate sums and averages. SUM() adds up all values, and AVG() computes their mean. These functions are very powerful for numerical analysis.

MATCH (p:Person)
RETURN SUM(p.age) AS TotalAgeSum, AVG(p.age) AS AverageAge

Finding Extremes with MIN/MAX

The MIN() and MAX() functions let you find the smallest and largest values for a property within your matched data. This is useful for identifying outliers or understanding data ranges.

MATCH (p:Person)
RETURN MIN(p.age) AS YoungestPersonAge, MAX(p.age) AS OldestPersonAge

Grouping into Lists with COLLECT

The COLLECT() function gathers all values of a specified property into a list. This is often used with grouping (which we'll cover later) but can also simply return all collected items in a list.

MATCH (p:Person)
RETURN COLLECT(p.name) AS AllPersonNames

Structuring Results with Maps

You can project complex, structured data using map projections. This allows you to create custom objects (maps) in your results, combining multiple properties and even calculated values into a single output column.

MATCH (p:Person)
RETURN {fullName: p.name, yearsOld: p.age, isAdult: p.age >= 18} AS PersonSummary

Quick Aggregation & Projection Check

Let's test your understanding of Cypher aggregation and projection. Choose all statements that correctly apply these concepts.

Recap: Aggregating & Projecting

Great job! You've learned how to summarize and shape your graph data using Cypher.

  • Projections with RETURN allow you to select specific data, rename it with AS, or create structured maps.
  • Aggregation functions like COUNT(), SUM(), AVG(), MIN(), MAX(), and COLLECT() help you analyze and summarize large datasets into meaningful insights.

These techniques are fundamental for extracting valuable information from your Neo4j graphs!

免费开始

用 AI 导师学习 Neo4j Graph Database Fundamentals — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「聚合与投影」课时是免费的吗?

是的 — 「聚合与投影」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Neo4j Graph Database Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Neo4j Graph Database Fundamentals 课程共包含 4 节课。

「聚合与投影」这节课中我会学到什么?

了解如何使用聚合函数(例如 COUNT、SUM),并将数据投影为自定义格式以供分析 你通过在浏览器中直接运行的动手代码来练习 Neo4j Graph Database Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Neo4j Graph Database Fundamentals 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Neo4j Graph Database Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「聚合与投影」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Neo4j Graph Database Fundamentals 课中编写并运行代码吗?

能。每节 Neo4j Graph Database Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 筛选与排序结果
  2. 聚合与投影
  3. 更新与删除图数据
  4. 使用 WITH 和 UNION 组合查询
← 返回 Neo4j Graph Database Fundamentals