Insert Into Third Table Based Two Another- 神谕
Insert Into Third Table Based Two Another- oracle
我有这三个table;假设 table 的(尤其是 A)数据量非常大,我如何插入 Table C (c_table
) FROM b_table 和 a_table :
Table乙:
CREATE TABLE b_table
(
NAME varchar2(10),
ID number PRIMARY KEY
)
Table答:
CREATE TABLE a_table
(
CODE number,
RATE number,
DATEE date,
CONSTRAINT fk_a_table
FOREIGN KEY (CODE) REFERENCES b_table(ID)
);
Table C:
CREATE TABLE c_table
(
DATEE date,
USD number,
EUR number,
OBP number
);
begin
for r in(
with m as(
select 1 as xcode, 1000 as xrate, sysdate as xdate from dual
union all
select 2, 2000, sysdate from dual
union all
select 3, 3000, sysdate-1 from dual
union all
select 1, 2000, sysdate from dual
union all
select 2, 4000, sysdate from dual
union all
select 3, 5000, sysdate-2 from dual
)
select * from m
pivot(
sum(xrate) for xcode in (
1 as usd,
2 as eur,
3 as gbp
))
)loop
update your_table set foo = r.xrate, foo_currency_usd = x.usd where foo_date = r.date;
end loop;
end;
试试下面的代码。
INSERT INTO c_table(DATEE, USD, EUR, OBP)
Select DATEE, USD, EUR, OBP from(
Select
TO_CHAR(a.DATEE,'DD-MON-YYYY') DATEE, a.Rate, b.name from a_table a
inner join b_table b on b.id=a.code
)PIVOT(SUM(rate)for name in ('USD' USD, 'EUR' EUR,'OBP' OBP));
您可以使用条件聚合:
insert into c_table (DATEE, USD, EUR, OBP)
select a.DATEE,
max(case WHEN a.code = 1 THEN a.rate end),
max(case WHEN a.code = 2 THEN a.rate end),
max(case WHEN a.code = 3 THEN a.rate end)
from a_table a
group by a.DATEE
我有这三个table;假设 table 的(尤其是 A)数据量非常大,我如何插入 Table C (c_table
) FROM b_table 和 a_table :
Table乙:
CREATE TABLE b_table
(
NAME varchar2(10),
ID number PRIMARY KEY
)
Table答:
CREATE TABLE a_table
(
CODE number,
RATE number,
DATEE date,
CONSTRAINT fk_a_table
FOREIGN KEY (CODE) REFERENCES b_table(ID)
);
Table C:
CREATE TABLE c_table
(
DATEE date,
USD number,
EUR number,
OBP number
);
begin
for r in(
with m as(
select 1 as xcode, 1000 as xrate, sysdate as xdate from dual
union all
select 2, 2000, sysdate from dual
union all
select 3, 3000, sysdate-1 from dual
union all
select 1, 2000, sysdate from dual
union all
select 2, 4000, sysdate from dual
union all
select 3, 5000, sysdate-2 from dual
)
select * from m
pivot(
sum(xrate) for xcode in (
1 as usd,
2 as eur,
3 as gbp
))
)loop
update your_table set foo = r.xrate, foo_currency_usd = x.usd where foo_date = r.date;
end loop;
end;
试试下面的代码。
INSERT INTO c_table(DATEE, USD, EUR, OBP)
Select DATEE, USD, EUR, OBP from(
Select
TO_CHAR(a.DATEE,'DD-MON-YYYY') DATEE, a.Rate, b.name from a_table a
inner join b_table b on b.id=a.code
)PIVOT(SUM(rate)for name in ('USD' USD, 'EUR' EUR,'OBP' OBP));
您可以使用条件聚合:
insert into c_table (DATEE, USD, EUR, OBP)
select a.DATEE,
max(case WHEN a.code = 1 THEN a.rate end),
max(case WHEN a.code = 2 THEN a.rate end),
max(case WHEN a.code = 3 THEN a.rate end)
from a_table a
group by a.DATEE