Missing data from converting OR condtion in Join to Another JOIN - sql-server

Few days back I asked how to change OR condtion (left join) into another left join. Here is the code below :
SELECT *
FROM Table1 t1
LEFT JOIN Table2 t2
ON t1.ID = t2.ID
OR (
t1.col1 = t2.col1
AND t1.col2 = t2.col2
AND t1.col3 = t2.col3
AND t2.ID IS NULL
)
I converted to OR condition like below:
SELECT *
FROM Table1 t1
LEFT JOIN Table2 t2
ON t1.ID = t2.ID
LEFT JOIN Table2 t3
ON (
t1.col1 = t3.col1
AND t1.col2 = t3.col2
AND t1.col3 = t3.col3
AND t2.ID IS NULL
)
But i am not getting records for second left join in seperate row.
With OR condition result are like :
T1.ID T2.ID
1 a
2 b
3 c
4 d
But with second left join its lile below:
T1.ID T2.ID
1 a
2 b
3 c
4rth record is from second left join. How can i bring that in left join. I dont have option for using UNION ALl as per company standards.
Please help.
Thanks

First of all please notice that you're using t2 instead of t3 in the second left join :
SELECT *
FROM Table1 t1
LEFT JOIN Table2 t2
ON t1.ID = t2.ID
LEFT JOIN Table2 t3
ON (
t1.col1 = t3.col1
AND t1.col2 = t3.col2
AND t1.col3 = t3.col3
AND t3.ID IS NULL
)
Anyhow, without understanding what you'r trying to do and how do you want to see your result, it's hard to answer you.

You're using left join which just gives you everything from left result set. Since, you want data from t3 which is in right, change second left join to be either right join or full join, depending on your requirements. One of the following query can give you correct result:
Editing 14-03-2015
SELECT *
FROM Table1 t1
FULL OUTER JOIN Table2 t2
ON t1.ID = t2.ID
FULL OUTER JOIN Table2 t3
ON (
t1.col1 = t3.col1
AND t1.col2 = t3.col2
AND t1.col3 = t3.col3
AND t2.ID IS NULL
)

Related

Different Filters for Different Columns

I am relatively new to SQL and I have the following question. I have the following code:
Select * from table1
LEFT JOIN table2 ON table1.name = table2.name and table1.id = table2.id
LEFT JOIN (SELECT id FROM table2 GROUP BY id) newtable ON table1.id = newtable.id
As both left joins uses data from the same table, is it possible to combine the two joins into one? How would the filters work in this case?
If your goal just to join table2 based on distinct values, then you can use WHERE and GROUP BY:
Select
*
from table1 t1
LEFT JOIN table2 t2
ON t1.name = t2.name and t1.id = t2.id
WHERE t1 id in (SELECT s2.id FROM table2 s2 GROUP BY s2.id)

MSSQL Query framing

I have 3 tables like master and 2 child tables . I am using join condition but did not get as expected client needs.
Query:
select * from(
select a.id as mid,b.id,b.val from ##mastertable a right join ##table1 b
on a.id=b.id ) as c inner join ##table2 d on c.mid=d.id
Kindly provide any other way to get proper result.
Try:
Select a.id, b.id, b.val
From (mastertable a
Right Join table1 b
On a.id = b.id)
Inner Join table2 d
On a.id = d.id;
This should work :
select * from (
(select master.id from master_table) master
LEFT OUTER JOIN
(select table1.id, table1.value from table_1) table1
ON
master.id = table1.id
FULL OUTER JOIN
(select table2.id, table2.value from table_2) table2
ON
table1.id = table2.id
AND
table1.val = table2.val
)
;
Try below: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=2b58625961c444997829abb45d40417b
select * from master m inner join
(select table2.id as id1, table2.value as val1, table1.value as val2, table1.id as id2
from table2 left join table1
on table1.id=table2.id and table1.value=table2.value)x on m.id=x.id1
This will be same as you expected
select
m.id, t1.id, t1.value, t2.id, t2.value
from
m
left join
t1
on
m.id = t1.id
left join
t2
on
t1.id = t2.id and t1.value = t2.value
order by m.id, t1.value
here M is your master table, T1 is table1 and t2 is table2

Choose Maximum among Duplicates

