SQL - Joining multiple selects/tables - sql

My first query...
SELECT
t1.a, t1.b, t1.c,
t2.a, t2.b, t2.c
FROM t1
LEFT JOIN t2 ON t1.a = t2.a
WHERE t1.b = '000000'
AND LENGTH(t1.a) > '5'
AND t1.c <> 'Y';
My second query...
SELECT
t1.a,
t3.b as testMe
FROM t1
LEFT JOIN t3 ON t1.a = '0' + LEFT(t3.a, 5)
WHERE t1.a = '017941';
Both of these queries work fine by themselves, but I need them combined into one result set. Worth noting is that the where clause in the second query is there for testing purposes, but when I remove it the whole thing crashes. Not sure if that means I need something to filter by, or it's timing out? The database I'm using is Pervasive.
My failed query...
SELECT
t1.a, t1.b, t1.c,
t2.a, t2.b, t2.c,
t3.b as testMe
FROM t1
LEFT JOIN t2 ON t1.a = t2.a
LEFT JOIN t3 ON t1.a = '0' + LEFT(t3.a, 5)
WHERE t1.b = '000000'
AND LENGTH(t1.a) > '5'
AND t1.c <> 'Y';
I've shorted my code to better show what I have going on, but the actual code can be found here: http://codeshare.io/A2aB9

You can try to use those two sets as subselect statements and join them together. I pulled your join condition between the two queries out as a separate column in order to keep testMe in the result set.
SELECT *
FROM (SELECT t1.a t1_a
, t1.b t1_b
, t1.c t1_c
, t2.a t2_a
, t2.b t2_b
, t2.c t2_c
FROM t1
LEFT JOIN t2
ON t1.a = t2.a
WHERE t1.b = '000000'
AND LENGTH(t1.a) > '5'
AND t1.c <> 'Y') a
LEFT JOIN (SELECT t1.a t1_a
, t1.b t1_b
, t1.c t1_c
, t3.a t3_a
, t3.b t3_b
, '0' + LEFT(t3.c, 5) t3_c
, LEFT(t3.c, 5) AS testMe
FROM t1
LEFT JOIN t3
ON t1.a = '0' + LEFT(t3.c, 5)) b
ON a.t1_a = c.t3_c;

Related

Inner join differences

I have a table1 (a,b,c) and table2(a,b,c)
What's the difference between
select * from table1 T1 inner join table2 T2 on
T1.a=T2.a and T1.b = t2.b and T1.c = T2.c
and
select * from table1 T1 inner Join table2 T2 on T1.a = T2.a where
T1.b= T2.b and T1.c = T2.C
Is is the same ? and which one is better?
Thanks
With inner joins there are no difference. It is only when you start using left/right joins, that you will see differences.
For LEFT JOINS, if you had
select *
from table1 T1 LEFT join
table2 T2 on T1.a=T2.a
and T1.b = t2.b
and T1.c = T2.c
It would include all rows fromo table1 and only rows from table2 where fields a,b and c matched.
If you had
select *
from table1 T1 inner Join
table2 T2 on T1.a = T2.a
where T1.b= T2.b
and T1.c = T2.C
This would include rows from table1 and those from table2 where a is equal, and then filter on b and c.
SQL Fiddle DEMO
I always find this visual representation usefull.
SQL SERVER – Introduction to JOINs – Basic of JOINs
For your queries, this doesn't change anything.
And in term of performance, your RDBMS is able to understand that it's the same.
But considering you have more joins, this would change the readability of the query.
Example :
SELECT
*
FROM
table1 T1
INNER JOIN table2 T2
ON T1.a = T2.a
AND T1.b = T2.b
AND T1.c = T2.c
LEFT JOIN table3 T3
ON T3.x = T1.a
AND T3.status = 1
WHERE
T1.a > 100
You can understand faster which condition works with which table in INNER/LEFT JOIN

Case (or alternative) statement to select which query criteria to use

I want to use a conditional statement to determine which set of query criteria to use. Open to any suggestions. Currently I have
Select p.1, p.2, p.3, Count(p.3) ASDF
from table1 t1
left join table2 t2 on t2.a = t1.a
inner join table3 t3 on t3.b = t1.b
where
case t1.abc
when 'x' then
t2 in ('1111','2222')
else t3.123 in ('asdf','qwer')
and t2.ad = '123'
and t1.bn = '456'
and t3.mk = '678'
group by p.1, p.2, p.3
I'm getting "Incorrect syntax near" the first 'in' and the following 'else'. Is what I am asking possible? If not, what is a good way to do this?
This should work as you want:
SELECT p.1, p.2, p.3, COUNT(p.3) ASDF
FROM table1 t1
LEFT JOIN table2 t2
ON t2.a = t1.a
INNER JOIN table3 t3
ON t3.b = t1.b
WHERE ((t1.abc = 'x' AND t2.somecolumn IN ('1111','2222'))
OR t1.abc <> 'x' AND t3.123 in ('asdf','qwer'))
AND t2.ad = '123'
AND t1.bn = '456'
AND t3.mk = '678'
GROUP BY p.1, p.2, p.3
Select p.1, p.2, p.3, Count(p.3) ASDF
from table1 t1
left join table2 t2 on t2.a = t1.a
inner join table3 t3 on t3.b = t1.b
where (t1.abc = 'x'AND t2 in ('1111','2222')) OR
(t1.abc <> 'x' AND t3.123 in ('asdf','qwer'))
and t2.ad = '123'
and t1.bn = '456'
and t3.mk = '678'
group by p.1, p.2, p.3

Select rows where some values are not in other table

A and B of table T3 are the same as A and B from T1.
Basically what I need to do is select all the values that aren't on T3.
If there is a line with A,B on T3 I don't wanna show it.
SELECT T1.A, T1.B, T1.C
FROM T1, T2
WHERE T1.X=T2.X
AND NOT EXISTS
(
SELECT T3.A, T3.B
FROM T3
)
Any help?
Thanks
SELECT T1.A, T1.B, T1.C
FROM T1 INNER JOIN T2 ON T1.X=T2.X
WHERE NOT EXISTS
(
SELECT 1 FROM T3
WHERE T3.A = T1.A AND T3.B = T1.B
)
select T1.A,T1.B,T1.C
from T1
inner join T2
on T1.X=T2.X
left join T3 on T1.A=T3.A and T1.B=T3.B
where T3.A is null
You can also do that using subqueries like
SELECT T1.A, T1.B, T1.C
FROM T1, T2
WHERE T1.X=T2.X
AND T1.A NOT IN (SELECT T3.A FROM T3)
AND T2.B Not IN (SELECT T3.B FROM T3)
SELECT T1.A, T1.B, T1.C
FROM T1, T2
WHERE T1.X=T2.X
AND (T1.A, T1.B) NOT IN (SELECT T3.A, T3.B FROM T3 )

Oracle SQL correlated update

I got three tables:
t1.columns: a,c
t2.columns: a,b
t3.columns: b,c,d
Now what I want is to update t1.c with t3.d. But I can't just update t1 from t3 using t1.c = t3.c I also have to go though t3.b = t2.b and t1.a = t2.a.
I've tried something like this:
UPDATE table1 t1
SET t1.c = (select t3.d
from table2 t2, table3 t3
where t2.b = t3.b and t1.a = t2.a)
WHERE EXISTS ( SELECT 1 FROM table2 t2, table3 t3 WHERE t1.c = t3.c and t1.a = t2.a);
This code generates error-msg: ORA-01427: single-row subquery returns more than one row
If there is a one-to-many relationship between t1 and t2 or between t2 and t3 you will get many matches for each row in t1. If you know that all rows in t3 that belong to the same row in t1 have the same value in d, then you can use DISTINCT to remove (identical) duplicates.
UPDATE table1 t1
SET t1.c = (select DISTINCT t3.d
from table2 t2, table3 t3
where t2.b = t3.b and t1.a = t2.a)
WHERE EXISTS ( SELECT 1 FROM table2 t2, table3 t3 WHERE t1.c = t3.c and t1.a = t2.a);
You have a subquery that is returning more than one row. Use rownum to get just one row:
UPDATE table1 t1
SET t1.c = (select d
from (select t3.d
from table2 t2 join table3 t3
on t2.b = t3.b
where t1.a = t2.a
) t
where rownum = 1
)
WHERE EXISTS ( SELECT 1 FROM table2 t2, table3 t3 WHERE t1.c = t3.c and t1.a = t2.a);
Sorry for the confusion but I solved it:
UPDATE table t1
SET t1.c = (select t3.d from table3 t3, table2 t2
where t1.a = t2.a and t2.b = t3.b and t3.c = t1.c)
WHERE EXISTS ( SELECT 1 FROM table1 t1, table2 t2 WHERE t1.a = t2.a and t2.b = t3.b and t3.c = t1.c)
UPDATE table1 t1
SET t1.c = (select MAX(t3.d)
from table2 t2, table3 t3
where t2.b = t3.b and t1.a = t2.a)
WHERE EXISTS ( SELECT 1 FROM table2 t2, table3 t3 WHERE t1.c = t3.c and t1.a = t2.a);

multiple condition in join

how can i use two or more condition on join?
i want to replace this query with join version of it:
select *
from t1,t2
where t1.a=t2.b and t1.c=t2.d and t1.e=t2.f
how could it be?
This should work
select *
from t1
join t2
on t1.a = t2.b and t1.c = t2.d and t1.e = t2.f