访问:一行内的笛卡尔积
Access: Cartesian product within a row
我是 运行 Access 2016,有 table(称为 Table1)parent 和他们的 children。 parent 列在第一列,它们的 children 显示在第二到第四列,所有 children 对应同一行的 parent。但是,每个 parent 的 children 数量不同,因此较高列编号中的某些单元格为空白:
Parent Children1 Children2 Children3 Children4 Children5
A A1 A2 A3 A4 A5
B B1 B2 -- -- --
C C1 C2 C3 -- --
有没有一种方法可以编写一个查询,将此 table 转换为每个 children 单独直接赋值给它的 parent?我需要这样的输出(应忽略空白单元格):
Parent Children
A A1
A A2
A A3
A A4
A A5
B B1
B B2
C C1
C C2
C C3
从技术上讲,这应该类似于 table1 中所有列的笛卡尔积,但恰好在每个 parent 的行内。我在 笛卡尔积 、 转置 和 变换 的访问函数方向中搜索,但我找不到一个合适的例子。
谢谢
您可以使用 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
我是 运行 Access 2016,有 table(称为 Table1)parent 和他们的 children。 parent 列在第一列,它们的 children 显示在第二到第四列,所有 children 对应同一行的 parent。但是,每个 parent 的 children 数量不同,因此较高列编号中的某些单元格为空白:
Parent Children1 Children2 Children3 Children4 Children5
A A1 A2 A3 A4 A5
B B1 B2 -- -- --
C C1 C2 C3 -- --
有没有一种方法可以编写一个查询,将此 table 转换为每个 children 单独直接赋值给它的 parent?我需要这样的输出(应忽略空白单元格):
Parent Children
A A1
A A2
A A3
A A4
A A5
B B1
B B2
C C1
C C2
C C3
从技术上讲,这应该类似于 table1 中所有列的笛卡尔积,但恰好在每个 parent 的行内。我在 笛卡尔积 、 转置 和 变换 的访问函数方向中搜索,但我找不到一个合适的例子。
谢谢
您可以使用 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