SQL QUERY FOR PAIRS - sql

I have the following values in my table
A B
1 2
2 3
4 5
2 1
5 6
7 6
6 5
what a sql query in order to find the results which have a pair so this is the output is
1 2
2 1
5 6
6 5

Seems like there're already great solutions:
SELECT t.A, t.B FROM table AS t1 INNER JOIN table AS t2 ON t1.A = t2.B AND t1.B = t2.A

WITH CTE(A,B) AS
(
SELECT 1, 2 UNION ALL
SELECT 2, 3 UNION ALL
SELECT 4, 5 UNION ALL
SELECT 2 ,1 UNION ALL
SELECT 5, 6 UNION ALL
SELECT 7, 6 UNION ALL
SELECT 6 ,5
)
SELECT C.A,C.B
FROM CTE AS C
JOIN CTE AS C2 ON C.A=C2.B AND C.B=C2.A

This is another way to do it. With Self join
SELECT t1.*
FROM table1 t1
table1 t2 ON t1.a = t2.b AND t2.a = t1.b

Doing an Inner join will help you in this case:
SELECT t.col1, t.col2
FROM Tablename t
INNER JOIN Tablename t2
ON t.col1 = t2.col2 AND t.col2 = t2.col1
This will give you the required result:
Sample Output :
Innings
Totalplayer
2
1
1
2
6
5
5
6
View on DB Fiddle

Related

Combining and checking table value on SQL (ORACLE)

Table 1
no name col1
1 a a_1
2 b b_1
Table 2
id name parent
a_1 zz c_1
b_1 yy d_1
c_1 aa null
d_1 bb e_1
e_1 dd1 null
what i want to show is showing the all list name. for example table 1 name a has col1 name a_1 it will show the name on table 2, and then check the parent in the table 2 and show it and keep checking until it found null. the example is like below.. im sorry for my bad explanation
t1_name t2_name t2_name t2_name
a zz aa
b yy bb dd1
or shows like below
t1_name t2_name
a aa/zz
b dd1/bb/yy
what I've done is this query
select t1.name,t2.name as folder from table1 as t1 inner join table2 as t2 on t1.col1=t2.id
and I don't know how to check again in query... I am using oracle version 12.2.0.1.0 in SQL developer any help?
You want to get the rows from the first table and then recursively fetch all the rows from the second table until you reach a null parent, so you do:
with cte(NAME,
PARENT,
CURRENTPATH) as
(select t1.NAME,
t2.PARENT,
t2.NAME as CURRENTPATH
from TABLE1 t1
join TABLE2 t2 on t1.COL1 = t2.ID
union all
select t1.NAME,
t2.PARENT,
t1.CURRENTPATH || '/' || t2.NAME as CURRENTPATH
from cte t1
join TABLE2 t2 on t2.ID = t1.PARENT)
select NAME,
CURRENTPATH
from cte
where PARENT is null;
You can use the hierarchical query as following:
SQL> -- Your data
SQL> with table1(no,name,col1) as
2 (SELECT 1, 'a','a_1' FROM DUAL UNION ALL
3 SELECT 2, 'b','b_1' FROM DUAL
4 ),
5 table2 (id, name, parent) as
6 (select 'a_1', 'zz', 'c_1' from dual union all
7 select 'b_1', 'yy', 'd_1' from dual union all
8 select 'c_1', 'aa', null from dual union all
9 select 'd_1', 'bb', 'e_1' from dual union all
10 select 'e_1', 'dd1', null from dual)
11 -- Your query starts from here
12 SELECT
13 T1.NAME AS T1_NAME,
14 T2.NAMES AS T2_NAMES
15 FROM TABLE1 T1
16 JOIN (
17 SELECT
18 T2.ID,
19 SYS_CONNECT_BY_PATH(T2.NAME, '/') AS NAMES,
20 ROW_NUMBER() OVER(PARTITION BY ID ORDER BY LEVEL DESC) AS L
21 FROM TABLE2 T2
22 CONNECT BY T2.PARENT = PRIOR T2.ID
23 ) T2 ON T1.COL1 = T2.ID
24 WHERE L = 1;
T1_NAME T2_NAMES
------- ---------------
a /aa/zz
b /dd1/bb/yy
SQL>
Cheers!!
Which Oracle version are you using?

Oracle pl/sql permutation and combination

