Oracle: INSERT on condition from other table - sql

I am trying to create a SQL query where I need to insert a new sets of records based but 1 column is needed from required table.
E.g.,
TABLE_1
=======
ID,
A,
B
TABLE2
======
ID,
C,
D
Each ID are identical ID column.
I have a query of this format:
INSERT INTO TABLE_1 (ID, A, B) VALUES (???, "Yes", "What")
WHERE ID IN (SELECT ID FROM TABLE_2 WHERE ID > 10)
This, clearly doesn't work.
My question: how do I add the ID value from Table 2 into Table 1 and make it runnable? This is a query that must run on Oracle 11g.

Try this query:
INSERT INTO MyTable(ID, A, B)
SELECT ID,'YES','What' FROM TABLE_2 WHERE ID > 10

Related

How to select only values that are not repeating in a column example if a have column with following values "A,b,c,a,c" i have to select only b

How to select only values that are not repeating in a column? For example if a have table with following values I expect to return only the id value of b:
id
--
a
b
c
a
c
Aggregation provides one approach:
SELECT id
FROM yourTable
GROUP BY id
HAVING COUNT(*) = 1;
In subselecet you select values which have only one record, Than in outer select you search for all datas based on that value in subselect
SELECT *
FROM table
WHERE id IN (SELECT id
FROM table
GROUP BY id
HAVING COUNT (id) = 1)

Insert new records with several ids from another table

I have a table, which has 9 records with id = 1 - 9 (for example, there can be more than 20 ids).
I have one varchar value = 'premium'.
I need to insert these values to another table, after this action I should have 9 records with id from the first table and 'premium' varchar in the second table:
1, 'premium';
2, 'premium';
etc.
How to write the function for SQL?
Are you looking for insert . . . select or create table as?
insert into table2 (id, value)
select id, 'premium'
from table1;
or:
create table table2 as
select id, 'premium' as value
from table1;
Do you want this?
demo:db<>fiddle
INSERT INTO second_table (id, text_value)
SELECT id, 'premium'
FROM first_table;

oracle - How to insert unique values from a table to another multiple times?

Using Oracle 12c, I have, for example, 5 rows with values 1,2,3,4,5 in a PK column in one table TABLEA. I would like to insert into another table TABLEB the values but 3 times. So TABLE would have 15 rows with values 1,1,1,2,2,2,3,3,3,4,4,4,5,5,5 after the insert. How can I achieve this?
I'm trying to create a script that will insert values from TABLEA to TABLEB if they don't already exist there. Currently I am manually inserting into TABLEB each value from TABLEA 3 times.
You can use cross join. The query would look something like this:
insert into t(pk)
select pk
from table t2 cross join
(select 1 as n from dual union all select 2 from dual union all select 3 from dual
) n;

Need to write Query with condition

I need to write a single query
If a record id already exists in table, and the same record id with some different values is
then I have to update its expiry date in the table and also insert the record id with new values to the table.
If not record id not present then make a new entry to the table .
Eg: In a table tmp record_id =101 exists then update main_table expiry_date of all the rows with record id='101' and also insert a new row from tmp to main_table else insert into main_table.
You need a Merge statement and an insert:
merge into your_table t
using(select 5 as id, sysdate as expiry_date from source where coditions) S
on(s.id = t.id)
when matched then update t.expiry_date = s.expiry_date;
insert into your_table t
values (5 , sysdate);
Check this one please:
MERGE INTO mytable b
USING (SELECT * from mytable where id = 10) a
ON b.id = a.id
WHEN MATCHED THEN UPDATE SET b.expirydate = a.expirydate
;
INSERT (id, col1, col2...) VALUES (a.id, col1, col2...)
In the event of matched you will update. At all times you will insert.

Insert a part of a table into another table

I have table A and table B, same schemas.
I want to insert certain rows from table A into table B. For example, insert into table B all rows from table A with column 'abc' > 10.
Couldn't figure out how to do it
Something like this
INSERT INTO B (supplier_id, supplier_name)
SELECT supplier_id, supplier_name FROM A
WHERE abc > 10;
Make sense?
You can use the following notation:
BEGIN TRAN
INSERT INTO ExistingTable (Col1, Col2...)
SELECT Something1, Something2... FROM Table1 WHERE ...
--ROLLBACK/COMMIT
At first blush, I'd say something like:
Insert Into B
(Select * from A
Where abc > 10)