Case in SQL join statement - sql

I have to use a conditional statement in join:
select * from A inner join B
on A.id = B.id
if B.id is null or B.id = '' or A.id is null or A.id = '' it should be A.name = B.name, instead of A.id = B.id
I have to use a conditional statement in join:
select * from A inner join B
on A.id = B.id
if B.id is null or B.id = '' it should be A.id2 = B.id2 instead of A.id = B.id.
Is it correct if I do something like this:
select *
from A inner join B on
(B.id is not null and B.id <> '' and A.id is not null and A.id <> '' and A.id = B.id) or
((B.id is null or B.id = '' or A.id is null or A.id = '') and A.name = B.name)

I think you will want something like this:
select * from A
inner join Bid on
(Bid.id is not null and Bid.id <> '' and A.id is not null and A.id <> '' and A.id = Bid.id)
inner join Bname on
((Bname.id is null or Bname.id = '' or A.id is null or A.id = '') and A.name = Bname.name)
that is to join on the B table twice, one join for when id is not null and the second join when id is null and you join on the name column instead. This works effectively like a case statement because the two joins to the B table are mutually exclusive.

Try this
select * from A inner join B
on (A.id = B.id) or
(A.name = B.name and (B.id is null or B.id = '' or A.id is null or A.id = ''))

try this code
DECLARE #b varchar(50)=(SELECT id FROM b)
DECLARE #a varchar(50)=(SELECT id FROM a)
if ((#b in (null , '')) and (#a in (null , '')))
BEGIN
select * from A inner join B
on
A.name = B.name
END
else
BEGIN
select * from A inner join B
on A.id = B.id
END

Related

Postgres how do I update all rows in a table with left join and case?

Assume that I have table A, and table B. Table B contains some rows from table A
How do I run an update like the following?
update table A
set A.column = case when B.id is present then B.column else null
from B where B.id = A.id OR B is null
EDIT
The original query is incomplete, this is the updated one i need help with
update table A
set A.column1 = case when B.id is present then B.column else null end,
set A.column2 = case when B.id is present then null else A.id end
from B
where B.id = A.id OR B is null
You can simply use a subquery:
UPDATE A SET A.column = (
SELECT B.column FROM B WHERE B.id = A.id
)
You can use a left join in the FROM clause as well to update multiple columns:
UPDATE A SET column1 = B.column1, column2 = B.column2
FROM A x LEFT JOIN B ON x.id = B.id
WHERE A.id = x.id
The table A in the FROM clause is only to allow a left join with B.

Unable to apply case statement in join condition

I have two tables TableA and TableB. For one of the record in TableB(id =1), I want to perform join on condition1(a.value = b.value) and for other records I want to join on condition2((a.value - b.value)/ a.val < 1).
But, I am getting syntax error at end. How to apply case condition in this scenario?
Select * from TableA a
LEFT JOIN TableB b
on a.id = b.id
and (
case when b.id = 1 then a.value = b.value
else (a.value - b.value)/ a.val < 1 end
)
Just use boolean logic:
select *
from TableA a left join
TableB b
on a.id = b.id and
(b.id = 1 and a.value = b.value or
b.id <> 1 and (a.value - b.value)/ a.val < 1
)

How do you display dynamic data using full outer join?

I have a table A and B with a common ID field. I would like to do a full outer join on these tables, selecting the ID and either 'table A' or 'table B', depending on which table the ID came from.
SELECT ID, ['tableA'|'tableB']
FROM A FULL OUTER JOIN B ON A.ID = B.ID
WHERE A.ID IS NULL OR B.ID IS NULL
Try this:
SELECT COALESCE(A.ID , B.ID) AS ID ,
CASE WHEN A.ID IS NOT NULL AND B.ID IS NULL THEN <tableA results>
WHEN A.ID IS NULL AND B.ID IS NOT NULL THEN <tableB results>
END AS results
FROM A FULL OUTER JOIN B ON A.ID = B.ID
WHERE A.ID IS NULL OR B.ID IS NULL
So this is what I came up with after a little help from Tab.
SELECT ID, SELECT CASE WHEN ID IN (SELECT ID FROM A) THEN 'tableA'
WHEN ID IN (SELECT ID FROM B) THEN 'tableB'
FROM A FULL OUTER JOIN B ON A.ID = B.ID
WHERE A.ID IS NULL OR B.ID IS NULL

SQL Server - Inner Join On - Conditional Statement

I am using SQL Server 2008 and have the following scenario:
I have Table a with fields id and groupId.
I have Table b with fields id and groupId also.
The rule in Table b, is that:
If a.id = 0, then a.groupId = b.groupId
Else a.id = b.id (in which case a.groupId = 0)
The 2 tables are also linked by agrId such that a.agrId = b.agrId
How can I join these tables, whilst satisfying the rule above?
Update: Apologies for the lack of clarity, I have updated the rule and added my attempt below:
select * from a
inner join b
on a.agrId = b.agrId
where (
(b.id > '0' and b.groupId = '0')
or
(b.groupId > '0' and b.id = '0')
)
Try this
SELECT *
FROM TableA A
INNER JOIN TableB B
ON (B.ID = 0 AND A.groupId = B.groupId)
OR (B.groupId = 0 AND A.id = B.id)
Al it needs is an expression that evaluates to true or false, to indicate if these two rows should be joined. So based on what you say, this would be the join condition:
select *
from tA a
inner join tB b
on (a.id = 0 and a.groupid = b.groupid)
or (a.groupid = 0 and a.id = b.id)
Try this:
SELECT *
FROM a INNER JOIN
b ON a.agrId = b.agrId
WHERE (b.id = '0' AND b.groupId = a.groupId)
or
(b.groupId = '0' AND b.id = b.id)

Multiple WHERE conditions WITH NOT IN

I am working on tuning a stored procedure. The stored procedure is doing a SELECT and has around 50 WHERE conditions. The SQL is similar to below. Can you please let me know if there is a better way to check these conditions
SELECT * FROM table A
JOIN table B
ON A.ID = B.ID
WHERE
(
(
A.name = 'abc'
and B.AID not in ( 111, 222)
)
or
(A.name = 'def'
and B.AID not in ( 222,1113,111,654,199,43,243,643,754244,2434)
)
or
(
A.name = 'etd'
and B.AID not in ( 111,345,54,34,454)
)
or
(
A.name = 'ent'
and B.AID not in ( 111,188,199,1647,128006)
)
or
(
A.name = 'yyy'
and B.AID not in (111,188,1113,1647)
)
or
(
A.name = 'uuu'
and B.AID not in (111,188,1113,1647)
)
or
(
A.name = 'jyf'
and B.AID not in (111,188,1647,344,45444,645)
)
or
(
A.name = 'tut'
and B.AID not in (111,222,1113,1647)
)
)
Create a table to map the names and IDs:
Name AID
------ ------
abc 111
abc 222
def 222
def 1113
..etc
Use a LEFT join to exclude matches:
SELECT * FROM table A
JOIN table B
ON A.ID = B.ID
LEFT JOIN Exclusions e
ON A.name = e.name
and B.AID = e.AID
WHERE e.name IS NULL -- not in exclusion table
or a NOT EXISTS:
SELECT * FROM table A
JOIN table B
ON A.ID = B.ID
WHERE NOT EXISTS(
SELECT null FROM Exclusions e
WHERE A.name = e.name
and B.AID = e.AID
)