[英]How to insert data from 2 other tables in oracle
我正在做一张新桌子。 表中的值/数据取自另一个表。 例如:在table A
我具有包含2个数据的字段Code
EXE
和IMP
。 在table B
我具有数据为2016
和2017
字段Year
。 现在我需要做table C
与现场Status
,其中在字段中的数据Status
是EXE-2016
, IMP-2016
, EXE-2017
和IMP-2017
。 如何查询此类问题?
table A
code | code_name | flag
EXE | Execute | Y
IMP | Implement| Y
table B
Year | phase | flag
2016 | P1 | Y
2016 | P2 | Y
2017 | P1 | Y
2017 | P2 | Y
2018 | P1 | N
table C
Status | Flag
EXE-2016 | Y
IMP-2016 | Y
EXE-2017 | Y
IMP-2017 | Y
EXE-2018 | N
IMP-2018 | N
在您的情况下,最好以这样一种方式创建view
以记录sql
以供以后使用,例如表:
create view tableC as
select code||' - '||year status, flag
from (
select a.code, b.year, b.flag from tableA a cross join tableB b
)
group by code, year, flag
order by year, code;/
select * from tableC;/
您想交叉连接两个表中的记录:
然后使用CREATE TABLE c AS <query>
以便从查询结果创建表C。
create table c as
select a.code || '-' || bb.year as status, bb.flag
from a
cross join
(
select year, max(flag) as flag
from b
group by year
) bb;
(也许BarbarosÖzhan是正确的,您实际上是想创建一个视图。然后将create view c as <query>
。)
create table C as
select A.code || '-' || B.year as status
from A, B
CREATE table C as
SELECT A.Code || '-' || B.Year AS
STATUS , B.flag
FROM A,B
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.