I am writing a query in SQL to be used for SSRS 2005 which is
select t1.Category, t2.condition,'1' as hasCondition from t1
left outer join t2 on t1.ID = t2.ID
left outer join t3 on t2.cID = t3.cID
where t3.clientID = 6
union
select t1.Category, t2.condition, '0' as hasCondition from t1
left outer join t2 on t1.ID = t2.ID
and it returns the data as:
Category Condition hasCondition
Behavioural Tourette's Syndrome 0
Communications Impaired Speech 0
Dexterity Osteoarthritus 0
Dexterity Osteoporosis 0
Dexterity Other Dexterity 0
Dexterity Rheumatoid Arthritus 0
Emotional Bipolar Disorder 0
Emotional Clinical Depression 0
Emotional Depression 0
Emotional Depression 1
Emotional Gulf War Syndrome 0
Emotional Gulf War Syndrome 1
Now I want to select only one record from duplicates rows/results for example:
Category-Emotional, condition-Depression which hasCondition '1' or 'true'
I hope I made it clear using the example.
Please help me.
Thanks.
It's not 100% clear what you want, but I think you're just looking for the MAX() of the final field...
SELECT
category, condition, MAX(has_condition) AS hasCondition
FROM
(
select t1.Category, t2.condition,'1' as hasCondition from t1
left outer join t2 on t1.ID = t2.ID
left outer join t3 on t2.cID = t3.cID
where t3.clientID = 6
union
select t1.Category, t2.condition, '0' as hasCondition from t1
left outer join t2 on t1.ID = t2.ID
)
AS data
GROUP BY
category, condition
If you wanted to simplify that whole query...
select
t1.Category,
t2.condition,
MAX(CASE WHEN t3.clientID = 6 THEN 1 ELSE 0 END) as hasCondition
from
t1
left outer join
t2
on t2.ID = t1.ID
left outer join
t3
on t3.cID = t2.cID
and t3.clientID = 6
group by
t1.Category,
t2.condition
SELECT category, condition, MAX(hasCondition)
FROM (
select t1.Category, t2.condition,'1' as hasCondition from t1
left outer join t2 on t1.ID = t2.ID
left outer join t3 on t2.cID = t3.cID
where t3.clientID = 6
union
select t1.Category, t2.condition, '0' as hasCondition from t1
left outer join t2 on t1.ID = t2.ID)
GROUP BY category, condition

How to get Count of Joined Rows

There is this SQL Statement
SELECT t1.Name
,Count(t2.SubID) Totals -- I don't know how to do it.
FROM Table t1
INNER JOIN Table2 t2 ON t1.ID = t2.SubID
Thanks.
Guessing... change the JOIN and add GROUP BY
SELECT t1.Name
,Count(t2.SubID) AS Totals
FROM Table t1
LEFT OUTER JOIN Table2 t2 ON t1.ID = t2.SubID
GROUP BY t1.Name
The LEFT OUTER JOIN allows you to find count zero rows per t1.Name
SELECT t1.Name, Count(t2.SubID) Totals
FROM Table t1
INNER JOIN Table2 t2 ON t1.ID = t2.SubID
GROUP BY t1.Name

Change Sql "Not In" To "Left Outer Join"

I don't want to use "not in" this sql query. How can I do it? thanks
SELECT
T2.Sno,
T2.Name,
T1.description,
T2.UserCode
FROM
Table1 AS T1 (nolock)
INNER JOIN T2 (nolock)
ON T1.UserCode = T2.UserCode
WHERE
g.xid= #p_xid
and T2.Sno not in (select Gid from T3 (nolock))
Assuming there is no row in T2 where Sno is null and in T3 where Gid is null:
SELECT
T2.Sno,
T2.Name,
T1.description,
T2.UserCode
FROM
Table1 AS T1 WITH (nolock)
INNER JOIN T2 WITH (nolock)
LEFT JOIN T3 WITH (NOLOCK)
ON T2.Sno = T3.Gid
ON T1.UserCode = T2.UserCode
WHERE
g.xid= #p_xid
and T3.Gid IS NULL
If you have multiple T3 rows per T2.Sno = T3.Gid, you'll need DISTINCT in a JOIN.
Without DISTINCT, it's a different query
With DISTINCT, it's an extra step.
I'd use NOT EXISTS which avoids this.
SELECT
T2.Sno,
T2.Name,
T1.description,
T2.UserCode
FROM
Table1 AS T1 (nolock)
INNER JOIN T2 (nolock)
ON T1.UserCode = T2.UserCode
WHERE
g.xid= #p_xid
and not exists (select * from T3 (nolock) where T3.Gid = T2.Sno)

Resources