I want to insert some data of column1 from Table_B to Table_A if the data in Table_B does not exist in Table_A. For example, 'Headache' is in column1 of Table_B but not in column1 of Table_A. Thanks. I wrote the SQL below, but it did not work:
insert into Table_A(column1)
select column1
from Table_B
where column1 not in (select column1 from Table_A)
Try This:
INSERT INTO Table_A(column1)
SELECT B.column1
FROM Table_B B
LEFT JOIN Table_A A ON B.column1 = A.column1
WHERE A.column1 IS NULL
Insert into Table_A(column1)
select column1 from Table_B
left join Table_A on Table_B.column1 = Table_A.column1
where A.column1 is null
With no sample data it is hard to say for sure, but my best guess would be that you have a NULL value in table_A.Column1. if you did have a null values, your query would be equivalent to something like:
SELECT Column1
FROM Table_B
WHERE Column1 NOT IN (1, 2, 3, NULL);
Which is equivalent of:
SELECT Column1
FROM Table_B
WHERE Column1 <> 1
AND Column1 <> 2
AND Column1 <> 3
AND Column1 <> NULL;
Since Column1 <> NULL is not true, the query returns no results. The most syntactically similar way to achieve the desired result where you might have NULL columns is using NOT EXISTS:
INSERT INTO Table_A(column1)
SELECT Column1
FROM Table_B AS B
WHERE NOT EXISTS (SELECT 1 FROM Table_A AS A WHERE A.Column1 = B.Column1);
However, another method you could use is:
INSERT INTO Table_A(column1)
SELECT Column1
FROM Table_B AS B
LEFT JOIN Table_A AS A
ON A.Column1 = B.Column1
WHERE A.Column1 IS NULL;
In this by left joining to table_A then stating that A.Column1 has to be NULL, you are removing any records that already exist in Table_A.
I prefer the former (NOT EXISTS), because I think the intent is much more clear, but if you use MySQL the latter will perform better
Or you could also use:
INSERT INTO Table_A(column1)
SELECT Column1
FROM Table_B AS B
WHERE B.Column1 IS NOT NULL
AND B.COlumn1 NOT IN (SELECT A.Column1 FROM Table_A AS A WHERE A.Column1 IS NOT NULL);
What about using a MERGE statement?
MERGE INTO TABLE_A a
USING TABLE_B b ON (a.column1=b.column1)
WHEN NOT MATCHED THEN
INSERT (column1) VALUES (b.column1);
Related
The setting is simple, I wanted to retrieve all rows from table A that were not present in table B. Because a unique row can be identified using 4 columns, I needed to have a way to write the WHERE statement that it works correctly.
My solution is to concatenate the 4 columns and use that as "one" column/key to do the outer join:
select *
from table_A
where filter_condition = 0
and (column1 || column2 || column3 || column4) not in (
select A.column1 || A.column2 || A.column3 || A.column4
from table_A A -- 1618727
inner join table_B B
on A.column1 = B.column1
and A.column2 = B.column2
and A.column3 = B.column3
and A.column4 = B.column4
and filter_condition = 0
)
My question is, is this a good way of doing this or am I doing something fundamentally wrong?
To be clear, the desired result is simply to get back only the rows of table_A that I "lose" due to the INNER JOIN with table_A and table_B.
You seem to be looking for not exists:
select a.*
from table_a a
where a.filter_condition = 0
and not exists (
select 1
from table_b b
where
a.column1 = b.column1
and a.column2 = b.column2
and a.column3 = b.column3
and a.column4 = b.column4
)
This will give you all records in table_a that do not have a corresponding record in table_b.
Using a LEFT JOIN between A and B and checking for a NULL row in B is probably easier:
SELECT *
FROM table_A A
LEFT JOIN table_B B ON A.column1 = B.column1
AND A.column2 = B.column2
AND A.column3 = B.column3
AND A.column4 = B.column4
WHERE B.column1 IS NULL
AND A.filter_condition = 0
You should be able to use tuples (aka row constructors) in PostgreSQL:
select *
from table_a
where filter_condition = 0
and (column1, column2, column3, column4) not in
(
select column1, column2, column3, column4
from table_b
);
If the columns can be null, then better use NOT EXISTS, as null=null results in "unknown" rather than in true or false.
MainTable
ID Column1 TableA.fK TableB.fk
1 some-value 1 null
2 some-value 1 1
3 some-value null 2
TableA
TableA.pk Column1
1 some-value
TableB
TableB.pk Column1
1 some-value
2 some-value
Select Main.ID,Main.Column1 ,A.Column1,B.Column1
FROM MainTable main
LEFT JOIN
Table A
ON Main.TableA.fk = A.TableA.pk
LEFT JOIN
TableB b
ON Main.TableB.fk =B.TableB.fk
WHERE Main.ID =1
Means
The Result is
ID Column1 A.Column1 B.Column1
1 some-value some-value null
Expecting Output
ID column1 A.Column1
1 some-value some-value
should not display B.column1
when tableA foreign key value has not null, it should get all details from TableA if above SELECT query used here
im using RDBMS Microsoft Sql Server 2008
Just don't put this column into the SELECT list:
SELECT Main.ID, A.Column1
FROM MainTable main
LEFT JOIN
Table A
ON Main.TableA.fk = A.TableA.pk
LEFT JOIN
TableB b
ON Main.TableB.fk = B.TableB.fk
WHERE Main.ID = 1
Remove B.Column1 from your query
Select Main.ID,A.Column1 FROM MainTable main LEFT JOIN Table A ON Main.TableA.fk=A.TableA.pk LEFT JOIN TableB b ON Main.TableB.fk =B.TableB.fk WHERE Main.ID =1
I don't understand the problem here. If you don't want it to display B.Column1, take B.Column1 out of your Select statement.
You now have
Select Main.ID, A.Column1, B.Column1
Change it to
Select Main.ID, A.Column1
Based on your comments below, it seems like you're looking for Coalesce or IsNull, depending on which DBMS you're using (which you didn't tell us in your question either).
Something like this should work:
SELECT
Main.ID, Coalesce(A.Column1, B.Column1) as Column1
FROM
MainTable main
LEFT JOIN
Table A
ON
Main.TableA.fk = A.TableA.pk
LEFT JOIN
TableB b
ON
Main.TableB.fk = B.TableB.fk
WHERE
Main.ID = 1
In case I have two SQL tables ,table_a and table_b, both are same , except they may contain different data, I want to insert into the table_b all the rows from table_b that does not exist in table_a already, how should the query look like ? The tables contain id1 and id2 columns only.
Please tell me if my question is not clear
Insert into table_a ...
Thank you
; with cte_left as
(
select id1+id2
from table_a
where id1+id2 not in (select id1+id2 from table_b)
)
insert into table_b
select * from cte_left
You could use the EXCEPT operator:
INSERT INTO table_a (id1, id2)
SELECT id1, id2
FROM (
SELECT id1, id2
FROM table_b
EXCEPT
SELECT id1, id2
FROM table_a
) except_gry
SQL Fiddle demo here
Insert into table_a ( id1, id2 )
select b.id1, b.id2
from table_b b
left outer join table_a a on a.id1 = b.id1 and a.id2 = b.id2
where a.id1 is null
You can use not exist in your query:
insert into table_a(id1,id2)
select id1,id2
from table_b
where not exists(select id1,id2 from table_a)
Hy guys,
can anybody please help me with a subquery in Oracle database 10g? I need to extract the values for a column in the first table as value of another column in the second table.
I currently use this statement:
SELECT
CASE WHEN A.column1 = 'A' THEN 'aaa'
WHEN A.column1 = 'B' THEN 'bbb'
.......
WHEN A.column1 = 'X' THEN 'xxx'
ELSE 'bad' END AS COLUMN1, A.*
FROM TRANSACTION_TABLE A, CATEGORY_TABLE B
WHERE A.column1 IS NOT NULL
AND A.column1 <> ' '
This is not an elegant approach, so I'm trying to use a subselect from CATEGORY_TABLE B like the following:
SELECT A.column1, A.*
FROM TRANSACTION_TABLE A, CATEGORY_TABLE B
WHERE A.column1 IS NOT NULL
AND A.column1 = B.column_b_1
AND A.column1 <> ' '
AND A.column1 IN (SELECT B.column_b_1_descr FROM CATEGORY_TABLE B
WHERE B.FIELDNAME = 'column1' AND A.column1 = B.column_b_1)
So, I cannot get any results by using the subquery and don't want to continue using the CASE against many conditions, just want to replace the A.column1 values with the descriptive values from B.column_b_1_descr , as they're easier to read.
I would appreciate any feedback.
Thanks
Unless I'm misunderstanding your question...
CATEGORY_TABLE:
name | value
A aaa
B bbb
C ccc
...
SELECT B.value AS COLUMN1, A.\*
FROM TRANSACTION\_TABLE A, CATEGORY\_TABLE B
WHERE A.column1 = B.name
or
SELECT t2.value as COLUMN1, t1.\*
FROM TRANSACTION\_TABLE t1
INNER JOIN CATEGORY\_TABLE t2 ON t1.column1 = t2.name;
The where clause isn't needed, since an inner join automatically excludes rows with null values or no matches.
I have this
SELECT COLUMN1, COLUMN2, COLUMN3
FROM TABLE_A
WHERE NOT COLUMN1 IN (SELECT COLUMN1 FROM TABLE B)
But I need it to look at 2 columns rather than 1. It needs to Select rows no row in TABLE_B has those 2 values together.
Where (NOt Column1 IN (Select ..) Or Not Column2 IN (Select ..))
Or
Where NOt Column1 IN (Select ..) And Not Column2 IN (Select ..)
If I understood the question right, this should work:
select COLUMN1, COLUMN2, COLUMN3
from TABLE_A
left outer join TABLE_B
on TABLE_A.COLUMN1 = TABLE_B.COLUMN1
and TABLE_A.COLUMN2 = TABLE_B.COLUMN2
where TABLE_B.COLUMN1 is null
This is under the assumption that the columns involved in the left outer join do not allow null values. If there can be nulls involved it gets more complicated...