SQL:通过排列起始项扩展现有 table
SQL: Expand existing table by permutating the starting item
我在 vertica 数据库中有一个 N x M
table,我的目标是用 N*M x M
创建一个新的 table,这样初始 [=] 中的每一行26=] 替换为 M 行,其中排列了起始项。
这里有一个 2 x 3
Table
的例子
+-------+-------+-------+
| Item1 | Item2 | Item3 |
+-------+-------+-------+
| A | B | C |
| C | K | L |
+-------+-------+-------+
成为 6 x 3
Table,其中原始行的每一行都被 3 行替换,其中 Item1 始终是不同的起始项。
+-------+-------+-------+
| Item1 | Item2 | Item3 |
+-------+-------+-------+
| A | B | C |
| B | A | C |
| C | A | B |
| C | K | L |
| K | C | L |
| L | C | K |
+-------+-------+-------+
是否有解决此类问题的优雅解决方案,我尝试以各种方式使用连接,但到目前为止没有运气。谢谢!!
一般情况下我帮不了你。在特定情况下,这是否适用于 Vertica?
select
case n
when 1 then item1
when 2 then item2
else item3
end as item1,
case n
when 1 then item2
when 2 then item3
else item1
end as item2,
case n
when 1 then item3
when 2 then item1
else item2
end as item3
from tab
cross join (select 1 as n union all select 2 as n union all select 3 as n) as b
我自己是一名 SQL 服务器人员,根据 table 的定义动态地进行此查询会很直接(给定对 table 的合理限制元数据),但唉,我不知道 Vertica
我在想为什么不使用一堆这样的联合
SELECT Item1, Item2, Item3 from Table
union SELECT Item2, Item3, Item1 from Table
UNION SELECT Item3, Item1, Item2 from Table
应该会得到相同的结果,不是吗?
我在 vertica 数据库中有一个 N x M
table,我的目标是用 N*M x M
创建一个新的 table,这样初始 [=] 中的每一行26=] 替换为 M 行,其中排列了起始项。
这里有一个 2 x 3
Table
+-------+-------+-------+
| Item1 | Item2 | Item3 |
+-------+-------+-------+
| A | B | C |
| C | K | L |
+-------+-------+-------+
成为 6 x 3
Table,其中原始行的每一行都被 3 行替换,其中 Item1 始终是不同的起始项。
+-------+-------+-------+
| Item1 | Item2 | Item3 |
+-------+-------+-------+
| A | B | C |
| B | A | C |
| C | A | B |
| C | K | L |
| K | C | L |
| L | C | K |
+-------+-------+-------+
是否有解决此类问题的优雅解决方案,我尝试以各种方式使用连接,但到目前为止没有运气。谢谢!!
一般情况下我帮不了你。在特定情况下,这是否适用于 Vertica?
select
case n
when 1 then item1
when 2 then item2
else item3
end as item1,
case n
when 1 then item2
when 2 then item3
else item1
end as item2,
case n
when 1 then item3
when 2 then item1
else item2
end as item3
from tab
cross join (select 1 as n union all select 2 as n union all select 3 as n) as b
我自己是一名 SQL 服务器人员,根据 table 的定义动态地进行此查询会很直接(给定对 table 的合理限制元数据),但唉,我不知道 Vertica
我在想为什么不使用一堆这样的联合
SELECT Item1, Item2, Item3 from Table
union SELECT Item2, Item3, Item1 from Table
UNION SELECT Item3, Item1, Item2 from Table
应该会得到相同的结果,不是吗?