英文:
How can we use filter in Apache age for condition?
问题
在PostgreSQL中,我们可以使用过滤器,例如-
SELECT column_name,
SUM(sales) FILTER (WHERE year = 2022) AS total_sales_2022,
SUM(sales) FILTER (WHERE year = 2023) AS total_sales_2023
FROM sales_table
GROUP BY column_name;
请问如何在Apache Age中实现这个功能?有人可以帮我吗?
英文:
In postgreSQL, we can use filter such as-
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
SELECT column_name,
SUM(sales) FILTER (WHERE year = 2022) AS total_sales_2022,
SUM(sales) FILTER (WHERE year = 2023) AS total_sales_2023
FROM sales_table
GROUP BY column_name;
How can we implement this in Apache age? Anyone help me plz.
答案1
得分: 2
根据文档,
> 为了计算汇总数据,Cypher提供了聚合功能,类似于SQL的GROUP BY。
等效的Cypher查询如下:
SELECT * FROM cypher('sales', $$
MATCH (product) WHERE product.year = 2022 WITH product.name as name, count(*) as c, SUM(product.sales) as
sales
RETURN name, sales
$$) AS (name agtype, sales agtype);
这个查询将匹配产品,根据年份进行过滤,并使用COUNT
(类似于GROUP BY
)进行聚合。然后,您可以应用SUM
聚合函数来计算每个产品的总销售额。
英文:
As per the Doc,
> To calculate aggregated data, Cypher offers aggregation, analogous to SQL’s GROUP BY.
An equivalent cypher query would be:
SELECT * FROM cypher('sales', $$
MATCH (product) WHERE product.year = 2022 WITH product.name as name, count(*) as c, SUM(product.sales) as
sales
RETURN name, sales
$$) AS (name agtype, sales agtype);
This query will match for the products, filter on the year and apply aggregation using COUNT
(analogous to GROUP BY
). Then you can apply SUM
aggregation function to calculate the total sales for each product.
答案2
得分: 0
在apacheAGE中,我们不处理表格,而是处理节点和边。这些节点/边通过它们的标签和/或属性保存信息。例如:
SELECT * FROM cypher('test_graph', $$
CREATE (u:Person {name: 'John'})
CREATE (v:Person {name: 'Jake'})
CREATE (k:Person {name: 'Alice'})
CREATE (l:Person {name: 'Anna'}) $$)
AS (u agtype);
在这里,我们创建了4个不同的节点,标签为Person
,并具有各自的name
属性。如果我们想通过标签查找节点,可以这样做:
SELECT * FROM cypher ('test_graph', $$
MATCH (u:Person)
RETURN u $$)
as (u agtype);
这将返回我们图中所有属于Person
标签的节点。类似地,我们可以通过节点的属性进行筛选,例如:
SELECT * FROM cypher ('test_graph', $$
MATCH (u:Person)
WHERE u.name = 'John'
RETURN u $$)
as (u agtype);
这将仅返回具有属性name
且其值为'John'
的节点。
英文:
In apacheAGE we are not dealing with tables, but with nodes and edges.
Those nodes/edges hold information though their labels and/or properties. For example :
SELECT * FROM cypher('test_graph', $$
CREATE (u:Person {name: 'John'})
CREATE (v:Person {name: 'Jake'})
CREATE (k:Person {name: 'Alice'})
CREATE (l:Person {name: 'Anna'}) $$)
AS (u agtype);
Here we create 4 different nodes with Person
as the label, and their respective name
as property. If we want to find a node through its label we can do:
SELECT * FROM cypher ('test_graph', $$
MATCH (u:Person)
RETURN u $$)
as (u agtype);
This will return all the nodes in our graph that belong to the Person
label. Similarly we can filter through a property of a node for example:
SELECT * FROM cypher ('test_graph', $$
MATCH (u:Person)
WHERE u.name = 'John'
RETURN u $$)
as (u agtype);
Here this will return only the nodes that have the property name
and it equals 'John'
.
答案3
得分: 0
以下是已翻译的内容:
"Let's take an example, assume you want to return all users except for those name are hossam
, then you can do that by the following query:
testdb=# SELECT * FROM ag_catalog.cypher('test', $$ MATCH(n: User) WHERE n.name <> 'hossam' RETURN n$$) as (users agtype) ;
this will return all users whose name not equal to hossam
. You can notice that we applied a filter using the WHERE
clause which is used for that purpose.
I hope this give you a sense of how you can use filters in age."
英文:
Let's take an example, assume you want to return all users except for those name are hossam
, then you can do that by the following query:
testdb=# SELECT * FROM ag_catalog.cypher('test', $$ MATCH(n: User) WHERE n.name <> 'hossam' RETURN n$$) as (users agtype) ;
this will return all users whose name not equal to hossam
. You can notice that we applied a filter using the WHERE
clause which is used for that purpose.
I hope this give you a sense of how you can use filters in age.
答案4
得分: 0
在Apache AGE中,一旦您创建了包含所需节点和属性的图表,您可以以以下方式执行您的PostgreSQL查询:
SELECT *
FROM cypher('your_graph', $$
MATCH (u {year: 2022})
WITH sum(u.sales) as total_sales_2022
ORDER BY u.column_name
RETURN u.column_name, total_sales_2022
$$) AS (column_name agtype, total_sales agtype);
您可以访问AGE文档以了解有关AGE中的WITH、ORDER BY和RETURN子句的更多信息。
英文:
Once you've created your graph along with the required nodes and properties, your postgresql query can be executed this way in Apache AGE
SELECT *
FROM cypher('your_graph', $$
MATCH (u {year: 2022})
WITH sum(u.sales) as total_sales_2022
ORDER BY u.column_name
RETURN u.column_name, total_sales_2022
$$) AS (column_name agtype, total_sales agtype);
You can visit the AGE docs to learn more about the WITH, ORDER BY and RETURN clauses in AGE.
答案5
得分: 0
select * from cypher('sale',$$ MATCH (n:销售)
WHERE n.year = 2022 OR n.year = 2023
RETURN n.column_name AS 列名,
SUM(CASE WHEN n.year = 2022 THEN n.sales ELSE 0 END) AS 2022年总销售额,
SUM(CASE WHEN n.year = 2023 THEN n.sales ELSE 0 END) AS 2023年总销售额
GROUP BY n.column_name);
英文:
select *from cypher('sale',$$ MATCH (n:Sales)
WHERE n.year = 2022 OR n.year = 2023
RETURN n.column_name AS column_name,
SUM(CASE WHEN n.year = 2022 THEN n.sales ELSE 0 END) AS total_sales_2022,
SUM(CASE WHEN n.year = 2023 THEN n.sales ELSE 0 END) AS total_sales_2023
GROUP BY n.column_name);
答案6
得分: 0
在上述查询中,CASE语句充当了FILTER子句的替代。
英文:
SELECT column_name,
SUM(CASE WHEN year = 2022 THEN sales ELSE 0 END) AS total_sales_2022,
SUM(CASE WHEN year = 2023 THEN sales ELSE 0 END) AS total_sales_2023
FROM sales_table
GROUP BY column_name;
In the above query, the CASE statement acts as a replacement for the FILTER clause
答案7
得分: 0
可以使用FILTER子句与聚合函数结合起来实现所需的结果:
SELECT column_name,
SUM(sales) FILTER (WHERE year = 2022) AS total_sales_2022,
SUM(sales) FILTER (WHERE year = 2023) AS total_sales_2023
FROM sales_table
GROUP BY column_name;
英文:
We can use the FILTER clause in combination with the aggregate function to achieve the desired results:
SELECT column_name,
SUM(sales) FILTER (WHERE year = 2022) AS total_sales_2022,
SUM(sales) FILTER (WHERE year = 2023) AS total_sales_2023
FROM sales_table
GROUP BY column_name;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论