How to find out if a row of one table exists in the values of at least one row of another table? - sql

I have two SQL tables, example below:
Table 1 (column types varchar, integer, numeric)
A
B
C
D
A007
22
14.02
_Z 1
A008
36
15.06
_Z 1
Table 2 (column types varchar)
A
B
C
D
A009,A010,A011
33,35,36
16.06,17.06
_Z 1,_Z 2
A003,A007,A009
14,22,85
13.01,17.05,14.02
_Z 1
Is there a way to compare individual rows of the first table with the rows of the second table and find out which row of the first table does not occur in the values of any row of the second table?
As can be seen, the first row of table 1 occurs in the values of the second row of table 2.
However, the second row of table 1 does not occur in the values of the rows of table 2, therefore the desired output is row 2 of table 1.
Desired output table:
A
B
C
D
A008
36
15.06
_Z 1
What I have tried so far:
My solution was to create a table containing all possible combinations of column values for each row of the second table (with the same column data types as the columns of the first table) and then use SELECT * FROM TABLE1 EXCEPT SELECT * FROM TABLE2 to get the difference rows.
The solution worked (for relatively small tables) but I am currently in a situation where generating all combinations of column values for each row of the second table (which in my case has 500 rows) results in a table containing millions of rows, so I am looking for another solution, where I can use the original table with 500 rows.
Thank you in advance for any possible answer, preferably one that could also work in the IBM DB2 database.

We can use a LIKE trick here along with string concatenation:
SELECT t1.*
FROM Table1 t1
WHERE NOT EXISTS (
SELECT 1
FROM Table2 t2
WHERE ',' || t2.A || ',' LIKE '%,' || t1.A || ',%'
);
Note that it would be a preferable table design for Table2 to not store CSV values in this way. Instead, get every A value onto a separate row.

Related

How do I find unmatched records with a table that contains comma separated values

I am trying to check if the values from Table1 exist in Table2.
The thing is that the values are comma separated in Table1
Table 1
ID
TXT
1
129(a),P24
2
P112
3
P24,XX
4
135(a),135(b)
Table 2
ID
P24
P112
P129(a)
135(a)
135(b)
The following only works if the complete cell value exists in both tables:
SELECT Table1.ID, Table1.TXT
FROM Table1 LEFT JOIN Table2 ON Table1.[TXT] = Table2.[ID]
WHERE (((Table2.ID) Is Null));
MY QUESTION IS:
Is there a way to check each comma separated value and return those that do not exists in Table 2.
In above example the value XX should end up in the result.
Not sure why you store your data in that way (which is bad practice as sos mentioned above), but you need to mimic the temp table like in SQL server.
Select from table1 and create different txt rows per id.
Insert the results from section 1 into the table3.
Select from table3 and join it to table2.
Delete table 3.
Table3 the temp table
ID
TXT
1
129(a)
1
P24
2
P112
3
P24
3
XX
4
135(a)
4
135(b)
Here is some explanation MS Access database (2010) how to create temporary table/procedure/view from Query Designer

Get the "most" optimal row in a JOIN

Problem
I have a situation in which I have two tables in which I would like the entries from table 2 (lets call it table_2) to be matched up with the entries in table 1 (table_1) such that there are no duplicates rows of table_2 used in the match up.
Discussion
Specifically, in this case there are datetime stamps in each table (field is utcdatetime). For each row in table_1, I want to find the row in table_2 in which has the closed utcdatetime to the table 1 utcdatetime such that the table2.utcdatetime is older than the table_1 utcdatetime and within 30 minutes of the table 1 utcdatetime. Here is the catch, I do not want any repeats. If a row in table 2 gets gobbled up in a match on an earlier row in table 1, then I do not want it considered for a match later.
This has currently been implemented in a Python routine, but it is slow to iterate over all of the rows in table 1 as it is large. I thought I was there with a single SQL statement, but I found that my current SQL results in duplicate table 2 rows in the output data.
I would recommend using a nested select to get whatever results you're looking for.
For instance:
select *
from person p
where p.name_first = 'SCCJS'
and not exists (select 'x' from person p2 where p2.person_id != p.person_id
and p.name_first = 'SCCJS' and p.name_last = 'SC')

SQL/T-SQL Substring LEFT or Right doesn't appear to resolve

I have a two table where I have some values in a column UniqueKeys such as:
Table 1
2016_2016-2 S2_001840_30_01
2017_2017-2 D4_002213_3_01
The problem is that I am trying to match these with table 2 Unique values where the values are written in a different order such as :
Table 2:
001840_2016-2_S2_30_D_179_364128_400985
002213_2017-2_D4_3_E_752_376901_422828
Table 1 is from a different source system and table 2 is from different one. What I am trying to achieve is create a new table TABLE 3 where when the unique values match between table 1 and table 2 then insert the data from certain columns of table 1 and 2 into table 3 or else ignore the rest.
The way the Unique values should be is the following:
Year and Period: 2016-2
Cycle : S2
Unit: 001840
Group: 30
Giving the end result in Table 3 as:
001840_2016-2_S2_30
002213_2017-2_D4_3
You need to split both input values by "_" and then recombine the parts in the way they lead to the same format. Then you can join the tables.
Use two functions, the first one for values from type table 1, the second for values from table 2.
Effekt:
SELECT ...
FROM table1
JOIN table2 ON splitfunction1(table1.Key1) = splitfunction2(table2.Key2);

Finding non-matching data in two tables as per columns and arranging column data as row by row

i have a requirement like below
i have two tables in same data base, both table have same structure and column count.but the columns not present in the same position.
ex:
table 1
id name age
1 dhileep 22
2 uday 33
table 2
id age name
1 20 udayga
2 22 uday
i have id column is same for all tables, if i change the table also i have id same, but may columns name and column count and data count will change.
my final output is:
column_name id table1 table 2
name 1 dhileep udayga
note: i gave above as example, the count of columns is more than 500 and data exist approximately 50000+
use Sql JOIN .to join the 2 tables
use the following answer .i think it is useful for u.
SELECT t1.id,t1.name,t2.name FROM table1 AS t1 JOIN table2 AS t2 ON t1.id = t2.id

Table whose columns are random samples from an original column

I have numerical data ​​related to clinical information from people with a particular disease recorded in an specific column ('Lab') from Table A.
I need to get a Table B with 30 rows and 50 columns.
The columns of Table B should be random samples from the values ​​contained in column 'Lab' (nearly 3300 registers).
I am able to get a table with one column using:
SELECT Lab FROM Table_A sample (1) WHERE Lab IS NOT NULL;
Is it possible make a query using the SELECT command that results in Table B with all its 50 columns without the need of getting its columns one by one?
You can use the RAND operator with ORDER BY, like this:
SELECT * FROM Table_A ORDER BY RAND( ) LIMIT 0 , 30