Oracle 中 R cbind() 函数的等价物?
Equivalent of R cbind() function in Oracle?
我是 sql 的新手,正在努力解决这个非常简单的任务。
正在考虑,
with table1 as (select '1' col1 from dual union
select '2' col1 from dual union
select 'NO_PATTERN' col1 from dual union
select 'RANDOM_STUFF' col1 from dual)
select * from table1;
并且
with table2 as (select 'aaa' col2 from dual union
select '4' col2 from dual union
select 'qwewqeq' col2 from dual
union select 'UUUUUU' col2 from dual)
select * from table2;
我想在两列之间执行一个 cbind()
到一个新的 table 中,它是 table1.[col1] 和 [=30 的 "vertical union" =]2[col2].
预期的解决方案是:
with solution as (select '1' col1, 'aaa' col2 from dual union
select '2' col1, '4' col2 from dual union
select 'NO_PATTERN' col1, 'qwewqeq' col2 from dual union
select 'RANDOM_STUFF' col1, 'UUUUUU' col2 from dual)
select * from solution;
有什么想法吗?
在 Oracle 中,您需要显式定义一些值来获取有序数据;否则,您每次 运行 查询时都可能得到不同的结果。
根据您的数据,这:
WITH table1 AS
(SELECT 1 rn, '1' col1 FROM DUAL UNION
SELECT 2 rn, '2' col1 FROM DUAL UNION
SELECT 3 rn, 'NO_PATTERN' col1 FROM DUAL UNION
SELECT 4 rn, 'RANDOM_STUFF' col1 FROM DUAL),
table2 AS
(SELECT 1 rn, 'aaa' col2 FROM DUAL UNION
SELECT 2 rn, '4' col2 FROM DUAL UNION
SELECT 3 rn, 'qwewqeq' col2 FROM DUAL UNION
SELECT 4 rn, 'UUUUUU' col2 FROM DUAL)
SELECT col1, col2, t1.rn
from (select row_number() over (order by rn) as rn, col1 from table1 ) t1
inner join
(select row_number() over (order by rn) as rn, col2 from table2 ) t2
on (t1.rn = t2.rn)
给出:
COL1 COL2 RN
------------ ------- ----------
1 aaa 1
2 4 2
NO_PATTERN qwewqeq 3
RANDOM_STUFF UUUUUU 4
没有明确的排序,例如,这个
WITH table1 AS
(SELECT '1' col1 FROM DUAL UNION
SELECT '2' col1 FROM DUAL UNION
SELECT 'NO_PATTERN' col1 FROM DUAL UNION
SELECT 'RANDOM_STUFF' col1 FROM DUAL),
table2 AS
(SELECT 'aaa' col2 FROM DUAL UNION
SELECT '4' col2 FROM DUAL UNION
SELECT 'qwewqeq' col2 FROM DUAL UNION
SELECT 'UUUUUU' col2 FROM DUAL)
SELECT col1, col2, t1.rn
from (select rownum as rn, col1 from table1 ) t1
inner join
(select rownum as rn, col2 from table2 ) t2
on (t1.rn = t2.rn)
给予
COL1 COL2 RN
------------ ------- ----------
1 4 1
2 UUUUUU 2
NO_PATTERN aaa 3
RANDOM_STUFF qwewqeq 4
如果您不想订购它们,这是一种简单的方法 -
select *, ROW_NUMBER() OVER (ORDER BY (SELECT 100)) AS SNO into #0 FROM Table 2
select *, ROW_NUMBER() OVER (ORDER BY (SELECT 100)) AS SNO into #1 FROM Table1
select a.*, b.* into #3 from #0 a join #1 b on a.SNO = b.SNO
从这里得到灵感 -
我是 sql 的新手,正在努力解决这个非常简单的任务。
正在考虑,
with table1 as (select '1' col1 from dual union
select '2' col1 from dual union
select 'NO_PATTERN' col1 from dual union
select 'RANDOM_STUFF' col1 from dual)
select * from table1;
并且
with table2 as (select 'aaa' col2 from dual union
select '4' col2 from dual union
select 'qwewqeq' col2 from dual
union select 'UUUUUU' col2 from dual)
select * from table2;
我想在两列之间执行一个 cbind()
到一个新的 table 中,它是 table1.[col1] 和 [=30 的 "vertical union" =]2[col2].
预期的解决方案是:
with solution as (select '1' col1, 'aaa' col2 from dual union
select '2' col1, '4' col2 from dual union
select 'NO_PATTERN' col1, 'qwewqeq' col2 from dual union
select 'RANDOM_STUFF' col1, 'UUUUUU' col2 from dual)
select * from solution;
有什么想法吗?
在 Oracle 中,您需要显式定义一些值来获取有序数据;否则,您每次 运行 查询时都可能得到不同的结果。
根据您的数据,这:
WITH table1 AS
(SELECT 1 rn, '1' col1 FROM DUAL UNION
SELECT 2 rn, '2' col1 FROM DUAL UNION
SELECT 3 rn, 'NO_PATTERN' col1 FROM DUAL UNION
SELECT 4 rn, 'RANDOM_STUFF' col1 FROM DUAL),
table2 AS
(SELECT 1 rn, 'aaa' col2 FROM DUAL UNION
SELECT 2 rn, '4' col2 FROM DUAL UNION
SELECT 3 rn, 'qwewqeq' col2 FROM DUAL UNION
SELECT 4 rn, 'UUUUUU' col2 FROM DUAL)
SELECT col1, col2, t1.rn
from (select row_number() over (order by rn) as rn, col1 from table1 ) t1
inner join
(select row_number() over (order by rn) as rn, col2 from table2 ) t2
on (t1.rn = t2.rn)
给出:
COL1 COL2 RN
------------ ------- ----------
1 aaa 1
2 4 2
NO_PATTERN qwewqeq 3
RANDOM_STUFF UUUUUU 4
没有明确的排序,例如,这个
WITH table1 AS
(SELECT '1' col1 FROM DUAL UNION
SELECT '2' col1 FROM DUAL UNION
SELECT 'NO_PATTERN' col1 FROM DUAL UNION
SELECT 'RANDOM_STUFF' col1 FROM DUAL),
table2 AS
(SELECT 'aaa' col2 FROM DUAL UNION
SELECT '4' col2 FROM DUAL UNION
SELECT 'qwewqeq' col2 FROM DUAL UNION
SELECT 'UUUUUU' col2 FROM DUAL)
SELECT col1, col2, t1.rn
from (select rownum as rn, col1 from table1 ) t1
inner join
(select rownum as rn, col2 from table2 ) t2
on (t1.rn = t2.rn)
给予
COL1 COL2 RN
------------ ------- ----------
1 4 1
2 UUUUUU 2
NO_PATTERN aaa 3
RANDOM_STUFF qwewqeq 4
如果您不想订购它们,这是一种简单的方法 -
select *, ROW_NUMBER() OVER (ORDER BY (SELECT 100)) AS SNO into #0 FROM Table 2
select *, ROW_NUMBER() OVER (ORDER BY (SELECT 100)) AS SNO into #1 FROM Table1
select a.*, b.* into #3 from #0 a join #1 b on a.SNO = b.SNO
从这里得到灵感 -