how to insert but get data from multiple tables - sqlite

I have a simple SQL insert query, however two of the data entires come from two other tables,
I know how to get data from from one table in an insert but how do i get data from two tables?
Example (One Table Data)
INSERT INTO TABLE (COL1, COL2, COL3)
SELECT :COL1, :COL2, TABLE2.ID
FROM TABLE2
WHERE TABLE2.NAME = :LEVEL0
The above works fine and pulls relevant data out of table2, issue is i need to add a third table.
INSERT INTO TABLE (COL1, COL2, COL3, COL4)
SELECT :COL1,
:COL2,
(TABLE2.ID FROM TABLE2 WHERE TABLE2.NAME = :LEVEL0),
(TABLE3.ID FROM TABLE3 WHERE TABLE3.NAME = :LEVEL1)
doesn't work i get SQL errors

Subqueries are queries, so they need their own SELECT:
INSERT INTO TABLE (COL1, COL2, COL3, COL4)
SELECT :COL1,
:COL2,
(SELECT ID FROM TABLE2 WHERE NAME = :LEVEL0),
(SELECT ID FROM TABLE3 WHERE NAME = :LEVEL1);
And when all values are computed by subqueries, you do not need to use the SELECT form of the INSERT:
INSERT INTO TABLE (COL1, COL2, COL3, COL4)
VALUES(:COL1,
:COL2,
(SELECT ID FROM TABLE2 WHERE NAME = :LEVEL0),
(SELECT ID FROM TABLE3 WHERE NAME = :LEVEL1));

Literally as i asked the question i found the solution 'cross join'
INSERT INTO TABLE1 (COL1, COL2, COL3, COL4)
SELECT :COL1, :COL2, TABLE2.ID, TABLE3.ID
FROM TABLE2 CROSS JOIN TABLE3
WHERE TABLE2.NAME = :LEVEL0 AND TABLE3.NAME = :LEVEL1

Related

insert row if one with equal value does not already exist

I'm trying to do an insert into table if the table doesn't already contain a field with a specified value. For this I am using this command: INSERT INTO zyprexa (NULL,'hello',0,0,0) SELECT date WHERE NOT EXISTS (SELECT 1 FROM zyprexa WHERE date='hello'); so it should insert a row with the values null,'hello',0,0,0. And then if it is executed again it should not do anything since there is already a row with the date value "hello". But it gives me a syntax error and doesn't run it even the first time. All the best.
The correct syntax is:
INSERT INTO zyprexa (col1, col2, col3, col4, col5)
SELECT NULL, 'hello', 0, 0, 0
WHERE NOT EXISTS (SELECT 1 FROM zyprexa WHERE date = 'hello');
where col1, col2, col3, col4, col5 are the names of the columns that will receive the values (for example col2 should be date).
If you want the column date to be unique in your table, you should define it as such:
CREATE TABLE zyprexa (
......
date TEXT UNIQUE,
......
);
and then you can execute INSERT statements with INSERT OR IGNORE, in which case if you try to insert a new row with a date (or any other column defined as unique) that already exists in the table, the insertion will fail (without any error):
INSERT OR IGNORE INTO zyprexa (col1, col2, col3, col4, col5)
VALUES (NULL, 'hello', 0, 0, 0);

Can anyone guide me how to achieve below scenario

I have source and target tables with some calculation as I mentioned below, I want to populate result set of the calculation from the source table into the destination table.
Columns in destination table are enabled NOT NULL constraint.
Can anyone guide me to achieve below requirement.
SOURCE TABLE:
Source_table1 >>
col1,
col2,
col3
Source_table2 >>
col1,
col2,
col3
TARGET TABLE:
Dest_table:
col1 >> Source_table1.col1+Source_table2.col1
col2 >> Source_table1.col2+Source_table2.col2
col3 >> Dest_table.col1+Dest_table.col2
col4 >> Source_table1.col3+Source_table1.col3+Dest_table.col3
Thanks in advance.
All you need is three layers of summations, something as shown below. A simple Insert into select should work than going for a PL/SQL block. And as per one of your comments, source_tab1.seq_no = source_tab2.seq_no is the join condition between the source tables.
INSERT INTO Dest_table (col1,
col2,
col3,
col4)
SELECT dcol1,
dcol2,
dcol3,
s1col3 + s2col3 + dcol3 dcol4
FROM (SELECT dcol1,
dcol2,
dcol1 + dcol2 dcol3,
s1col3,
s2col3
FROM (SELECT s1col1 + s2col1 dcol1,
s1col2 + s2col2 dcol2,
s1col3,
s2col3
FROM (SELECT s1.col1 s1col1,
s1.col2 s1col2,
s1.col3 s1col3,
s2.col1 s2col1,
s2.col2 s2col2,
s2.col3 s2col3
FROM source_table1 s1
JOIN source_table2 s2
ON s1.seq_no = s2.seq_no)));

