How to do a SQL CASE query? - sql

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

Related

Select in Where Clause

Well, I did some research before posting here and I haven't find the correct solution.
Here is my SQL, I would like to improve the performance and remove select clause after where if possible by join or any other way.
The catch is Table1 is the first table to join and it is the same table in Where clause. I am not sure whether I am doing it right or wrong. I would like to know if there is any other efficient way to get the same result
SELECT T3.Id, T3.Name
FROM dbo.Table1 T1
JOIN dbo.Table2 T2 ON T1.Id = T2.Id
JOIN dbo.Table3 T3 ON T2.Name = T3.Name
WHERE T1.fId = (SELECT fId FROM dbo.Table1 WHERE Id = 1)
You can do this with a simple join:
SELECT T3.Id, T3.Name
FROM dbo.Table1 T1
inner JOIN dbo.Table2 T2 ON T1.Id = T2.Id
inner JOIN dbo.Table3 T3 ON T2.Name = T3.Name
inner join dbo.Table1 T4 on T4.fId = T1.fId and T4.Id= 1
Because you are using a WHERE clause in the subquery with a fixed value, you can simply replace the whole query by the value used
SELECT T3.Id, T3.Name FROM dbo.Table1 T1
JOIN dbo.Table2 T2 ON T1.Id = T2.Id
JOIN dbo.Table3 T3 ON T2.Name = T3.Name
WHERE T1.Id = 1
See if you want output of only T3 table then use "Exists" clause
SELECT T3.Id, T3.Name
FROM dbo.Table3 T3
where exists(
select t2.id from dbo.Table2 T2
inner join dbo.Table1 T1 on T1.Id = T2.Id
where T2.Name = T3.Name
and t1. Id = 1
)

Left Join; Only if there is at least one matching record

I need to join two pairs of tables. If there is an ID in Table1 that can also be found in Table3 I need to join the tables. If there is no matching ID from Table1 in Table3, I need to not join the tables.
Ex.
If there is at least one id in Table1 in Table3; do something that is effectively this:
SELECT *
FROM Table1 AS t1
INNER JOIN Table2 AS t2 ON t1.ID = t2.ID
LEFT JOIN Table3 AS t3 ON t1.ID = t3.ID
LEFT JOIN Table4 AS t4 ON t3.ID = t4.ID
If there are no IDs that match between Table1 and Table3; do something that is effectively this:
SELECT *
FROM Table1 AS t1
INNER JOIN Table2 AS t2 ON t1.ID = t2.ID
Just translating your question into SQL, you can do this:
IF EXISTS(SELECT * FROM Table1 T1 INNER JOIN Table3 T3 ON T1.ID=T3.ID)
SELECT *
FROM Table1 AS t1
INNER JOIN Table2 AS t2 ON t1.ID = t2.ID
LEFT JOIN Table3 AS t3 ON t1.ID = t3.ID
LEFT JOIN Table4 AS t4 ON t3.ID = t4.ID
ELSE
SELECT *
FROM Table1 AS t1
INNER JOIN Table2 AS t2 ON t1.ID = t2.ID

Oracle SQL: JOIN by dblink

