如何分组中的两个列中的每个值组合? -SQL
我有一个表格,希望每个父母都映射到每个组中的每个孩子。
输入:
group_id parent child
1 A E
1 B
2 C F
2 D
2 E G
3 X
3 Y
输出:
group_id parent child
1 A E
1 B E
2 C F
2 D F
2 E F
2 C G
2 D G
2 E G
因此,在每个组中,我希望parent
中的每个值都可以映射到child
中的每个值。如果child
在组中没有值,我希望该组完全省略(如所示,其中group_id
= 3)
我最初是在考虑使用group by by 1,2
,通过max(child)
之类的汇总,但是后来我在数据中遇到了一个可能有1个孩子的数据。我还尝试使用交叉加入
,但我正在努力获取所需的输出。提前致谢。
I have a table where I want every parent to be mapped to every child within each group.
Input:
group_id parent child
1 A E
1 B
2 C F
2 D
2 E G
3 X
3 Y
Output:
group_id parent child
1 A E
1 B E
2 C F
2 D F
2 E F
2 C G
2 D G
2 E G
So within each group, I want every value in parent
to map to every value in child
. If there are no values in child
for a group, I want that group completely omitted (as shown where group_id
= 3)
I was originally thinking of using GROUP BY 1, 2
and aggregating by something like MAX(child)
, but then I came across edge cases in my data where there may be >1 child. I also tried using CROSS JOIN
but I'm struggling with getting my desired output. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
免责声明,我不使用红移,因此可能有更好的选择。但是,交叉连接应该起作用。只需获取所有组的不同父值即可。然后对子值进行相同的操作,然后将两个结果加在一起
结果:
db<> fiddle
Disclaimer, I don't use Redshift so there may be better options. However, a CROSS JOIN should work. Just grab the DISTINCT parent values for all groups. Then do the same for the child values, and JOIN the two results together
Results:
db<>fiddle here
您可以利用变量。该变量将设置为
child
的值,如果其值不为null,否则将复制在变量中包含的值之上,这是一个小提琴: https://www.db-fiddle.com/f/fiddle.com/f/5knofogv19jc4nwa8rrrrzvx/0 。
它对您有用吗?
You can do it exploiting a variable. The variable will be set to the value of
child
if its value is not null, otherwise it will copy over the value contained in the variableHere's a fiddle: https://www.db-fiddle.com/f/5KnoFogV19jc4nWa8rrzVx/0.
Does it work for you?