Fast update or create a table based on data from another table - sql

I have a table with 100k rows and 20 columns and I want based on that data to create a different table with the same amount of rows and columns, but with data calculated from the first table and previous columns of the second table.
Example:
Table A(with columns a1,a2,a3,a4,a5) is the table with original data.
Table B (with columns b1,b2,b3,b4,b5) is the second table with the calculated data.
What I would like to do is this (pseudocode - the calculations are more complicated):
UPDATE B
SET B.b2= A.a1*2,
B.b3= A.a2*A.a1,
B.b4= B.b1+B.b2,
B.b5= round(B.b4,2)
WHERE B.a1 = A.a1;
Another approach could be the CTAS (Create Table As Select) but this doesn't allow to create a column based on a previous column (I mean create B.b4 based on B.b3 at the same time)
Which is the faster approach to do it?
PS1. My approach to CTAS:
create table B NOLOGGING as
select a1 b1,
round(a2, 3) b2,
a3 b3,
b1+30 b4, --ORA 00904 b1 invalid identifier
a5 b5
from A;

If you want to do a CTAS without repeating the calculations you can use a subquery:
create table b (b1, b2, b3, b4, b5)
as
select t1, t2, t3, t1 + t2, round(t1 + t2, 2)
from (
select a1 as t1, a1*2 as t2, a2 * a1 as t3
from a
);
SQl Fiddle.
If you have multiple levels of dependency then you can have multiple levels of subquery, adding new calculated values at each level:
create table b (b1, b2, b3, b4, b5)
as
select t1, t2, t3, t4, round(t4, 2)
from (
select t1, t2, t3, t1 + t2 as t4
from (
select a1 as t1, a1*2 as t2, a2 * a1 as t3
from a
)
);
Another Fiddle.
You could use the same approach for an update or merge, but then you have two passes, one to insert the b1 value on its own, then another to set everything else based on it. The CTAS approach will be quicker and simpler, I think.

Related

Select entries that have non repeating values on a specific column (although other columns may have repeating or non repeating values) (SQL)

Let's say I have the following table:
A
B
C
D
a1
b1
c1
d1
a1
b1
c1
d2
a2
b2
c3
d3
a2
b2
c4
d3
I want to filter and see all four columns for entries that have the same value con column A but different on column C, so I get only this as a result:
A
B
C
D
a2
b2
c3
d3
a2
b2
c4
d3
I don't really care if values con columns B and D are the same or different, although I would like to have them in my table to do further analysis later.
Using the DISTINCT statement would give me all the columns as a result, as they all are different in some column, so that doesn't work for me.
I read some questions (like this one) and the answers recommended using the row_number() over(partition by...) clause, although the use they gave it doesn't quite fit my problem (I think), as it would also return the first row with a repeating value on column C.
Any ideas how this could be done?
You can use exists:
select t.*
from t
where exists (select 1
from t t2
where t2.a = t.a and t2.c <> t.c
)
order by t.a;
You could use a self join
select t1.*
from t t1
join t t2 on t1.a=t2.a and t1.c<>t2.c

How can I migrate the data from one table to other efficiently without fail in oracle

I have two tables suppose t1 and t2.
T1 has columns - c1, c2, c3, c4, c5
T2 has columns - ac1, ac2, ac3, ac4
I have to migrate the values of c1 of t1 into ac4 of t2.
Onw way to do this is -
INSERT INTO t2 (ac1,ac2,ac3, ac4)
SELECT
STAGING_LOGIN_RECORD_SEQUENCE.NEXTVAL,
'N',
'PARTNER',
c1
FROM t1;
but this is taking too much time.
I have almost 10 million records.
is there any effective way to do this? In batches or something?
I am open to other ways like writing procedure or any other ideas.

How to merge two queries having different output into one query while sequence should remain same

In different SQL queries when I merge into one while follow the same sequence. Query are as follows-
select c1, c2, .....,
convert(varchar, t2.col1) AS col from table1 t1 inner join table2 t2 on t1.col2=t2.col1 AS col1,
....., c15 from table;
Here in above previous lots of columns before JOIN are there to fetch the data and mentioned as c1, c2, .... c15 are the column to fetching the values also lots of column are there after JOIN. But I want all these things into one SQL query. I stuck only on one JOINING two different tables as one column.
select all the columns as usual and at that time of JOINING write query like this-
select c1, c2, .....
convert(varchar, t2.col1) AS col,
...., c15 from table1
inner join on table2 on t1.col1 = t2.col2
The output you want merge into one.

Compare Every Record from Two Tables

Assuming I have two SQL tables consisting of a single column,
i.e.
Table 1 Table 2
a1 a2
b1 b2
c1 c2
Is there a succinct SQL command to compare each record in one table against each record in the other? (and return true if any record from table 1 matches any record from table 2)
i.e.
if( a1 = a2 OR a1 = b2 OR a1 = c2 OR b1 = a2 OR b1 = b2...)
I want
If any record from table a matches table b (i.e., in a table of ints, they are the same int), return true.
Why not simply
if exists (select 1 from T1 inner join TB on T1.Col = T2.Col)
A full join is well suited to finding differences. To find rows that are missing in either table:
select *
from t1
full join
t2
on t1.col1 = t2.col1
where t1.col1 is null
or t2.col1 is null
This assumes that the single column is unique (i.e. has no duplicate values.)

SQL calculate difference between cell values

Hi i was wondering if anyone knows how i can calculate the difference between two tables in tsql. I dont mean finding which cells are different - i mean calulcating the numerical difference. eg - Table A has column1, column 2 and only one 1 row. A1 = 40, B1 = 30. Table B has column1, column 2 and only one 1 row. A1 = 25, B1 = 10. So how could i get (A1 = 15, B1 = 20) using TSQL?
Given that you have no way to join the tables, you'll need a Cartesian product of the two. Luckily since each table only has one record that's not a problem.
You do it like this:
SELECT
TableA.A1 - TableB.A1 AS A1,
TableA.B1 - TableB.B1 AS B1
FROM TableA, TableB
If you had more than one record in each table, this query would return a result for every pair of records in both tables. So if TableA had n records and TableB has m, the result will have n*m records.
SELECT a.column1 - b.column1 , a.column2 - b.column2
FROM a
CROSS JOIN
b
Free from my mind =)
Select
(CONVERT(int, T1.A1) - Convert(int, T2.A1)) as A1,
(CONVERT(int, T1.B1) - Convert(int, T2.B)) as B1
From Table1 T1
inner join Table2 T2 on T1.Key = T2.Key