访问:连续的笛卡尔积

时间:2018-11-05 17:12:42

标签: sql ms-access-2016

我正在运行Access 2016,其中包含一个父母及其子女的表(称为表1)。父母在第一列中被列为listet,其子女在第二至第四列中显示,所有子女均对应于同一行的父母。但是,每个父母的孩子数量各不相同,因此列号较高的某些单元格为空白:

Parent     Children1  Children2  Children3  Children4  Children5  
  A            A1         A2         A3         A4         A5
  B            B1         B2         --         --         --
  C            C1         C2         C3         --         --    

有没有一种写查询的方法,可以将该表转换为每个子对象直接分配给其父对象的直接分配?我需要输出看起来像这样(空白单元格将被忽略):

Parent     Children  
  A            A1    
  A            A2                              
  A            A3       
  A            A4      
  A            A5   
  B            B1 
  B            B2           
  C            C1   
  C            C2  
  C            C3  

从技术上讲,这应该类似于表1中所有列的笛卡尔乘积,但仅在每个父级的行内。我在访问函数的方向上搜索了笛卡尔积转置 transform ,但是找不到合适的示例。

谢谢

1 个答案:

答案 0 :(得分:0)

您可以使用union all

select parent, children1 as child from t where children1 is not null
union all
select parent, children2 as child from t where children2 is not null
union all
select parent, children3 as child from t where children3 is not null
union all
select parent, children4 as child from t where children4 is not null
union all
select parent, children5 as child from t where children5 is not null