Join three Tables with 2 different Columns - sql-server

Here is the thing:
I have three tables:
Table1:
COL1 COL2
Table2:
COL2 COL3
Table3:
COL3 COL4
And I want to select the COL4 from Table3 when Table.COL3 = TABLE2.COL3, which the Table2.COL3 from when Table2.COL2 = Table1.COL2
It likes two join table, but when I use the following query it doesn't work.
SELECT * FROM
table3
INNER JOIN table2 ON table3.col3
=
(
SELECT table2.col3
FROM table2
INNER JOIN table1 ON
table1.col2 = table2.col2
)

You are selecting from table1 joining to the others. So table1 goes in the from.
SELECT TABLE3.col4
FROM TABLE1
JOIN TABLE2 ON TABLE2.COL2 = TABLE1.COL2
JOIN TABLE3 ON TABLE3.COL3= TABLE2.COL3

In the first table in the join statement. You have used a column name i.e. table3.col4 in stead of a table name i.e. table3. So, it's giving error.
Your correct query will be as follows
SELECT table3.col4 FROM table3
INNER JOIN table2
ON table3.col3 =
( SELECT table2.col3
FROM table2
INNER JOIN table1 ON
table1.col2 = table2.col2)

Related

Conditional where statement involving multiple columns in same table

I have 3 tables
Table1:
emp_id, code_1, code_2
Table2:
code_1, code_2
Table3:
emp_id, emp_ind
Table2 is a lookup table. I need to update Table3 checking code_1 and code_2 of Table1 present in Table2.
If code_2 in Table2 is null, then check if code_1 in Table1 and Table2 match, if yes update emp_ind in Table3 as Y.
If code_1 in Table2 is null, then check if code_2 in Table1 and Table2 match, if yes update emp_ind in Table3 as Y.
If code_1 and code_2 both are valued in Table2, check if both code1 and code2 in Table1 match with that of Table2, if yes update emp_ind in Table3.
Is there a way to do it simply instead of checking like below:
Update table_3 t3
Set t3.emp_ind = 'Y'
Where exists (
Select 1 from table1 t1
Inner Join table2 t2
on t1.code_1 = t2.code_1
Where t2.code_2 is null
And t2.emp_id= t3.emp_id
Union
Select 1 from table1 t1
Inner Join table2 t2
on t1.code_2 = t2.code_2
Where t2.code_1 is null
And t2.emp_id= t3.emp_id
Union
Select 1 from table1 t1
Inner Join table2 t2
on t1.code_1 = t2.code_1
And t1.code_2 = t2.code_2
Where t2.emp_id= t3.emp_id
)
You can combine the conditional joins using OR operator like this:
Update table_3 t3
Set t3.emp_ind = 'Y'
Where exists (
Select 1 from table1 t1
Inner Join table2 t2
on
(t1.code_1 = t2.code_1 AND t2.code_2 is null)
OR (t1.code_2 = t2.code_2 AND t2.code_1 is null)
OR (t1.code_1 = t2.code_1 AND t1.code_2 = t2.code_2)
Where t2.emp_id= t3.emp_id
)

SQL Server : query to get sum of values in Col1 of Table1 for a condition in Col2 of Table 2

I have two tables. Table1 has Col1 and Col2 and Table2 has Col1 and Col3.
So col1 name is common in both the tables.
I need a query that should do sum of matched strings for common ROW IDs (Table1-col1 value)
Attached this screenshot:
I would try this (udpated for nulls and empty spaces):
SELECT 'Success', SUM(t2.Col2) FROM Table1 t1 JOIN Table2 t2 ON t1.Col1 = t2.Col1 WHERE t1.Col2 = 'Success' UNION ALL
SELECT 'Failure', SUM(t2.Col2) FROM Table1 t1 JOIN Table2 t2 ON t1.Col1 = t2.Col1 WHERE t1.Col2 = 'Failure' UNION ALL
SELECT 'NULL', SUM(t2.Col2) FROM Table1 t1 JOIN Table2 t2 ON t1.Col1 = t2.Col1 WHERE t1.Col2 IS NULLUNION ALL
SELECT 'EMPTY SPACE', SUM(t2.Col2) FROM Table1 t1 JOIN Table2 t2 ON t1.Col1 = t2.Col1 WHERE t1.Col2 = ' '
will give you two rows with the results you wanted.
UNION ALL simply concatenates two or more rows (with the same columns); ALL tells to ignore duplicates (which is not a concern here and actually speeds up performance)
SUM of course sums all cells in a column
JOIN will "merge" your tables
A JOIN clause is used to combine rows from two or more tables, based on a related column between them.

Find rows only present in the result of one of two queries

I have two JOIN queries which give an output column of Id. How do I find all the Id values returned by query 1 but not returned by query 2?
select Id from Table1 join Table2;
select Id from Table2 join Table3;
SELECT id FROM dbo.Table1 INNER JOIN dbo.Table2 ON ...
EXCEPT
SELECT id FROM dbo.Table2 INNER JOIN dbo.Table3 ON ...;
try this one:
select Id from Table1 join Table2
union
select Id from Table2 join Table3

Using where condition in sql query

I have an sql query like this
Select col1, (select abc from table2 where def=1) as col2
From Table1 inner join table3 on Table1.id = table3.id
Where col2 = 4
The problem is that the where condition doesn't work. I get an error saying
Invalid column name 'col2'
Kindly help me fix this sql query.
Thanks in advance
You can define it in a CROSS APPLY and then reference in the SELECT and WHERE
SELECT col1,
col2
FROM Table1
INNER JOIN table3
ON Table1.id = table3.id
CROSS APPLY (SELECT abc
FROM table2
WHERE def = 1) C(col2)
WHERE col2 = 4
Using a CTE (Common Table Expression):
WITH SubQuery AS (Col2) {
SELECT
ABC
FROM
table2
WHERE
def = 1
}
SELECT
T.Col1,
S.Col2
FROM
SubQuery S,
Table1 T
INNER JOIN table3 t3
ON T.id = t3.id
WHERE
S.Col2 = 4
Although I must say I agree with the first comment - this makes no sense since your subquery is not correlated (joined) to the rest of your query...

Opposite Of An Inner Join Query

Table 1 2 columns: ID, Name
Table 2 2 columns: ID, Name
What is a query to show names from Table 1 that are not in table 2? So filtering out all the names in table 1 that are in table 2 gives the result query. Filtering is on ID not name.
Select * from table1
left join table2 on table1.id = table2.id
where table2.id is null
This should perform better than the left join...is null version. See here and here for comparisons.
select t1.id, t1.name
from table1 t1
where not exists(select null from table2 t2 where t2.id = t1.id)
Use this query
select
t1.*
from table1 t1
left outer join table2 t2
on t1.id=t2.id
where t2.id is null
this works by joining everything in t1 to whatever exists in t2. the where clause filters out all of the records that don't exist in t2.
SELECT Table1.ID, Table1.Name, Table2.ID
FROM Table1 LEFT OUTER JOIN Table2 ON Table1.ID = Table2.ID
WHERE Table2.ID IS NULL
I think that should do it.
Try like this:
select t1.*
from table1 as t1
where t1.id not in
(select distinct t2.id from table2 as t2);

Resources