[英]similar to groupByKey() in Spark but using SQL queries
I trying to make我试图使
ID CATEGORY VALUE
'AAA' 'X' 123
'AAA' 'Y' 456
'BBB' 'X' 321
'BBB' 'Y' 654
into进入
ID VALUE_X VALUE_Y
'AAA' 123 456
'BBB' 321 654
using only SQL queries.仅使用 SQL 查询。 It is kind of similar to using groupByKey() in pyspark.这有点类似于在 pyspark 中使用 groupByKey()。
Is there a way to do this?有没有办法做到这一点?
Just use conditional aggregation.只需使用条件聚合。 One method is:一种方法是:
select id,
max(case when category = 'X' then value end) as x_value,
max(case when category = 'Y' then value end) as y_value
from t
group by id;
In Postgres, this would be phrased using the standard filter
clause:在 Postgres 中,这将使用标准filter
子句来表达:
select id,
max(value) filter (where category = 'X'),
max(value) filter (where category = 'Y')
from t
group by id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.