I use next SQL-query in Oracle DB:
SELECT T1.*,
T3.*
FROM MyTable1 T1
INNER JOIN MyTable2 T2 ON T2.Id1 = T1.Id
LEFT JOIN MyTable3#dblink1 T3 ON T3.Id2 = T2.Id
This query is very simple and fast (about 1 min, T1 contain about 1 million rows, T3 more then 10 million rows). Now I want to use MyTable4 from dblink1 for filtering selected rows data. For it, I use subquery:
SELECT T1.*,
T3.*
FROM MyTable1 T1
INNER JOIN MyTable2 T2 ON T2.Id1 = T1.Id
LEFT JOIN (SELECT Sub_T1.*
FROM MyTable3#dblink1 Sub_T1
INNER JOIN MyTable4#dblink1 Sub_T2 ON Sub_T2.Id3 = Sub_T1.Id
WHERE
Sub_T2.MyColumn1 = 'required value') T3 ON T3.Id2 = T2.Id
But this query is too slow (more then 20min). If I rewrite this query to:
SELECT T1.*,
T3.*
FROM MyTable1 T1
INNER JOIN MyTable2 T2 ON T2.Id1 = T1.Id
LEFT JOIN MyTable3#dblink1 T3 ON T3.Id2 = T2.Id
LEFT JOIN MyTable4#dblink1 T4 ON T4.Id3 = T3.Id
WHERE
T4.MyColumn1 = 'required value'
Then my query work fast again, but I donn't like result (I want to see columns of T3 as null, if WHERE return false).
How to improve my second query, for speed up it?
Does phrasing the query with parentheses solve the problem?
SELECT T1.*,
T3.*
FROM MyTable1 T1 INNER JOIN
MyTable2 T2
ON T2.Id1 = T1.Id LEFT JOIN
(MyTable3#dblink1 T3 JOIN
MyTable4#dblink1 T4
ON T4.Id3 = T3.Id AND
T4.MyColumn1 = 'required value'
)
ON T3.Id2 = T2.Id;
Or, also:
SELECT T1.*,
T3.*
FROM MyTable1 T1 INNER JOIN
MyTable2 T2
ON T2.Id1 = T1.Id LEFT JOIN
MyTable3#dblink1 T3
ON T3.Id2 = T2.Id
EXISTS (SELECT 1 FROM MyTable4#dblink1 T4 WHERE T4.Id3 = T3.Id AND T4.MyColumn1 = 'required value'
)

LEFT JOIN ambiguous columns into one column in SQL statement?

Is it possible to LEFT JOIN two columns of a same nature, i.e. join t2.Name and t3.Name into a single column (dataproperty) Name in a T-SQL statement?
SELECT t1.ID,
t1.RowID,
t2.name,
t3.Name
FROM MasterTable T1
LEFT OUTER JOIN RightTable1 T2
ON T1.RowID = T2.ID
LEFT OUTER JOIN RightTable2 T3
ON T1.RowID = T3.ID
WHERE t1.ID= " & ID & ";
...or do I have to load the tables into separate datasets and join them programatically (for each ... next) into a new dataset?
Regards,
Libor
It depends on hat you mean by "join" - here are a few possibilities:
If one is null and the other isn't, use ISNULL:
SELECT DISTINCT
t1.ID,
t1.RowID,
ISNULL(t2.name, t3.Name) Name
FROM MasterTable T1
LEFT OUTER JOIN RightTable1 T2 on T1.RowID = T2.ID
LEFT OUTER JOIN RightTable2 T3 on T1.RowID = T3.ID
If you want to concatenate them:
SELECT DISTINCT
t1.ID,
t1.RowID,
t2.name + t3.Name Name
FROM MasterTable T1
LEFT OUTER JOIN RightTable1 T2 on T1.RowID = T2.ID
LEFT OUTER JOIN RightTable2 T3 on T1.RowID = T3.ID
If you want two rows (a UNION):
SELECT DISTINCT
t1.ID,
t1.RowID,
t2.Name
FROM MasterTable T1
LEFT OUTER JOIN RightTable1 T2 on T1.RowID = T2.ID
UNION ALL
SELECT DISTINCT
t1.ID,
t1.RowID,
t3.Name
FROM MasterTable T1
LEFT OUTER JOIN RightTable2 T3 on T1.RowID = T3.ID
Note that DISTINCT is necessary because unless you can relate T2 and T3 you basically do a cross join (cartesian product) between T2 and T3 so you need to eliminate the duplicates that are created.
SELECT t1.ID,
t1.RowID,
t2.name + t3.Name AS JoinedName
FROM MasterTable T1
LEFT OUTER JOIN RightTable1 T2
ON T1.RowID = T2.ID
LEFT OUTER JOIN RightTable2 T3
ON T1.RowID = T3.ID
WHERE t1.ID= " & ID & ";

sql - double condition check in sql

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