I have 2 tables, call Table1 and Table2. Table1 columns are id,name,col3.
Table2 has columns id, name , col3.
I want to extract all records from Table1 who's id and name (Both to satisfy condition)
not in Table2.
How to do that
Does the below assist:
SELECT
Table1.ID,
Table1.name,
Table1.col3,
Table2.ID
FROM
Table2 RIGHT JOIN Table1 ON (Table2.name = Table1.name) AND (Table2.ID = Table1.ID)
WHERE
Table2.ID Is Null;
How about this...
SELECT t1.*
FROM Table1 t1
LEFT OUTER JOIN Table2 t2
ON t1.id = t2.id AND t1.name = t2.name
WHERE t2.id IS NULL
I think you require values( id, name) which are not present in other table.
SELECT t1.*
FROM Table1 t1
LEFT JOIN Table2 t2
ON t1.id <> t2.id AND t1.name <> t2.name
WHERE t2.id is null
UNION
SELECT t2.*
FROM Table2 t2
LEFT JOIN Table1 t1
ON t1.id <> t2.id AND t1.name <> t2.name
WHERE t1.id is null
Related
I try to do an Case query, but it is not showing the desired success.
SELECT
t1.Id,
t1.amount,
t2.Id,
t1.singlePrice,
CASE t1.number
WHEN NULL THEN t3.number
END
FROM TABLE1 t1
LEFT JOIN TABLE2 t2 ON t1.Id = t2.fk_id
LEFT JOIN TABLE3 t3 ON t1.number = t3.number
WHERE t2.id = 487
Usually all numbers are in Table3.
But in Table1 there are numbers which are not existing in Table3.
What I want:
If the number is found in Table3 => use number ob Table3 otherwise use ist from Table1.
you should check for t3.number is null
SELECT
t1.Id,
t1.amount,
t2.Id,
t1.singlePrice,
CASE WHEN t3.number IS NULL
THEN t1.numnber
ELSE t3.number
END
FROM TABLE1 t1
LEFT JOIN TABLE2 t2 ON t1.Id = t2.fk_id
LEFT JOIN TABLE3 t3 ON t1.number = t3.number
WHERE t2.id = 487
Also possible
SELECT
t1.Id,
t1.amount,
t2.Id,
t1.singlePrice,
COALESCE (t3.number, t1.numnber)
FROM TABLE1 t1
LEFT JOIN TABLE2 t2 ON t1.Id = t2.fk_id
LEFT JOIN TABLE3 t3 ON t1.number = t3.number
WHERE t2.id = 487
How can I do something like select * from T1 inner join T2 on (T1.ID=T2.ID OR T1.ID2=T2.ID)
When I execute this code, it seems to fall in a infinity loop so I guess I'm wrong.
In other words, how can I match one of two columns from T1 to one column from T2
T1
ID ID2
1 10
2 20
T2
ID value
1 dummy10
20 dummy20
Result
ID ID2 value
1 10 dummy10
2 20 dummy20
Try to do like this:
select *
from T1, T2
where T1.ID = T2.ID or T1.ID2 = T2.ID
you can use 2 select statements with union, like this:
select
t1.ID,
t1.ID2,
t2.value
from Table1 as t1
inner join Table2 as t2 on t1.ID = t2.ID
UNION
select
t1.ID,
t1.ID2,
t2.value
from Table1 as t1
inner join Table2 as t2 on t1.ID2 = t2.ID
/* this will exclude values selected by other statement */
where t1.ID2 not in (select ID2 from Table1 inner join Table2 on Table1.ID = Table2.ID)
The only issue I can see with the code you provide is that you have not specified from which table you want the common column ID to be selected:
proc sql;
select
t1.*
,t2.value
from t1
inner join t2
on t1.id = t2.id or t1.id2 = t2.id;
quit;
Otherwise, your code should work. Perhaps the size of the data being joined is the problem?
I have table1 and table2. They have the same columns and the column ID is the one that i can use to connect the tables.
How can i run foreach statment that will update row Name in table1 with the value for column Name in table2?
I need this so i can fix the column Name in Table1 because it is incorect , and the good values for it are in table2
I tried using a single update statement but it takes forever to execute because both tables are with over 600 000 rows
update
table1 t1
set
(
t1.name
) = (
select
t2.name
from
table2 t2
where
t2.id = t1.id
and
rownum = 1
)
where exists (
select
null
from
table2 t2
where
t2.id = t1.id
);
For this query:
update table1 t1
set t1.name = (select t2.name from table2 t2 where t2.id = t1.id and rownum = 1)
where exists (select 1
from table2 t2
where t2.id = t1.id
);
You want an index on table2(id, name).
A simple inner join should work this.
UPDATE T1
SET T1.NAME = T2.NAME
FROM MyTable T1
INNER JOIN MyOtherTable T2
ON T1.ID = T2.ID
I have two tables T1 and T2
T1
-id
-columnA
-columnB
-columnC
T2
-id
-columnX
-columnY
-columnZ
I have a query like
Select t1.* t2.columnZ
from T1 t1
left join on T2 t2 on t1.id = t2.id
where t2.columnZ = 'test'
I want result like if "where t2.columnZ = 'test'" does not return any row then it should return value of columnZ as null value
Try:
SELECT t1.*
, t2.columnZ
FROM T1 t1
LEFT JOIN T2 t2
ON t1.id = t2.id
AND t2.columnZ = 'test'
You also missed a comma, and you had a misplaced on.
My requirement is like :
select (*ALL columns of table1, [count of rows of TABLE2 where TABLE1.id = Table2.id]
from TABLE1, TABLE2
where TABLE1.status = 'A'
Am now to SQL, please pardon if the question is too simple
select t1.col1, t1.col2, t1.col3, count(t2.id)
from table1 t1
left join TABLE2 t2 on t1.id = t2.id
where t1.status = 'A'
group by t1.col1, t1.col2, t1.col3
Using correlated subquery:
SELECT t1.*
,(SELECT COUNT(*) FROM TABLE2 t2 WHERE t1.id = t2.id) AS Counter
FROM TABLE1 t1
WHERE t1.status = 'A';