简体   繁体   中英

updating a table with a column from another table in SQL

I need to create a new table from data I extract from the following two tables:

First table:

Var     cur_number
-------------------
 A        10
 B         8

Second table:

Var    new_number
-------------------
 A          2
 A         11
 B          4
 B          6

The new table should contain a 'Var' column and a 'Number' column, where for each variable there will be one line with its cur_number, and the rest of the lines will contain the numbers from second table's new_number columns, where the new_number < cur_number. For example, in the example shown above, for A there will be one line with 10 (its cur_number) and one line with '2' (since 2<10, however 11>10).

in my example the new table will be:

Var         Number
 A            10
 A            2
 B            8
 B            4
 B            6

The database is very large, and the running time is crucial so I cannot use UNION on the two tables...

This script assumes there is only one record per "Var" value in table1.

--insert all from table1

insert into newtable (var,number)
select var,cur_number from table1 t1

--insert from table2 where new_number < cur_number

insert into newtable (var,number)
select t2.var, t2.new_number 
from table2 t2
inner join table1 t1 on t1.var = t2.var and t2.new_number < t1.cur_number

Try this:

 
 
 
 
  
  
  SELECT var, cur_number FROM FirstTable UNION SELECT t2.var, t2.new_number FROM Firsttable t1 INNER JOIN SecondTable t2 ON t2.new_number < t1.cur_number;
 
 
  

Update: If you are using SQL Server 2008 or above, you can use MERGE to merge the two tables into one like so:

MERGE INTO FirstTable AS TGT
USING SecondTable AS SRC
  ON  SRC.new_number >= TGT.cur_number
WHEN NOT MATCHED THEN
  INSERT (var, cur_number)
  VALUES (SRC.var, SRC.new_number);

SQL Fiddle Demo

This will merge the two tables values into the first table. The first table will contains:

VAR    CUR_NUMBER
A          10
B          8
A          2
B          4
B          6

Note that: When using MERGE :

  • You have to terminate the MERGE statement with a semicolon, It is mandatory.
  • You can't INSERT inside WHEN MATCHED thats why I used the reverse condition >= in the WHEN NOT MATCHED .

If you know for a fact that you do not have duplicate values in those 2 tables, then you can use UNION ALL instead of UNION

select * from table1 t1
UNION ALL
select* from table2 t2
where t2.new_number < t1.cur_number

By duplicate values, I mean something like this:

table1:

var           cur_number
A                8

table2:

var           new_number
A                8  

The difference is that UNION ALL is faster than UNION , due to the fact that UNION eliminates duplicates from the resultset by using SELECT DISTINCT . In the case you have duplicates, UNION is the best in my experience.

Why not use Union ? * SQLFIDDLE

select * from first
union 
select s.var,s.new_number from second s
join first f
on s.var = f.var
join (
select f.var, max(f.cur_number) maxn
from first f
group by f.var) as t
on t.var = s.var
where s.new_number < t.maxn
order by var asc
;

results

VAR     CUR_NUMBER
A        10
B        8
A        2
B        4
B        6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM