如何分组中的两个列中的每个值组合? -SQL

发布于 2025-01-21 19:39:28 字数 829 浏览 0 评论 0原文

我有一个表格,希望每个父母都映射到每个组中的每个孩子。

输入:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光病人 2025-01-28 19:39:29

免责声明,我不使用红移,因此可能有更好的选择。但是,交叉连接应该起作用。只需获取所有组的不同父值即可。然后对子值进行相同的操作,然后将两个结果加在一起

SELECT  p.group_id, p.parent, c.child
FROM   ( 
           SELECT group_id, parent
           FROM   YourTable
           GROUP BY group_id, parent
       ) 
       p CROSS JOIN 
       (
           SELECT group_id, child
           FROM   YourTable
           WHERE  child IS NOT NULL
           GROUP BY group_id, child       
       )
       c 
WHERE  p.group_id = c.group_id
ORDER BY p.group_id, c.child, p.parent

结果:

group_idparentChild
1AE
1BE
2CF
2DF
2 F 2EF
2 E F 2CG
2G 2 G2 G 2 E
2Eg

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

SELECT  p.group_id, p.parent, c.child
FROM   ( 
           SELECT group_id, parent
           FROM   YourTable
           GROUP BY group_id, parent
       ) 
       p CROSS JOIN 
       (
           SELECT group_id, child
           FROM   YourTable
           WHERE  child IS NOT NULL
           GROUP BY group_id, child       
       )
       c 
WHERE  p.group_id = c.group_id
ORDER BY p.group_id, c.child, p.parent

Results:

group_idparentchild
1AE
1BE
2CF
2DF
2EF
2CG
2DG
2EG

db<>fiddle here

隐诗 2025-01-28 19:39:29

您可以利用变量。该变量将设置为child的值,如果其值不为null,否则将复制在变量中包含的值之上,

SET @child := "";
SELECT 
    group_id,
    parent,
    IF(child IS NULL, @child, @child := child) AS child
FROM 
    tab

这是一个小提琴: 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 variable

SET @child := "";
SELECT 
    group_id,
    parent,
    IF(child IS NULL, @child, @child := child) AS child
FROM 
    tab

Here's a fiddle: https://www.db-fiddle.com/f/5KnoFogV19jc4nWa8rrzVx/0.

Does it work for you?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文