Insert value into a table when strings match conditions from another table - sql

I have two tables in a PostgreSQL database. Table2 has an FK from table1's PK. I want to search table1 for specific strings, and if I find matches I want to update a column in table2 with a string.
Table1
+----+------+------+------+
| PK | Col1 | Col2 | Col3 |
+----+------+------+------+
| 1 | A | x | x |
| 2 | x | x | x |
| 3 | x | A | x |
| 4 | x | x | x |
| 5 | x | x | A |
+----+------+------+------+
Table2
+----+-----------------+
| FK | matching_column |
+----+-----------------+
| 1 | string |
| 2 | |
| 3 | string |
| 4 | |
| 5 | string |
+----+-----------------+
So where table1 contains '%A%'
update table2 with 'string'
I'm not sure where to start on this one. Does anyone have a solution?

Use a subquery:
UPDATE table2
SET matching_column = 'string'
WHERE fk = (SELECT pk
FROM table1
WHERE "Col1" LIKE '%A%'
OR "Col2" LIKE '%A%'
OR "Col3" LIKE '%A%');

You could use the update ... set ... from syntax. If I followed you correctly, you want:
update table2 t2
set t2.matching_column = 'string'
from table 1 t1
where
t1.pk = t2.fk
and 'A' in (t1.col1, t1.col2, t1.col3)
This phrases as: if the fk of table2 exists in table1 and one of the 3 columns contains (col1, col2, col3) contains 'A', then set column matching_column in the corresponding record in table2 to 'string'.

Related

How to update Hive table rows

I have a table that looks like this:
id | Col2 | Col3 | Text
--------------------------
1 | ... | ... | "abc"
2 | ... | ... | "def
3 | ... | ... | "ghi"
4 | ... | ... | "jkl"
And another table that looks like this:
id | Text
-------------
1 | "qwe"
2 | "rty"
And I want to end up with a table that looks like this:
id | Col2 | Col3 | Text
--------------------------
1 | ... | ... | "qwe"
2 | ... | ... | "rty"
3 | ... | ... | "ghi"
4 | ... | ... | "jkl"
where the original values for col2 and col3 are maintained. Essentially, I want to use the values from table 2 to update the values of table 1 where ids are the same.
I tried:
SELECT
A.id,
col1,
col2,
A.text
FROM table1 AS A
LEFT JOIN (
SELECT
id,
text
FROM table2
) AS B
ON A.product_id = B.product_id
But this just returned the original table. Is there a way to achieve what I want in Presto/Hive?
You are loading Text from table A, it should be from table B or NVL(B.text, A.Text) if you want to update value if exists in table B and leave as is if not exists (see comment in the code)
INSERT OVERWRITE table1
SELECT
A.id,
col1,
col2,
NVL(B.text, A.Text) as Text -- Take Text from table B, if not exists, leave as is (A.Text)
FROM table1 AS A
LEFT JOIN B ON A.product_id = B.product_id
You can use coalesce(B.text, A.Text) instead of NVL, as #PiotrFindeisen mentioned, it will work fine on Presto and Hive as well.

How to find out if 2 values in the same row exist in another table

I want to find out if 2 values in the same row exist in another table.
Only return the row if both values exist at the same time.
I can probably do 2 joins but is there an efficient way to do this?
Is there something like below?
table_1
+---------+---------------+------+
| id | other_id | key |
+---------+---------------+------+
| 1 | 2 | A |
| 2 | 1 | B |
| 1 | 3 | C |
| 4 | 2 | D |
+---------+---------------+------+
table_2
+---------+
| id |
+---------+
| 2 |
| 3 |
| 4 |
+---------+
SELECT
*
from
table_1
where
(id, other_id) in (
SELECT
id
from
table_2
)
output_table
+---------+---------------+------+
| id | other_id | key |
+---------+---------------+------+
| 4 | 2 | D |
+---------+---------------+------+
You can try as
SELECT *
FROM table1 t1
WHERE exists (select 1 from table2 t2 where t1.id=t2.id)
and exists (select 1 from table2 t3 where t1.anotherid=t3.id) ;
select * from table_1
where table_1.id in (select table_2.id from table_2)
try this , hope this help you!
Use exists:
SELECT
*
from
table_1 t1
where exists
(
select id from table_2 t2
where t1.id = t2.id or t1.other_id = t2.id;
)

