我们如何在Apache Age中使用筛选器进行条件筛选?

huangapple go评论74阅读模式
英文:

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(&#39;sales&#39;, $$
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(&#39;test_graph&#39;, $$
CREATE (u:Person {name: &#39;John&#39;})
CREATE (v:Person {name: &#39;Jake&#39;})
CREATE (k:Person {name: &#39;Alice&#39;})
CREATE (l:Person {name: &#39;Anna&#39;}) $$)
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 (&#39;test_graph&#39;, $$
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 (&#39;test_graph&#39;, $$
MATCH (u:Person) 
WHERE u.name = &#39;John&#39;
RETURN u $$)
as (u agtype);

Here this will return only the nodes that have the property name and it equals &#39;John&#39;.

答案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(&#39;test&#39;, $$ MATCH(n: User) WHERE n.name &lt;&gt; &#39;hossam&#39; 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(&#39;your_graph&#39;, $$
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(&#39;sale&#39;,$$ 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;

huangapple
  • 本文由 发表于 2023年6月18日 21:29:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定