Keyword from not found where expected(oracle sql)

CREATE TABLE table2 AS
(select *,
min(nullif (("col1"),0)) over (partition by col2,col3,col4) as "col6",
min(nullif("col5",0)) over (partition by col1,col2,col3,col4) as "col7"
FROM table1);
I dont know whats the error?
You can't use the wildcard "*" for columns like that. Because you are adding additional columns, you need to list out all the columns ...
So if your table1 has columns "col1 and col2", then your query would be:
CREATE TABLE table2 AS
(select col1, col2,
min(nullif (("col1"),0)) over (partition by col2,col3,col4) as "col6",
min(nullif("col5",0)) over (partition by col1,col2,col3,col4) as "col7"
FROM table1);

join 2 tables with update, but only rows with matching value

I am having two tables which i wish to join.
I am not using foreign keys.
table structure table1
id,val1
table structure table2
id,val2
I am using now following command:
update table1 set val1=(SELECT val2 FROM table2 WHERE table1.id LIKE table2.id)
How can i force sqlite not to insert a value in table1 if an id does not exist in table2?
To use exact comparisons, use = instead of LIKE.
To update only those rows where a match is found, use a WHERE clause:
UPDATE Table1
SET val1 = (SELECT val2
FROM Table2
WHERE Table1.id = Table2.id)
WHERE id IN (SELECT id FROM Table2);
If you omit the WHERE clause, rows without a match get updated with a NULL value.

Insert distinct records to table in sqlite3

I want to insert records from first table to second in such a way that only unique records get inserted. That is if table one and two has overlapping (duplicate) records, only the unique records get inserted into table one from table two. For example I have one table named Table_One,
and another table named Table_Two,
now I want to add the records from Table_Two the records that are not already in Table_one which is (XYZ | UVW) so that the output is,
I tried using this technique,
INSERT INTO TableB(Col1, Col2, Col3, ... , Coln)
SELECT DISTINCT A.Col1, A.Col2, A.Col3, ... , A.Coln
FROM TableA A
LEFT JOIN TableB B
ON A.KeyOfTableA = B.KeyOfTableB
WHERE B.KeyOfTableB IS NULL
but the duplicate rows also got inserted, any suggestions as to what could be the solution ??? I am using sqlite3 db.
INSERT INTO TableB (Col1, Col2, Col3, ... , Coln)
SELECT DISTINCT A.Col1,
A.Col2,
A.Col3,
... ,
A.Coln
FROM TableA A
WHERE A.KeyOfTableA NOT IN (SELECT B.KeyOfTableB
FROM TableB B)
Simply use UNION.
Query
sqlite> CREATE TABLE table1(firstName VARCHAR(50),lastName VARCHAR(50));
sqlite> INSERT INTO table1 VALUES('ABC','DEF');
sqlite> INSERT INTO table1 VALUES('GHI','JKL');
sqlite> INSERT INTO table1 VALUES('MNO','PQR');
sqlite>
sqlite> CREATE TABLE table2(first VARCHAR(50),last VARCHAR(50));
sqlite> INSERT INTO table2 VALUES('ABC','DEF');
sqlite> INSERT INTO table2 VALUES('XYZ','UVW');
sqlite> .headers ON
sqlite> SELECT * FROM table1 UNION SELECT * FROm table2;
OUTPUT
firstName|lastName
ABC|DEF
GHI|JKL
MNO|PQR
XYZ|UVW

Resources