Could anyone help with how to take a single column:
ID
1
2
3
4
and show all possible distinct pairings in 2 columns:
ID ID
1 2
1 3
1 4
2 3
2 4
3 4
You can join the column on itself and arbitrarily decide that the left hand side will always be smaller than the right hand side (as the example shows):
SELECT t1.col, t2.col
FROM mytable t1
JOIN mytable t2 ON t1.col < t2.col
do self join
select t.id,t2.id from t t1 join t t2 on t.id<t1.id
A Simple Cartesian product should do
select distinct a.id as id_a, b.id as id_b
from test a, test b
where a.id<b.id;
Related
I am trying to join the following two tables T1 and T2 on T1.id=T2.id, T1.MonA=T2.MonB such that
Whenever MonA=MonB (iow an entry can be found in both tables),
perform an ordinary join. This is the case for instace for ID A,
MonA=MonB=3
If a MonB entry is in table T2 but no equal MonA entry can be found in
Table T1, the join shall take from table T1 the row where MonA is
maximal. In the sample tables, this is the case for both last rows.
MonA from T1 which are not in T2 shall be ignored
The condition T1.id=T2.id is a necessary precondition, so this always needs to be true!
Table T1
ID MonA Data
A 2 BBB
A 3 CCC
B 4 DDD
B 5 EEE
B 11 EEE
Table T2
ID MonB Organ
A 3 Liver
B 5 Heart
B 7 Kidney
Here is, how the result should look like
ID MonA MonB Data Organ
A 3 3 CCC Liver
B 5 5 EEE Heart
B 11 7 EEE Kidney
I need this to be performed in Teradata SQL and honestly have no idea currently how to tackle the problem. Thanks for help!
EDIT: The may be several entries with identical ID, MonA=MonB, but different Data/Organ columns and i want all of them in the resulting table.
Lets do a join of t1 with t2 as follows
--gets all of the matching records by (id,mona) pairs from t1 with (id,monb) from t2
select a.id,a.mona,b.monb,a.data,b.organ
from t1 a
join t2 b
on a.id=b.id
and a.mona=b.monb
union all /*Here you want only from t2 not there in t1 by id*/
select b.id,x.mona,b.monb,x.data,b.organ
from t2 b
left join t1 a
on a.id=b.id
and a.mona=b.monb
left join (select row_number() over(partition by id order by mona desc) as rnk
,id
,mona
,data
from t1
)x
on b.id=x.id
and x.rnk=1 /*pick up only the largest values arranged by mona*/
where a.mona is null /*Gets only the missing records from t2 which are not in t1*/
This should return the expected result:
SELECT t1.id,t1.mona,t2.monb,t1.data,t2.organ
FROM t1
JOIN t2
ON t1.id=t2.id
QUALIFY
Row_Number()
Over (PARTITION BY t2.id, t2.organ
-- prefer same entry in both tables
ORDER BY CASE WHEN t1.mona = t2.monb THEN 1 ELSE 2 END
-- otherwise take max monA
,t1.mona DESC -- ) = 1
I have two tables identical to each other like below
table 1
col1
1
2
3
4
5
table 2
col1
1
2
3
4
5
is there way to write a SQL query to join every row of table 1 to every row of table 2?
Do you want a Cartesian product? If so, use cross join:
select t1.col1, t2.col2
from table1 t1 cross join
table2 t2;
It sounds like an inner join as they are identical tables:
select t1.col, t2.col, ...
from table t1
inner join t2 on t1.col = t2.col
I have the following tables:
T1 T2 Desired result
CA CB CA CC CA CB
1 2 1 3 1 4
1 4 1 2 2 1
1 3 1 5 2 3
2 1 2 4
2 3
3 6
3 1
4 ...
I need to make a join between T1 and T2 (using column CA) and return only those rows which the values in CB do not exists in T2.CC
A simple way to achieve that is using the following query:
SELECT T1.* FROM T1 INNER JOIN T2 ON t1.CA = t2.CA AND
t1.CB NOT IN (SELECT CC FROM T2 WHERE T2.CA = T1.CA)
I think the previous query is not very efficient. For that reason I am looking for something better
Any help will be appreciated
Generally, a more efficient means of achieving this sort of result is finding records which fail a simpler join condition. Those can be found by doing an outer join and checking for null, as follows:
select t1.ca, t1.cb
from t1 left outer join t2 on t1.ca=t2.ca and t1.cb=t2.cc
where t2.ca is null;
I think you just want not exists:
select t1.*
from t1
where not exists (select 1
from t2
where t2.ca = t1.ca and t2.cb = t1.cb
);
For performance, you want an index on t2(ca, cb).
I'm not sure how to explain what I need but here's the data first:
Table 1
District
-1
3
2
1
3
Table 2
ID ID_Name
1 Main 1
2 Main 2
3 Main 3
How do I join the tables so that it looks like this?
District
-1
Main 3
Main 2
Main 1
Main 3
I'm assuming the second column is named Name for this, but you can do it with a COALESCE and a LEFT JOIN:
Select Coalesce(T2.Name, Str(T1.District)) As District
From Table1 T1
Left Join Table2 T2 On T1.District = T2.Id
assuming table 2 have
Table 2
ID col2
1 Main 1
2 Main 2
3 Main 3
you could use a left join
select table1.Distric, table2.col2
from table1
left join table2 on table1.dictrict = t2.ID
order by table2 col2
You can use left join:
Select coalesce(t2.col, t1.District) from table1 t1
left join table2 t2 on t1.District = t2.Id
Why does this query produce duplicates in some scenarios?
Table_1
ID
1
2
3
Table_2
ID
1
2
4
Table_3
ID
1
3
4
Query:
SELECT COALESCE(Table_1.ID, Table_2.ID, Table_3.ID)
FROM Table_1
FULL OUTER JOIN TABLE_2
ON Table1.ID=Table_2.ID
FULL OUTER JOIN TABLE_3
ON Table1.ID=Table3.ID;
Result:
1
2
3
4
4
The query duplicates all values where T1 is null and T2/T3 share the same value. Duplicates are removed for any other combination.
This is a little bit hard to explain. If you show the other ids, you will see the full range of what happens:
"coalesce" "id1" "id2" "id3"
1 1 1 1
2 2 2 .
3 3 . 3
4 . 4 .
4 . . 4
You can see the results here.
So, You get one row because t1 & t2 create a row with t2.id = 4 and t1.id = null when they don't match. Then, you get the same thing when t3.id = 4. The comparison is to t1.id -- so you get another row. There is no comparison to t2.id.
I suspect that you intend logic more like this:
select coalesce(t1.id, t2.id, t3.id)
from t1 full join
t2
using (id) full join
t3
using (id);
Here is the SQL Fiddle.