I have two queries I have created in Access and I'm a step away from arriving at the data I need. I need to subset in some way.
Table1 and Table2 (Actually query1 and query2). They both have 3 fields: Email, Matcher and List.
I need to get all the results from Table2 where Email does not exist in Table1.
I found some posts about using an outer join and where null clause. I could not get it to work though. Didn't post what I tried here in case I was off course.
select t2.*
from table2 t2
left join table1 t1 on t2.email = t1.email
where t1.email is null
SELECT t2.*
FROM table2 t2
WHERE NOT EXISTS (
SELECT *
FROM table1 t1
WHERE t1.email = t2.email
)
Related
I have two tables and have mentioned the layout of tables in the below screenshot.
Using Id column I need to join two tables. Whenever there is a match with Id (example Id is a2), I need to take the corresponding values from Table2. In some scenario, Id from Table1 will not be there in Table2 (example Id is a1). In that case, I have to take values from Table2 where the value of Id in Table2 is all.
For better clarification, I have mentioned the Expected output in the below screenshot.
I tried the below code, but don't seem to be working.
select distinct t1.Id,t1.Name,t2.Phone1,t2.Phone2
from Table1 t1
left join Table1 t2 on t1.Id = case when t2.Id is null then t1.Id else t2.Id end
Any help is greatly appreciated.
You can use two left joins, where the second brings in the default values:
select t1.*, coalesce(t2.phone1, t2d.phone1) as phone1, coalesce(t2.phone2, t2d.phone2) as phone2
from table1 t1 left join
table2 t2
on t1.id = t2.id left join
table2 t2d
on t2d.id = 'All' and t2.id is null;
So, I would like the next query for postgres:
SELECT name
FROM Table1 as T1
WHERE T1.id = (
SELECT id
FROM Table2 AS T2
WHERE T2.active=true)
So, I need to get all the values from the first table, whose id matches the ones set as active in another table.
You don't need a sub query for this. Use a join. It will be a lot more efficient.
SELECT T1.name
FROM Table1 as T1
INNER JOIN Table2 as T2 ON T2.id = T1.id AND T2.active=true
The equality operator imposes that the subquery should return a single record. You want IN instead, which accepts a resultset:
SELECT T1.name
FROM Table1 as T1
WHERE T1.id IN (SELECT id FROM Table2 AS T2 WHERE T2.active)
This can also be expressed with EXISTS:
SELECT T1.name
FROM Table1 as T1
WHERE EXISTS (SELECT 1 FROM Table2 AS T2 WHERE T2.id = T1.id AND T2.active)
Note that in Postgres condition T2.active = true can be shortened T2.active.
For performance, you want an index on Table2(id, active) and another on Table1(id).
I have some set of records, but now i have to select only those records from this set which have theeir Id in either of the two tables.
Suppose I have table1 which contains
Id Name
----------
1 Name1
2 Name2
Now I need to select only those records from table one
which have either their id in table2 or in table3
I was trying to apply or operator witin inner join like:
select *
from table1
inner join table2 on table2.id = table1.id or
inner join table3 on table3.id = table1.id.
Is it possible? What is the best method to approach this? Actually I am also not able to use
if exist(select 1 from table2 where id=table1.id) then select from table1
Could someone help me to get over this?
Use left join and then check if at least one of the joins has found a relation
select t1.*
from table1 t1
left join table2 t2 on t2.id = t1.id
left join table3 t3 on t3.id = t1.id
where t2.id is not null
or t3.is is not null
I would be inclined to use exists:
select t1.*
from table1 t1
where exists (select 1 from table2 t2 where t2.id = t1.id) or
exists (select 1 from table3 t3 where t3.id = t1.id) ;
The advantage to using exists (or in) over a join involves duplicate rows. If table2 or table3 have multiple rows for a given id, then a version using join will produce multiple rows in the result set.
I think the most efficient way is to use UNION on table2 and table3 and join to it :
SELECT t1.*
FROM table1 t1
INNER JOIN(SELECT id FROM Table2
UNION
SELECT id FROM Table3) s
ON(t.id = s.id)
Alternatively, you can use below SQL as well:
SELECT *
FROM dbo.Table1
WHERE id Table1.IN ( SELECT table2.id
FROM dbo.table2 )
OR Table1.id IN ( SELECT table3.id
FROM Table3 )
I want to join the two tables above to show the appropriate Authorizationcode based on the NameID.
If the NameID matches, then will show corresponding Authorizationcode,
If it does not, then it will show the other Authorizationcode
NameID and OrderID are uniqueidentifiers.
The result should be:
cyclophosphamide - Auth01234
Adriamycin RDF - Auth01234
Neulasta - Auth04567
Not able to join based on NULL NameID. Please suggest.
This is a bit tricky. Here is a method that uses two left joins. The condition on the second one only chooses the NULL row from table2 (a cross join could also be used):
select t1.name, coalesce(t2.authorizationcode, t2null.authorizationcode) as authorizationcode
from table1 t1 left join
table2 t2
on t1.nameid = t2.nameid left join
table2 t2null
on t2null.nameid is null;
This is ANSI standard and should work in most databases.
I would approach this with a UNION
SELECT DISTINCT NameID, AuthorisationCode
FROM
( SELECT NameID, AutorisationCode
FROM table1 t1 INNER JOIN
table2 t2 ON t1.nameid = t2.nameID
UNION
SELECT NameID, AutorisationCode
FROM table1 t1 LEFT OUTER JOIN
table2 t2 ON t1.nameid = t2.nameID
) DATA
Try this one:
SELECT t1.Name, ISNULL(t2.AuthorizationCode, t3.AuthorizationCode) AS AuthorizationCode
FROM Table1 AS t1
LEFT JOIN Table2 AS t2 on t1.NameID = t2.NameID AND t1.OrderID = t2.OrderID
LEFT JOIN Table2 AS t3 on t3.NameID IS NULL AND t1.OrderID = t3.OrderID
I assume you also want to join on OrderID. If not please make your question more clear.
Your screenshots are from management studio so a SQL Server specific answer.
SELECT t2.Name, CA.AuthorizationCode
FROM Table2 t2
CROSS APPLY
(
SELECT TOP (1) *
FROM Table1 t1
WHERE t1.OrderId = t2.OrderId
AND (t1.NameId = t2.NameId OR t1.NameId IS NULL)
ORDER BY t1.NameId DESC -- not null prioritised over null
) CA
It would need an index on table1 (OrderId, NameId)
I have a SQL Insert statement that needs to insert records into another table only if the the record doesn't exist in table2 or the zip code has changes in table1. I have tried the following but it throws an error and it is the logic I am looking for:
INSERT INTO table2
SELECT id, zip
FROM table1 t1
JOIN table2 t2
ON t1.id = t2.id and t1.zip <> t2.zip
I also need it to insert the records if the id doesn't exist at all in table2. I have googled the crap out of this and can't seem to find the solution anywhere.
What about this?
INSERT INTO table2
SELECT t2.id, t2.zip
FROM table1 t1
LEFT OUTER JOIN table2 t2
ON t1.id = t2.id
WHERE (t1.id IS NULL OR t2.zip <> t1.zip)
Also, be sure to clarify which table's id and zip columns you are asking for.
You should always include column lists when doing inserts. Second, your query doesn't quite capture your logic. You need a left outer join to find the records that don't exist in the second table. Perhaps this might do what you want:
INSERT INTO table2(id, zip)
SELECT id, zip
FROM table1 t1 LEFT JOIN
table2 t2
ON t1.id = t2.id
WHERE (t1.zip <> t2.zip) or (t2.zip is null)
You just need a WHERE NOT EXISTS clause
INSERT INTO table2
SELECT id, zip
FROM table1
WHERE NOT EXISTS (SELECT 1 FROM table2 WHERE table2.id = table1.id AND table2.zip = table1.zip)