I need to run a query that will give me the list of all entries in one column that is NOT LIKE any of the entries in another column, i.e.:
SELECT DISTINCT columnA
FROM tableA
WHERE columnA NOT LIKE (SELECT columnB FROM tableA)
Obviously, the above query doesn't work, I'm providing it only in the hopes that it will clarify what I'm trying to achieve. So, as an example, say that my columns contain the following:
COLUMNA:
ABCD
ABCE
BCDE
BCDF
BCDEF
GHIJ
GHIK
COLUMNB:
ABC
DEF
HIJ
My desired results would be:
BCDE
BCDF
GHIK
There are a total of 396 values in column in the table, so just entering the values manually is not feasible. In addition, as noted in the example, the values in columnB would always be substrings of the values in columnA, so I also need to have my query do the comparison with that in mind.
Thanks in advance for any help anyone can offer, and also apologies if this question has already been answered elsewhere - I did a search but wasn't able to find anything that I could interpret as addressing this specific requirement.
ADDING NEW INFO **
So, as noted, I made a HUGE mistake in that the two columns are in different tables. That said, though, it was easy enough to modify califax's suggestion below as follows:
SELECT DISTINCT COLUMNA
FROM TABLE1 T1
LEFT JOIN TABLE2 T2 ON
T1.COLUMNA LIKE '%' + T2.COLUMNB + '%'
AND T2.COLUMNB IS NULL
However, it's still returning the full list of entries from COLUMNA. I've confirmed that there are entries in COLUMNB that are substrings of the entries in COLUMNA - any ideas why this isn't filtering?
Thanks.
SELECT DISTINCT columnA
FROM tableA as O
WHERE not exists ( select 42 from TableA where O.ColumnA like ColumnB )
Perform a self join, and look for the ones that don't match:
SELECT DISTINCT a1.ColumnA
FROM TableA a1
LEFT JOIN TableA a2
ON a1.ColumnA LIKE '%' + a2.ColumnB + '%'
AND a2.ColumnB IS NULL
(I added a leading wildcard, since you clarified the desired matches in your question.)
UPDATE
If there are two distinct tables, b.ColumnB shows you the ones that don't match:
SELECT DISTINCT a.ColumnA
FROM TableA a
LEFT JOIN TableB b
ON a.ColumnA LIKE '%' + b.ColumnB + '%'
AND b.ColumnB IS NULL
I would try something like :
select distinct columnA from tableA where columnA not like '%' + columnB + '%'
or following criticalfix's remark (as I'm not sure what you wish exactly)
SELECT DISTINCT columnA FROM tableA tbA
WHERE not exists ( select 1 from TableA where tbA.ColumnA like '%' + ColumnB '%' )
Related
I need help with a probably simple SQL problem. quite new with SQL so not sure how to solve this.
My problem is that i'm trying to select a columnA where the data in the column contains keywords from another columnB in tableB. an example of how the data is as below.
tableA, columnA : {'This apple is red', 'This ball is round', 'This chair is metal'}
tableB, columnB : {'red', 'round'}
Now im thinking something like this query
SELECT columnA FROM tableA
WHERE columnA LIKE '%' + (SELECT columnB FROM tableB) + '%'
So obviously it won't work because the subquery returns more than 1 value. I'm trying to see some other functions to use like CURSOR but I can't figure it out. Help is very appreciated, thank you.
Use EXISTS instead:
SELECT columnA FROM tableA A
WHERE EXISTS (SELECT 1 FROM tableB B where A.columnA LIKE '%' + B.columnB + '%')
You can try this:
select * from tableA A
JOIN tableB B on A.columnA like '%' + B.columnB +'%'
I have two tables in database.
Both tables have a business name column but not always going to be the same.
For example tbl 1 has a business name of 'Aone Dental Practices Limited TA Jaws Dental' and Tbl 2 has a business name of 'Jaws Dental'. I want to be able to join these together as Jaws Dental is visible in both.
I can't seem to get the Like clause working for this.
tried
Tbl1_BusinesName Like '%' + Tbl2_BusinesName + '%'
This query should work :
SELECT *
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.BusinesName LIKE '%'+TS.BusinesName+'%'
Using EXISTS you can get the expected result:
SELECT *
FROM dbo.TableName1 AS Tbl1
WHERE EXISTS (SELECT 1
FROM dbo.TableName2 AS Tbl2
WHERE Tbl1.BusinesName LIKE '%' + Tbl2.BusinesName + '%');
I know this question has been asked, but I have a slightly different flavour of it. I have a use case where the only thing I have control over is the WHERE clause of the query, and I have 2 tables.
Using simple example:
Table1 contains 1 column named "FULLNAME" with hundreds of values
Table2 contains 1 column named "PATTERN" with some matching text
so, What I need to do is select all values from Table 1 which match the values in table 2.
Here's a simple example:
Table1 (FULLNAME)
ANTARCTICA
ANGOLA
AUSTRALIA
AFRICA
INDIA
INDONESIA
Table2 (PATTERN)
AN
IN
Effectively what I need is the entries in Table1 which contain the values from Table2 (result would be ANTARCTICA, ANGOLA, INDIA, INDONESIA)
In other words, what I need is something like:
Select * from Table1 where FULLNAME IN LIKE (Select '%' || Pattern || '%' from
Table2)
The tricky thing here is I only have control over the where clause, I can't control the Select clause at all or add joins since I'm using a product which only allows control over the where clause. I can't use stored procedures either.
Is this possible?
I'm using Oracle as the backend DB
Thanks
One possible approach is to use EXISTS in combination with LIKE in the subquery:
select * from table1 t1
where exists (select null
from table2 t2
where t1.fullname like '%' || t2.pattern || '%');
I believe that you can do this with a simple JOIN:
SELECT DISTINCT
fullname
FROM
Table1 T1
INNER JOIN Table2 T2 ON T1.fullname LIKE '%' || T2.pattern || '%'
The DISTINCT is there for those cases where you might have a match to multiple rows in Table2.
If the patterns are always two characters and only have to match the start of the full name, like the examples you showed, you could do:
Select * from Table1 where substr(FULLNAME, 1, 2) IN (Select Pattern from Table2)
Which prevents any index on Table1 being used, and your real case may need to be more flexible...
Or probably even less efficiently, similar to TomH's approach, but with the join inside a subquery:
Select * from Table1 where FULLNAME IN (
Select t1.FULLNAME from Table1 t1
Join Table2 t2 on t1.FULLNAME like '%'||t2.Pattern||'%')
Right, this involved a bit of trickery. Conceptually what I've done is turned the column from the PATTERN into a single cell, and use that with REGEX_LIKE
So the values "AN and IN" becomes one single value '(AN|IN)' - I just feed this to the regexp_like
SELECT FULLNAME from table1 where
regexp_like(FULLNAME,(SELECT '(' || SUBSTR (SYS_CONNECT_BY_PATH (FULLNAME , '|'), 2) || ')' Table2
FROM (SELECT FULLNAME , ROW_NUMBER () OVER (ORDER BY FULLNAME) rn,
COUNT (*) OVER () cnt
FROM Table2)
WHERE rn = cnt START WITH rn = 1 CONNECT BY rn = PRIOR rn + 1))
The subquery in the regexp_like turns the column into a single cell containing the regular expression string.
I do realise this is probably a performance killer though, but thankfully I'm not that fussed about performance at this point
Can someone tell me what is the sql expression for below? Result returns true because CD in table A exists partially in value of table B.
Table A:
CD
Table B:
ABCD,CD,ABEF
("ABCD,CD,ABEF" is 1 value in the field of table B)
where
TableB.YourField LIKE '%CD%'
OR
where
TableB.YourField LIKE '%' + TableA.TheCDField + '%'
However, VERY poor design of table content when multiple values concatenated in a single field. Should be normalized, but not enough information provided
Assume TableA has Column1 which contains 'CD' and TableB has Column1 which contains 'ABCD,CD,ABEF'. Your text match is in the middle, but can happen at beginning of string, mid string or at the end of the string. So:
select *
from TableA cross join TableB
where
TableB.Column1 like TableA.Column1 +',%' or
TableB.Column1 like '%,'+ TableA.Column1 +',%' or
TableB.Column1 like '%,'+ TableA.Column1
I am using SQL Server 2008 and I have two tables that I want to join. I have provided something below that shows how my data looks. I want to join the two tables on the given columns, but how can I do this with the "ID" in front of the number in table B? I was thinking of a trim on the join, but I don't know how to do that.
Something like...
Select *
From TableA AS A
Left Join TableB AS B
On A.ColumnA = B.ColumnB
But this won't work because the numbers don't completely match up.
TableA ColumnA
123
456
789
TableB ColumnB
ID123
ID456
ID789
I hope I made this clear enough. Any suggestions?
SQL Fiddle Demo
select *
from tableA a
join tableB b
on 'ID' + cast(columnA as varchar(5)) = b.columnB