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?
Related
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.
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
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;
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.
I have two tables.
Table1
col1 col2 col3
100 A 1000
100 A 1000
100 A 1002
100 B 1003
100 C 1004
Table2
col1 col2 colC
100 A 1X
100 A 2X
100 A 3X
100 B 4X
100 C 5X
In the above table colC value is unique.
i want my ouptput to be like this, where colC values to be unique here also.
col1 col2 col3 colC
100 A 1000 1X
100 A 1000 2X
100 A 1002 3X
100 B 1003 4X
100 C 1004 5X
I have to use col1 and col2 as the key for the join.
Is that possible to do that. i got duplicates comming in for the first two records, when i tried with inner and left outer joins. TIA
Something like this?
select
a.col1,
a.col2,
a.col3,
b.colC
from (
select
row_number() over (partition by col1, col2 order by 1) r,
col1,
col2
from
table1
) a, (
select
row_number() over (partition by col1, col2 order by 1) r,
col1,
col2
from
table2
) b
where a.r = b.r and
a.col1 = b.col1 and
a.col2 = b.col2;
SELECT t1.col1, t1.col2, t1.col3, t2.colC FROM Table1 t1
JOIN Table2 t2
ON t1.col1 = t2.col1
AND t1.col2 = t2.col2
Is this not right?
EDIT: You say you get duplicates, but this will happen as the 2 columns you specify as keys, are not actually keys. There are other rows with the same values. So if the combination is meant to be unique there is either a fault with the data or your requirements need to be looked into again.