How to delete rows from two tables which are in common with single query in postgresql - sql

I have two tables t1 and t2
table t1 as follows:
id name
1 x
2 y
3 z
table t2 as follows:
id name
1 a
121 b
131 c
Here I am selecting rows that are common in both the tables i.e.,
SELECT *
from t1,t2
where t1.id=t2.id;
Now I want to delete the rows when id=1 in both the tables at once. I have tried to delete the rows but I am able to delete only in one table but not both. Can anyone help me out in solving this.

You can do that with a data modifying common table expression
with common_ids as (
select id
from t1
intersect
select id
from t2
), t1_delete as (
delete from t1
where id in (select id from common_ids)
)
delete from t2
where id in (select id from common_ids);
Online example: http://rextester.com/NAQ26877

Related

Delete a record from a table referring to 2 or more columns from another table

Consider having 2 tables as follows.
Table 1:
Unit
SKU number
Active
A
1
Y
B
2
Y
c
3
Y
Table 2:
Unit
SKU number
description
X
4
Apple
B
2
Mango
Y
5
Grapes
z
6
Banana
I wanted to delete record B,2,Y from table 1 by referring to table 2 where values in columns Unit and SKU number match.
I tried using the following query but it didn't seem to work
DELETE FROM table1
WHERE (Unit, SKU_Number) IN (SELECT Unit, SKU_Number FROM table2);
The error was
An expression of non-boolean type specified in a context where a condition is expected, near ','
Can someone please help me understand what I am doing wrong here or help me rewrite the SQL query to achieve the required objective?
You could use similar logic with exists:
DELETE
FROM table1 t1
WHERE EXISTS (SELECT 1 FROM table2 t2
WHERE t2.Unit = t1.Unit AND t2.SKU_Number = t1.SKU_Number);
You can try using this query, assuming Unit of Table 1 is unique:
DELETE FROM table1
WHERE table1.Unit IN (
SELECT table1.Unit
FROM table1
LEFT JOIN table2 ON table1.Unit = table2.Unit
AND table1.SKU_Number = table2.SKU_Number
)
If unit is not an unique field, simply replace it with whichever field is unique, or with primary key of Table 1.
You can use inner join for delete:
DELETE t1
FROM table1 t1
INNER JOIN table2 t2
ON t1.unit=t2.unit and t1.SKU_Number = t2.SKU.Number

SQL Update using data from other tables and foreign keys

I am trying to update a column from table1 based off of data from two other tables.
Table 1 has columns id, columnIWantToUpdate, table2FK, table3FK
Table 2 has columns table2FK, table2_unique_id
Table 3 has columns table3FK, table3_unique_id
So I get table2_unique_id and table3_unique_id as inputs and I want to use the columns table2FK and table3FK to update table 1 based off of the unique_ids I received as input
One method uses filtering in the where clause:
update table1
set columnIWantToUpdate = ?
where exists (select 1
from table2 t2
where t2.table2FK = table1.table2FK
) or
exists (select 1
from table3 t3
where t3.table3FK = table1.table3FK
);
It is not clear if you want and and or for the conditions.

SQL Subquery Join and Sum

I have Table 1 & 2 with common Column name ID in both.
Table 1 has duplicate entries of rows which I was able to trim using:
SELECT DISTINCT
Table 2 has duplicate numeric entries(dollarspent) for ID's which I needed and was able to sum up:
Table 1 Table 2
------------ ------------------
ID spec ID Dol1 Dol2
54 A 54 1 0
54 A 54 2 1
55 B 55 0 2
56 C 55 3 0
-I need to join these two queries into one so I get a resultant JOIN of Table 1 & Table 2 ON column ID, (a) without duplicates in Table 1 & (b) Summed $ values from Table 2
For eg:
NewTable
----------------------------------------
ID Spec Dol1 Dol2
54 A 3 1
55 B 3 2
Notes : No. of rows in Table 1 and 2 are not the same.
Thanks
Use a derived table to get the distinct values from table1 and simply join to table 2 and use aggregation.
The issue you have is you have a M:M relationship between table1 and table2. You need it to be a 1:M for the summations to be accurate. Thus we derive t1 from table1 by using a select distinct to give us the unique records in the 1:M relationship (assuming specs are same for each ID)
SELECT T1.ID, T1.Spec, Sum(T2.Dol1) as Dol1, sum(T2.Dol2) as Dol2
FROM (SELECT distinct ID, spec
FROM table1) T1
INNER JOIN table2 T2
on t2.ID = T1.ID
GROUP BY T1.ID, T1.Spec
This does assume you only want records that exist in both. Otherwise we may need to use an (LEFT, RIGHT, or FULL) outer join; depending on desired results.
I can't really see your data, but you might want to try:
SELECT DISTINCT ID
FROM TblOne
UNION ALL
SELECT DISTINCT ID, SUM(Dol)
FROM TblTwo
GROUP BY ID
Pre-aggregate table 2 and then join:
select t1.id, t1.spec, t2.dol1, t2.dol2
from (select t2.id, sum(dol1) as dol1, sum(dol2) as dol2
from table2 t2
group by t2.id
) t2 join
(select distinct t1.id, t1.spec
from table1 t1
) t1
on t1.id = t2.id;
For your data examples, you don't need to pre-aggregate table 2. This gives the correct sums -- albeit in multiple rows -- if table1 has multiple specs for a given id.

Lookup two columns using SQL

I have a task. For Instance, I have 2 tables.
Table 1 columns ID, Name
Table 2 columns ID, Name
Data is like in Table 1:
ID Name
1 A
2 B
3 C
Data is like in Table 2:
ID Name
1 D
2 B
3 E
I want to write a SQL query which lookup two tables in both columns. I want to have the count, which records not matching (both columns) with table 2.
Here only one record was matched (2 B). So, I should get the count 2.
Thanks.
Use not exists to count the # of rows in table1 that are not in table2
select count(*) from mytable t1
where not exists (
select 1 from mytable t2
where t2.id = t1.id
and t2.name = t1.name
)

Search Row not exist in another table

Can anyone help me to write the query for below condition.
Table 1
ID Key
1 A
2 A
4 C
5 D
6 A
Table 2
ID Key
2 B
3 B
5 D
6 A
These are the two tables
I want a query in which the ID, which is not exist in Table1 corresponding to Table2, where deleted from table.
Example: ID = 1 row was completely deleted from Table1, and in which key are not match were also deleted
Example: ID = 2, exists in both tables but key are not same so the complete row also delete from Table1 not in Table2, I need a single query which is applicable to both condition
Thanks in advance
SELECT id, [Key]
FROM table1
EXCEPT
SELECT id, [Key]
FROM table2
You can try to check if exists in following:
DELETE FROM Table1
WHERE NOT EXISTS (
SELECT 1
FROM Table2 t2
WHERE Table1.Id = t2.Id
)
OR EXISTS (
SELECT 1
FROM Table2 t2
WHERE Table1.Id = t2.Id AND Table1.[Key] <> t2.[Key]
)
OUTPUT
ID Key
5 D
6 A