SQL Server 2008
Table A looks like:
A_ID v1 v2 v3
---------------------------
1 d e f
1 a b c
1 a b d
2 d a b
2 e f g
3 d e f
3 e f g
3 d a b
and Table B is similar:
B_ID v1 v2 v3
---------------------------
Q a b c
Q b a c
Q a b d
R d e f
R a b c
R d e f
P e f g
P d a b
What I need back from these two tables are the (A_ID, B_ID) pairs, if any, where each row of Table B where B_ID = any one value has one matching row in Table A where A_ID = any one value. In other words, I need to the complete matching set in A for each full set of triples in B--no super sets or subsets. The value of B_ID and A_ID is immaterial.
I thought partitioning would be the way to go, since I already have the column that naturally partitions A and B, and I also thought I could pre-select which paritions where JOINed by ensuring only partitions with matching numbers of rows would be attempting. I haven't been able to do either--partitioning both tables was easy, but I see no way to tell the join to only act on the partitions.
In this example, (2,P) would be returned because all rows in Set P match all rows in Set 2. Result (1, R) would NOT be returned because all rows of Set R are not matched by all rows of Set 1, etc.
Using symetric difference:
SELECT DISTINCT a1.A_ID, b1.B_ID
FROM A a1,B b1
WHERE NOT EXISTS (
(SELECT v1,v2,v3
FROM A WHERE A.A_ID = a1.A_ID
EXCEPT
SELECT v1,v2,v3
FROM B WHERE B.B_ID = b1.B_ID
)
UNION ALL
(
SELECT v1,v2,v3
FROM B WHERE B.B_ID = b1.B_ID
EXCEPT
SELECT v1,v2,v3
FROM A WHERE A.A_ID = a1.A_ID)
);
LiveDemo
Related
I need a recommendation.
I have two tables. Table 1 is the main table and table 2 is the table that I initially thought to join Table 1 through a left join, table 2 is much larger than table 1. What would be the best performing way to join Table 1 and Table 2 being the union condition that Column b is equal to column b or that column c is equal to column c and column d is equal to column d, that is, any of these conditions is met but no empty values are met. This without using OR in the left join due to the poor performance it would have and the execution time. I appreciate any help.
Note: table 1 and table 2 is the result of 40 lines query. Database do not support recursive query. The database is sap hana.
Table 1
ID
column b
column c
column d
1
d
g
j
2
e
h
k
3
f
i
Table 2
ID_2
column b
column c
column d
4
d
g
5
k
6
i
Desired Result
ID
column b
column c
column d
ID_2
1
d
g
J
4
2
e
h
k
5
3
f
i
6
Use two left joins:
select t1.*,
coalesce(t2_b.id_2, t2_c.id_2, t2_d.id_2) as id_2
from table1 t1 left join
table2 t2_b
on t1.b = t2_b.b left join
table2 t2_c
on t1.c = t2_d.c and t2_b.b is null
table2 t2_d
on t1.d = t2_d.d and t2_c.c is null;
Note that for optimal performance, you want three indexes:
table2(b, id_2)
table2(c, id_2)
table2(d, id_2)
Suppose I have a table A as a 'dictionary':
A type code name
1 a x
1 b y
2 a z
2 b t
I want to translate the following table B in to names:
B c1 c2
a a
a b
b b
Where B.c1 is the code of A.type=1 and B.c2 is the code of A.type=2.
The expected result is
x z
x t
y t
If there is only one column in B needs to be translated, it is easy.
SELECT A.name
FROM A, B
WHERE A.type = 1
AND B.c1=A.code
Using joins;
select a1.type,b.c1,a2.type,b.c2 from b
left outer join a as a1 on b.c1= a1.code and a1.type=1
left outer join a as a2 on b.c2= a2.code and a2.type=2
I have 2 tables joined with inner join with a column
Now rows would have
A B
C D
E G
P Q
Z F
This row I need to compare with Master Relation Table
Column1 Column2
A B
D C
E F
So based on the above condition I need to show records in a report
A B Do Not Show
C D Do Not Show
E G Show
P Q Show
Z F Show
You can do it this using LEFT JOIN:
SELECT T1.Col1,T1.Col2,
CASE WHEN T2.Column1 IS NOT NULL THEN 'Do Not Show' ELSE 'Show' END AS Result
FROM Table1 T1 LEFT JOIN
Table2 T2 ON
(T1.Col1=T2.Column1 AND T1.Col2=T2.Column2)
OR (T1.Col1=T2.Column2 AND T1.Col2=T2.Column1)
Result:
Col1 Col2 Result
---------------------------
A B Do Not Show
C D Do Not Show
E G Show
P Q Show
Z F Show
Sample result in SQL Fiddle
I'm building matching rules for data reconciliation systems and need your advise on adjusting my sql for it as it currently doesn't return what I need.
There are 2 source tables:
Table X Table Y
--------------------- ----------------------
Exec_ID From To Exec_ID From To
1 A B 1 B C
2 A B 2 B C
3 A B 3 B C
4 A B
5 B C
Matching conditions are:
X.To = Y.From
X.Exec_ID = Y.Exec_ID
if there is A -> B and then B -> C, it should return A -> C in the end.
if there is only A -> B and no further B -> C, it should return A -> B.
So the output should be the following.
From To
---------
A C
A C
A C
A B
SQL I'm using is:
select X.From, Y.To
from x
left outer join y on
x.To = Y.From
and x.Exec_ID = y.Exec_ID
It returns the values like
A C
A C
A C
A Null
So the last record is incorrect as it should be A B. Please help to adjust.
Check for null?
select X.From, [To] = COALESCE(Y.To, X.To)
from x
left outer join y on
x.To = Y.From
and x.Exec_ID = y.Exec_ID
I want to combine 2 table into 1 table as example below
Table1
100
200
300
Table2
A B C
D E F
G H I
OutputTable
100 A B C
100 D E F
100 G H I
200 A B C
200 D E F
200 G H I
300 A B C
300 D E F
300 G H I
Thank you.
If you already have the new table then:
insert into new_table (field1,field2,field3,field4)
select
a.field1
,b.field2
,b.field3
,b.field4
from a
cross join b
If you do not have the table:
select
a.field1
,b.field2
,b.field3
,b.field4
into new_table
from a
cross join b
Use Cross Join which will create a Cartesian product of two tables
SELECT a.*,b.*
FROM table1 a
CROSS JOIN table2 b
or Use Cross Apply
SELECT a.*,b.*
FROM table1 a
CROSS Apply table2 b