I am not sure what is the correct search term for this, please let me know if there's already an answer for this.
eg:
I have these data
A
B
C
D
E
What is the best way to calculate the addition of each possible combination? Such as:
A
A+B
A+C
A+D
A+E
A+B+C
A+B+D
A+B+E
A+C+D
A+C+E
A+C+D+E
A+B+C+D
A+B+C+E
A+B+C+D+E
B
B+C
B+D
B+E
B+C+D
B+C+E
B+C+D+E
C
C+D
C+E
...
The list goes on.......
Is there any way to achieve this?
The 5 data are not fixed. I might have 10.. 20 or 50 or 1000 :(
Thank you.
In SQL, you can almost do this with this set of left joins:
select (t1.col + coalesce(t2.col, 0) + coalesce(t3.col, 0) +
coalesce(t4.col, 0) + coalesce(t5.col, 0)
) as sumcombo
from t t1 left join
t t2
on t1.col < t2.col left join
t t3
on t2.col < t3.col left join
t t4
on t3.col < t4.col left join
t t5
on t4.col < t5.col;
It doesn't quite work, because you can never get just "A" for instance. Instead:
with t as (
select col
from table
union all
select NULL
from dual
)
select (t1.col + coalesce(t2.col, 0) + coalesce(t3.col, 0) +
coalesce(t4.col, 0) + coalesce(t5.col, 0)
) as sumcombo
from table t1 left join
t t2
on t1.col < t2.col or t2.col is null left join
t t3
on t2.col < t3.col or t3.col is null left join
t t4
on t3.col < t4.col or t4.col is null left join
t t5
on t4.col < t5.col or t5.col is null;
This can be solved by a hierarchical query. First of all, build a further child column col2 for connection:
-- your test data set
with testdata as
(select 'A' as col from dual
union
select 'B' from dual
union
select 'C' from dual
union
select 'D' from dual
union
select 'E' from dual),
-- create child column
testdata2 as
(select t.col as col1, t.col as col2 from testdata t)
select level, sys_connect_by_path(col1, '/') path
from testdata2 t
connect by prior col1 < col2
order by level, sys_connect_by_path(col1, '/');
result:
1 /A
1 /B
1 /C
1 /D
1 /E
2 /A/B
2 /A/C
2 /A/D
2 /A/E
2 /B/C
2 /B/D
2 /B/E
2 /C/D
2 /C/E
2 /D/E
3 /A/B/C
3 /A/B/D
3 /A/B/E
3 /A/C/D
3 /A/C/E
3 /A/D/E
3 /B/C/D
3 /B/C/E
3 /B/D/E
3 /C/D/E
4 /A/B/C/D
4 /A/B/C/E
4 /A/B/D/E
4 /A/C/D/E
4 /B/C/D/E
5 /A/B/C/D/E

How to join oracle tables

I have two tables.
Table 1
ID STRING
1 ABC
2 CDE
3 FGH
Table 2
ID STRING
1 xyz
2 uvw
4 abc
I want the output as
ID STRING STRING2
1 ABC xyz
2 CDE uvw
3 FGH null
4 null abc
which join should I use. Is it possible to do this in simple SQL query?
with
t1 as
(select 1 id, 'ABC' string from dual
union
select 2, 'CDE' from dual
union
select 3, 'FGH' from dual
),
t2 as
(select 1 id, 'xyz' string from dual
union
select 2, 'uvw' from dual
union
select 4, 'abc' from dual)
select COALESCE(t1.id,t2.id) id, t1.string, t2.string string2
from t1 full outer join t2 on (t1.id = t2.id)
order by 1
What you can do is use Union to combine two different result sets. That will give you exactly what you're looking for:
SELECT tab1.ID,
tab1.name,
tab2.name2
FROM tab1 tab1
LEFT JOIN tab2 tab2 ON tab1.ID = tab2.ID
UNION
SELECT tab2.ID,
tab1.name,
tab2.name2
FROM tab1 tab1
RIGHT JOIN tab2 tab2 ON tab1.ID = tab2.ID
You can see that here-> http://sqlfiddle.com/#!4/cf9e2/10
Hope this helps!!!
I guess a full join would be correct
select * from tab1 t1 full join tab2 t2 on t1.id = t2.id

UNION ALL Sql query - how to bind three tables

I have the following tables:
T1
ID PRIORITY
1 1
2 1
3 2
4 4
T2
ID SERVICE
1 PSTN
1 ADSL
3 ADSL
T3
ID DEVICE
1 BSC1
3 BSC7
4 BSC7
I want as output
ID PRIORITY SERVICE/DEVICE
1 1 PSTN
1 1 ADSL
1 1 BSC1
2 1
3 2 ADSL
3 2 BSC7
How to bind those tables using UNION ALL? Also I must put WHERE clause for T1 WHERE PRIORITY!=4
Total number in output table for one id should be the summary of T2+T3 (FOR ID=1 2+1=3) but for ID=2 it also SHOULD exist in table output with blank second column.
Thank you
If you are okay using just a UNION and not UNION ALL this should give you what you want
SELECT t1.Id, t1.Priority, COALESCE(t2.Service, '') AS [Service/Device]
FROM t1
LEFT JOIN t2 ON t1.Id = t2.Id
WHERE t1.Priority != 4
UNION
SELECT t1.Id, t1.Priority, COALESCE(t3.Device, '') AS [Service/Device]
FROM t1
LEFT JOIN t3 ON t1.Id = t3.Id
WHERE t1.Priority != 4
SQL Fiddle example
select T1.id , T1.PRIORITY ,T2.SERVICE as service/Device from t1
left outer join T2 on T2.id=T1.id where T1.PRIORITY!=4
union all
select T1.id , T1.PRIORITY ,T3.DEVICE as service/Device from t1
left outer join T3 on T3.id=T1.id where PRIORITY!=4

Joining two tables in a select

I have two tables:
TABLE 1
ID VALUE
1 ABC
2 DEF
3 GHI
4 JKL
5 XYZ
TABLE 2
ID T1_ID VALUE
1 1 A
2 1 B
3 2 A
4 3 A
5 3 B
6 4 B
I want to select all rows from TABLE 1 which have a TABLE 2 row for Values A AND B.
This would be rows 1 and 3 (not 2 because it has only A, not 4 because it has only B).
Can I do this without a subquery?
(Note: I also need to query against values in table 1 so I can't just query table 2.)
Tadaaah! Without a subquery.
select distinct
t1.*
from
Table1 t1
inner join Table2 t2a on t2a.t1_ID = t1.ID and t2a.VALUE = 'A'
inner join Table2 t2b on t2b.t1_ID = t1.ID and t2b.VALUE = 'B'
SELECT t1.ID,
t1.VALUE
FROM Table1 t1
JOIN Table2 t2
ON t1.ID = t2.T1_ID
WHERE t2.VALUE IN ( 'A', 'B' )
GROUP BY t1.ID,
t1.VALUE
HAVING COUNT(DISTINCT t2.VALUE) = 2