[英]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.