Select complete matching between two table - sql

I have two tables
t1
col1 col2
A 1
A 2
B 1
C 2
t2
col1
1
2
I want to retrieve the records in tab1 which match all the records in tab2,
For the given scenario, I want to output A only because it has both 1 and 2 in col2 where as B and C has only a 1 or a 2 (not both).

You can write the query this way:
select t1.col1
from t1 join
t2
on t1.col2 = t2.col1
group by t1.col1
having count(distinct t1.col2) = (select count(distinct t2.col1) from t2);
This counts the number of matching values in the first table and compares it to the total number of values in the table.

Related

SQL join two tables that have the same columns, with an overlapping `id` column, but merge based on if table1.col1 >= table2.col1

I want to join two tables that have the same columns, with an overlapping id column, but merge based on if table1.col1 >= table2.col1. This is in SQL.
If table1.col1>=table2.col1, use the columns from table1.
If table1.col1< table2.col1, then use columns from table2.
If the id does not exist in table1 but exists in table2, use the columns from table2
If the id does not exist in table2 but exists in table1, use the columns from table1
For example:
Table1:
id
col1
col2
col3
A
3
5
4
B
1
2
3
C
8
9
7
Table2:
id
col1
col2
col3
A
2
5
6
B
5
7
8
D
2
3
4
I want the result to be:
id
col1
col2
col3
A
3
5
4
B
5
7
8
C
8
9
7
D
2
3
4
I have tried union, full outer join, and CASE statements, but am stuck
I think individual case expressions for each column might be best:
select id,
(case when t1.col1 < t2.col1 then t2.col1 else t1.col1 end) as col1,
(case when t1.col1 < t2.col1 then t2.col2 else t1.col2 end) as col2,
(case when t1.col1 < t2.col1 then t2.col3 else t1.col3 end) as col3
from t1 full join
t2
using (id);
If that is cumbersome, another approach uses not exists:
select t1.*
from t1
where not exists (select 1
from t2
where t2.id = t1.id and t2.col1 > t1.col1
)
union all
select t2.*
from t2
where not exists (select 1
from t1
where t2.id = t1.id and t1.col1 >= t2.col1
);
Another solution:
SELECT DISTINCT ON (id) *
FROM (
SELECT *
FROM table1
UNION ALL
SELECT *
FROM table2
) AS aux
ORDER BY id, col1 DESC;
I tried it in Postgresql.

SAP HANA | JOIN with LIKE OPERATOR

I have two tables -
TABLE T1 having below data -
COL1 COL2
1 A,B,C
TABLE T2 having below data -
COL3 COL4
A 10
B 20
C 30
D 40
The output I want is -
COL1 COL3 COL4
1 A 10
1 B 20
1 C 30
T1 and T2 have around one million rows.
I have already tried the following:
select T1.COL1,T2.COL3,T2.COL4
from T1
inner join T2 on T1.COL2 LIKE '%' || T2.COL3 || '%';
select T1.COL1,T2.COL3,T2.COL4
from T1
inner join T2 on instr(T1.COL2, T2.COL3 )>0;
select T1.COL1,T2.COL3,T2.COL4
from T1
inner join T2 on T1.COL2 LIKE_REGEXPR T2.COL3;
All the three statements work fine with TEST data. But when running on actual data results in memory allocation error.
Is there a way to do this in better way?

Select distinct values one column into multiple columns

I have the following data: column 1 with many category and column 2 with values for each category. I need to convert or pivot this information to show each value for category group across multiple columns.
col1 col2
----------------
1 a
2 b
2 c
2 d
3 e
3 f
4 g
4 h
And need this result:
col1 col2 col3 col4 col5 col6
-----------------------------------------------
1 a
2 b c d
3 e f
4 g h
There are no more than seven values per tb1 count(column 2) group(column 1). All values from tb1 column 2 are different and about + 50 records.
You want to pivot your table, but your table doesn't currently contain the field that you want to pivot on ("col1", "col2", "col3", etc...). You need a row number, partitioned by col1. The Jet database does not provide a ROW_NUMBER function, so you have to fake it by joining the table to itself:
select t1.col1, t1.col2, count(*) as row_num
from [Sheet1$] t1
inner join [Sheet1$] t2 on t2.col1 = t1.col1 and t2.col2 <= t1.col2
group by t1.col1, t1.col2
Now you can pivot on row_num:
transform Min(x.col2) select x.col1
from(
select t1.col1, t1.col2, count(*) as row_num
from [Sheet1$] t1
inner join [Sheet1$] t2 on t2.col1 = t1.col1 and t2.col2 <= t1.col2
group by t1.col1, t1.col2
) x
group by x.col1
pivot x.row_num

Return null for second duplicate row in JOIN in Access

I saw few posts for avoiding duplicates. But this is not about removing duplicates from the output. but keeping duplicates from one table but avoiding rows repeating from the joining table.
So I have 2 table Table1 and Table2 . Table1 has duplicate values for ID column. Table2 does not have duplicate values for ID column. I wish to join both tables. In the result I want all rows from Table1 including duplicates but i do not want Table2 to repeat rows for each duplicate row in Table1.
Table1
ID Column2
1 A
1 B
2 C
3 D
Table2
ID Column3
1 X
2 Y
My Query is
Select A.ID,A.Column2,B.ID,B.Column3 from Table1 A LEFT JOIN Table2 B on A.ID=B.ID
The result i get is
ID Column2 ID Column3
1 A 1 X
1 B 1 X
2 C 2 Y
3 D null null
The expected result is
ID Column2 ID Column3
1 A 1 X
1 B null null
2 C 2 Y
3 D null null
i could not see any possibilities in access database .
Please let me know if there is any possibility of getting this result
I can not check right now, but you will get the idea:
select t11.id, t11.col2, t2.id, t2.col3
from (table1 t11
left join (select id, min(col2) as col2 from table1 group by id) t12
on t11.id = t12.id and t11.col2 = t12.col2)
left join table2 t2 on t12.id = t2.id

Ordering in Access SQL

I don't know how to order the following data in Access SQL:
Col1 Col2
1 1
1 2
1 3
2 4
2 5
2 6
3 7
3 8
3 9
Where it grabs the lowest value in Col2 where Col1 = 1, and then the lowest value in Col2 where Col1 = 2 etc, leading to sorted data of:
Col1 Col2
1 1
2 4
3 7
1 2
2 5
3 8
1 3
2 6
3 9
Col1 can range from 1 to any number, and Col2 doesn't start from 1, or is consistently incremental (but still in the order of size).
The table also has an auto ID primary key if that helps.
---- Thanks to #shA.t this answer works perfectly. I added a simple table join which works as well:
SELECT t1.Col1, t1.Col2 FROM
(SELECT Table1.Col1, Table2.Col2 FROM Table2 INNER JOIN Table1 ON Table2.ID = Table1.ID) t1
INNER JOIN
(SELECT Table1.Col1, Table2.Col2 FROM Table2 INNER JOIN Table1 ON Table2.ID = Table1.ID) t2
ON t1.Col1 = t2.Col1 and t1.Col2 >= t2.Col2
Group by t1.Col1, t1.Col2
ORDER BY Count(t2.Col2), t1.Col1
I think you can use a query like this:
SQLFIDDLE DEMO
SELECT t1.Col1, t1.Col2
FROM t t1
JOIN t t2 ON t1.Col1 = t2.Col1 and t1.Col2 >= t2.Col2
Group by t1.Col1, t1.Col2
ORDER BY Count(t2.Col2), t1.Col1;