簡體   English   中英

如何從Oracle中的其他2個表插入數據

[英]How to insert data from 2 other tables in oracle

我正在做一張新桌子。 表中的值/數據取自另一個表。 例如:在table A我具有包含2個數據的字段Code EXEIMP table B我具有數據為20162017字段Year 現在我需要做table C與現場Status ,其中在字段中的數據StatusEXE-2016IMP-2016EXE-2017IMP-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;/

德莫

您想交叉連接兩個表中的記錄:

  • 表A中的每條記錄
  • 每年從表B中獲得其最大標志(即“ Y”勝過“ N”)

然后使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM