I have two table, in SQL; the first table(T1) is this:
ID
----
401
402
403
634
635
636
The second table(T2) is this:
ID1 | VALUE1 | ID2 | VALUE2
---- -------- ----- -------
401 | TEST | 634 | SAMPLE1
402 | MYVAL | 635 | TRUE
The result i need is this:
T1.ID | T2.ID1| T2.VALUE1 | T2.ID2 | T2.VALUE2
------ ------- ----------- -------- ----------
401 | 401 | TEST | 634 | SAMPLE1
402 | 402 | MYVAL | 635 | TRUE
634 | 401 | TEST | 634 | SAMPLE1
635 | 402 | MYVAL | 635 | TRUE
The value 403 and 636 of T1 must not be present in the result because don't have any reference in T2.
There is a way to accomplish this with some INNER JOIN? I'm using MS SQL Server 2017.
Wouldn't this just be in IN clause?
SELECT *
FROM dbo.Table1 T1
JOIN dbo.Table2 T2 ON T1.ID IN (T2.ID1,T2.ID2)
DB<>Fiddle
Suppose you can do simple join tables like this:
select t1.id, t2.id1, t2.value1, t2.id2, t2.value2
from t1
join t2 on (t1.id = t2.id1 or t1.id = t2.id2)
You need INNER JOIN AND UNION. Try like
(SELECT T1.ID , T2.ID1, T2.VALUE1 , T2.ID2 , T2.VALUE2 FROM
T1 INNER JOIN T2 ON t1.ID = T2.ID1)
UNION
(SELECT T1.ID , T2.ID1, T2.VALUE1 , T2.ID2 , T2.VALUE2 FROM
T1 INNER JOIN T2 ON t1.ID = T2.ID2)
Please, try with belwo query:
SELECT T1.ID , T2.ID1, T2.VALUE1 , T2.ID2 , T2.VALUE2
FROM dbo.Table1 T1
LEFT OUTER JOIN dbo.Table1 T2 ON T1.ID IN (T2.ID1,T2.ID2)
WHERE T2.ID1 IS NOT NULL
You could use OR in your JOIN condition:
SELECT T1.ID, T2.ID1, T2.VALUE1, T2.ID2, T2.VALUE2
FROM T1
INNER JOIN T2
ON T1.ID = T2.ID1 OR T1.ID = T2.ID2
Demo
Is this query output what you need?
SELECT t1.ID, t2.ID1, t2.VALUE1, t2.ID2, t2.VALUE2
FROM T1 t1
INNER JOIN T2 t2 ON t1.ID = t2.ID1
UNION
SELECT t1.ID, t2.ID1, t2.VALUE1, t2.ID2, t2.VALUE2
FROM T1 t1
INNER JOIN T2 t2 ON t1.ID = t2.ID2
You seem to want:
select v.id, t2.*
from table2 t2 cross apply
(values (t2.id1), (t2.id2)) as v(id);
I don't see what table1 has to do with the result set. Hence, join does not seem very useful.
Related
I joined two tables
SELECT
t1.column1,
t2.column2
FROM
t1 JOIN t2 cu
ON t1.id = t2.id AND t1.col LIKE 'A%'
SELECT
t1.column1,
t2.column2
FROM
t1 JOIN t2 cu
ON t1.id = t2.id AND t1.col LIKE 'B%'
How could i intersect these tables so I can have the following output (ids that have both the A% and B%)
join 1
id | col
---┼------
1 | Axxxx
2 | Axxxx
join 2
id | col
---┼-------
1 | Bxxxx
3 | Bxxxx
Final output
id | col
---┼-------
1 | Axxxx
1 | Bxxxx
Thank you
If I understood correctly, and what you want is only the ID's that have both A% and B%, then this is your answer
SELECT
t1.column1,
t2.column2
FROM
t1 JOIN t2 cu
ON t1.id = t2.id AND ((t1.col LIKE 'A%' AND t2.col like 'B%')
OR (t1.col LIKE 'B%' AND t2.col like 'A%'))
Based on your sample you don't need intersect but a simple union
SELECT
t1.column1,
t2.column2
FROM
t1 JOIN t2 cu
ON t1.id = t2.id AND t1.col LIKE 'A%'
union
SELECT
t1.column1,
t2.column2
FROM
t1 JOIN t2 cu
ON t1.id = t2.id AND t1.col LIKE 'B%'
adapt the sintax for union at your db/sql
Also try it this way ... as you are looking for only repeatable IDs ... you could just count them from inner select
select column1, column2 from (
select column1, column2, count(column1) over (partition by column1) [Counted] from (
SELECT
t1.column1,
t2.column2
FROM
t1 JOIN t2 cu
ON t1.id = t2.id AND t1.col LIKE 'A%'
UNION
SELECT
t1.column1,
t2.column2
FROM
t1 JOIN t2 cu
ON t1.id = t2.id AND t1.col LIKE 'B%'
) src
) src2 where Counted > 1
I want to join these three tables with outer join
Table1
ID NAME
123 KING
456 KONG
Table2:
ID A_DATE VALUE
123 9/1/2015 1000
123 8/1/2015 1100
123 7/1/2015 1200
456 8/1/2015 900
456 7/1/2015 800
Table3:
Date
9/1/2015
Query Using:
select t1.ID, t1.NAME, t2.A_Date t2.Value
from Table1 t1, Table2 t2
where t1.ID = t2.ID(+)
and t2.A_Date = (Select Date from Table3)
Current Results:
ID NAME A_DATE VALUE
123 KING 9/1/2105 1000
This query only giving me common value.
Results Required:
ID NAME A_DATE VALUE
123 KING 9/1/2105 1000
456 KONG NULL NULL
You should write this using ANSI JOIN syntax, and put the constraint on the second table in the ON clause.
SELECT t1.ID, t1.NAME, t2.A_Date, t2.Value
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.ID = t2.ID AND t2.A_Date = (SELECT Date FROM Table3)
Using implicit joins, you need to test specifically for NULL.
select t1.ID, t1.NAME, t2.A_Date t2.Value
from Table1 t1, Table2 t2
where t1.ID = t2.ID(+)
and t2.A_Date IS NULL OR t2.A_Date = (Select Date from Table3)
I am trying to join two tables such that I am getting only a first match from the Right table instead of every match in Table2.
So if the query is:
SELECT T1.Name, T2.Dates
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.ID = T2 = ID
WHERE T1.Name = 'John'
I would like to see
John | 14/11/14
Joe | 10/10/2014
Jane | 25/10/2014
Instead of
John | 14/11/2014
John | 12/10/2014
Joe | 10/10/2014
Jane | 25/10/2014
Jane | 26/10/2014
Which join should I use?
You need to decide which row, you should select. Min or max as commented.
SELECT T1.Name,
( SELECT MIN( T2.Dates) FROM Table2 T2 WHERE T1.ID = T2 = ID) AS Dates
FROM Table1 T1
WHERE T1.Name = 'John'
The ANSI standard function row_number() can be a big help here. It is supported by most databases, so you can do:
SELECT T1.Name, T2.Dates
FROM Table1 T1 LEFT JOIN
(SELECT t2.*, ROW_NUMBER() OVER (PARTITION BY t2.ID ORDER BY t2.DATE DESC) as seqnum
FROM Table2 t2
) T2
ON T1.ID = T2.ID AND seqnum = 1
WHERE T1.Name = 'John';
In your question, you have only one column from the second table, so you can also do this with aggregation:
SELECT t1.ID, t1.Name, MAX(t2.Date)
FROM Table1 T1 LEFT JOIN
Table2 T2
ON t1.ID = t2.ID
WHERE T1.Name = 'John'
GROUP BY t1.ID, t1.Name;
Query
SELECT a.name,
MAX(b.Dates)
FROM tbl1 a
JOIN tbl2 b
ON a.id=b.id
WHERE a.name='John'
GROUP BY a.name;
Demo
I have two tables:
id
1
2
3
4
t1 AND t2
id | related_id
1 | 2
1 | 3
Where t2 is relationship table between t1 records. What is the best way to get desired output?
t1.id | t1_copy.id
1 | NULL -- want to get this NULL row
1 | 2
1 | 3
Simple JOIN would almost work, however it doesn't give me the first NULL row.
SELECT t1.id, t1_copy.id FROM t1
LEFT JOIN t2 ON t1.id = t2.id
LEFT JOIN t1 t1_copy ON t1_copy.id = t2.related_id
WHERE t1.id = 1
P.S: Yes, I do realize that desired output is wacky.
Seems like a simple UNION should do the trick
SELECT
id,
null as copy_id
FROM
t1
WHERE
exists (select * from t2 where t1.id = t2.id)
UNION ALL
SELECT
t1.id,
t2.related_id
FROM
t1
INNER JOIN t2
ON t1.id = t2.id
SQL Fiddle
SELECT DISTINCT t1.id, t1_copy.id FROM t1
LEFT OUTER JOIN t2 ON t1.id = t2.id
WHERE t1.id = 1
Say we have the following tables t1 and t2:
t1:
id | column_1
-------------
1 | 1
2 | 2
t2:
id | column_2
-------------
2 | 2
3 | 3
and we want to find the following result:
id | column_1 | column_2
------------------------
1 | 1 |
2 | 2 | 2
3 | | 3
This basically is the union of a right join with a left join. The following code works but feels clumsy:
(
SELECT t1.id, t1.column_1, t2.column_2
FROM t1
LEFT JOIN t2 ON t1.id = t2.id
)
UNION
(
SELECT t2.id, t1.column_1, t2.column_2
FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
)
Is there a better way to achieve this?
select a.id, t1.column_1, t2.column_2
from (
select id from t1
union
select id from t2
) a
left outer join t1 on a.id = t1.id
left outer join t2 on a.id = t2.id
Try this one:
SELECT t1.id, t1.column_1, t2.column_2
FROM t1
FULL OUTER JOIN t2 ON (t1.id = t2.id)
Edit: Doesn't work, MySQL does not know FULL OUTER JOIN.
Have a look here:
http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/
Haven't tried this myself, but this might work:
SELECT t1.id, t1.column_1, t2.column_2, t2a.column_2
FROM t1
LEFT JOIN t2 ON t1.id = t2.id
RIGHT JOIN t2 AS t2a ON t1.id = t2a.id