SQL query join- unrelated tables

Can someone help me to join the two tables without any primary or secondary keys. Sample table is
TABLE 1
| ID | NAME |
| 1 | x |
| 2 | Y |
| 3 | z |
TABLE 2
| Num | NAME | DATE |
| 52 | X | 12-aug-17 |
| 53 | X | 11-apr-17 |
| 62 | X | 10-aug-11 |
| 12 | y | 2-jan-16 |
| 23 | Y | 3-apr-18 |
I want retrieve data from X
select *
from table2
where name = 'x';
| Num | NAME | DATE |
| 52 | X | 12-aug-17 |
| 53 | X | 11-apr-17 |
| 62 | X | 10-aug-11 |
Now I will get three data from table2. I'm little stuck after this step. I want to get top of data the from table 2 and combine with table one.
I want final output should be
| ID | NAME | Num | DATE |
| 1 | x | 52 | 12-aug-17 |
Can someone suggest me how can I join this table? Its easy to join when we have any primary key but here not the case
Thanks
You can use this:
SELECT TOP(1) table1.ID, table2.Num, table2.Name, table2.DATE
FROM table2 INNER JOIN table1 ON table1.NAME = table2.NAME
WHERE table2.NAME = 'x'
ORDER BY table2.DATE ASC
OR
SELECT table1.ID, table2.Num, table2.Name, table2.DATE
FROM table1 INNER JOIN
(SELECT TOP(1) * FROM table2 WHERE NAME = 'x' ORDER BY DATE ASC) table2
ON table1.NAME = table2.NAME
You need to get the maximum DATE using a subquery, as in:
select t1.id, t2.*
from table1 t1
join table2 t2 on t2.name = t1.name
where t2.date = (
select max(date) from table2 where name = 'x'
);

select data from table 1 and get cross reference values of table 2 when table1 column name is matching with row values of column1 in table2

Table 1
id | check | status
1 | abc | 1
2 | def | 3
Table 2
Column1 | rawvalue | to_be_updated_value|
check | abc | new
status | 3 | 3333
Please help me to write select statement to get the following output in Oracle11g
Expected Output:
id | check | status
1 | **new** | 1
2 | def | **3333**
This is the first thing I think you want to do (applies to SQL Server):
SELECT ISC.COLUMN_NAME, T2.*
FROM <YourDatabase>.INFORMATION_SCHEMA.COLUMNS ISC
INNER JOIN Table2 T2
ON T2.Column1 = ISC.COLUMN_NAME
WHERE ISC.TABLE_NAME = N'Table1';

How to join tables: Column equals value or is null?

I have the following table structure
Table1:
+--------+
| foo_id |
+--------+
| 1 |
| 2 |
| 3 |
+--------+
Table2:
+--------+--------+
| foo_id | bar_id |
+--------+--------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 3 | 2 |
+--------+--------+
Now, I want an output like this:
+--------+--------+
| foo_id | bar_id |
+--------+--------+
| 1 | 1 |
| 2 | null |
| 3 | null |
+--------+--------+
What came to my mind was sth like
select table2.foo_id, table2.bar_id
from table1
left join table2 on table1.foo_id = table2.foo_id
and table2.bar_id = 1
but it does not quite work, I get 3 lines for foo_id = 1.
Any ideas?
Thanks very much!
You only need to change from:
select table2.foo_id, table2.bar_id
from table1
left join table2 on table1.foo_id = table2.foo_id
and table2.bar_id = 1
to
select table1.foo_id, table2.bar_id
from table1
left join table2 on table1.foo_id = table2.foo_id
and table2.bar_id = 1
You mistakenly chose the wrong table to output foo_id from
select table2.foo_id, table2.bar_id
from table1
left join table2 on table1.foo_id = table2.foo_id
where table2.bar_id = 1 or table2.bar_id is null