select count(distinct) where col not like ('%d1,%d2,...') - sql

select
count(distinct [PROV_CT])
from
[HRecent]
where
[PROV_CT] not like ('%P125, %P961, %P160, %P960, %P220, %P004')
Can I write a query like this? Actually it is showing outputs which is different from the query output.
select
count(distinct [PROV_CT])
from
[HRecent]
where
[PROV_CT] not like '%P125' and
[PROV_CT] not like '%P220' and
[PROV_CT] not like '%P960' and
[PROV_CT] not like '%P004' and
[PROV_CT] not like '%P961' and
[PROV_CT] not like '%P160'
Can anyone help me out please? I want to write an optimised query.

You cannot write the query using a single string literal like in:
[PROV_CT] not like ('%P125, %P961, %P160, %P960, %P220, %P004')
This predicate doesn't look for separate values like '%P125', '%P961' etc.
If you have a very big list of values against which NOT LIKE operation is to be performed, then it might be simpler to do it like this:
select
count(distinct [PROV_CT])
from
[HRecent]
cross apply (
select count(*)
from (values ('%P125'), ('%P961'),
('%P160'), ('%P960'),
('%P220'), ('%P004') ) AS t(v)
where [PROV_CT] LIKE t.v) AS x(cnt)
where x.cnt = 0
Using VALUES Table Value Constructor you create an in-line table containing all the values against which [PROV_CTRCT] column is to be compared. Then query this table using a single LIKE operation to find if there is a match or not.
Demo here

Related

using like operator in spark sql databricks

I'm using spark sql, and I created some Vues to join some data. but I have to join these Vues based on a string column. thats whu I had to use the like operator.
select table.perfume,table2.perfume
from global_temp.gv_table1 table1
join global_temp.gv_table2 table2
on(lower(table1.perfume) like CONCAT('%', lower(table2.perfume), '%') )
but the problem with this query it does not not give all the result, example.
there'es a perfume on the table1 called "FlowerBomb" and a perfume on the table2 called "Flowerbomb Eau du parfum", after the join this perfume was not displayed.
is there a problem with the like operator ?
You've got the order of the columns wrong in your like expression.
Since table2.perfume contains table1.perfume, the expression should be like this:
on(lower(table2.perfume) like CONCAT('%', lower(table1.perfume), '%') )
you may want to convert this to Spark APIs. Its fairly simple -
result = table1.alias('1').join(
table2.alias('2'),
F.expr("2.perfume like 1.perfume")
)

SQL Query with Regular Expression in LIKE function

I have a requirement where in I want to filter all rows in a table, which have a keyword 'Risk' in the description but not 'F Risk'. For example, in the following test data, the query should select rows 1,5,6 and 7.
However, when I am using the below query, it is eliminating id 1 (as it meets the second half of the condition) and just returning 5,6 and 7.
SELECT *
FROM TestDesc
WHERE ([desc] LIKE '%Risk%' AND [desc] NOT LIKE '%F Risk%')
I believe I would have to use regular expressions to achieve the desired result, but I am not very familiar with regular expression.
Any help/suggestion would be appreciated. Thanks!
I think you need to tweak the search for "F Risk":
SELECT *
FROM TestDesc
WHERE [desc] LIKE '%Risk%' AND [desc] NOT LIKE '%[ (]F Risk%'

How can query be optimized?

I have a simple select query on a table, but with different values in LIKE operator. Query is as follows:
SELECT *
FROM BatchServices.dbo.TaskLog
WHERE EntryTime BETWEEN '20190407' AND '20190408' AND
TaskGroup LIKE '%CSR%' AND
(LogText LIKE '%error%' OR LogText LIKE '%fail%')
This above query is fine and returning me the expected results but I don't want to have multiple LIKE in a query, so I have already tried something like
SELECT *
from BatchServices.dbo.TaskLog
WHERE taskgroup = 'csr' AND
LogText IN ( '%error%','%fail%') AND
EntryTime>'2019-04-07'
ORDER BY EntryTime ASC
This query is not giving me any results.
I am expecting a query which looks smarter than the one I have which returns result. Any help?
use like operator with OR condition
SELECT * from BatchServices.dbo.TaskLog WHERE taskgroup ='csr' AND
(LogText like '%error%' or LogText like '%fail%')
AND EntryTime>'2019-04-07'
ORDER BY EntryTime ASC
The LIKE operators are not the problem. It's the leading wild cards. Unless you cant get rid of those, your optimization options are going to be limited to making sure you have a covering index on EntryTime... That and replacing the "*" with the specific columns you need.

SQL query with CONCAT LIKE AND

I have an query like this:
SELECT * FROM `tbl_shop`
WHERE
(LOWER (CONCAT(address, name)) LIKE (LOWER ('%boston%')) AND
LOWER (CONCAT(address, name)) LIKE (LOWER('%smoke%')));
My question is simple - is there any way how to write this code without need to repeat CONCAT(address, name) part?
I tried
SELECT * FROM `tbl_shop`
WHERE
(LOWER (CONCAT(address, name)) LIKE (LOWER ('%boston%')) AND (LOWER('%smoke%')));
But this was not giving any results. I simply need all results, which contain both words. I can not use full text, because I am using inno db and want to keep it.
Thanks in advance.
You can do
SELECT b.* FROM
(
SELECT a.*, LOWER(CONCAT(a.address, a.name)) AS field_to_check
)b
WHERE b.field_to_check LIKE (LOWER ('%boston%'))
AND b.field_to_check LIKE (LOWER('%smoke%'));
However, it's just syntax sugar, and it shouldn't be a difference in performance.

SQL Query for finding a column name where matching text is in column

This is my first stakoverflow question, although I've lurked for quite a while. I'm writing a webapp in PHP/SQLite, and I'm trying to find a column name along with the following SQL query:
SELECT lexemeid FROM lexeme, sense WHERE lexeme.lexemeid =
sense.senselexemeid AND (lexeme.lexeme LIKE '%apani%' OR lexeme.variant
LIKE '%apani%' OR lexeme.affixedform LIKE '%apani%' OR sense.example
LIKE '%apani%');
Basically, I'm offering a full text lookup for a few different fields. The query above works, but I'd like to get the name of the column where my wildcard matches for each result as well. Basically I want something like the above query with the SELECT looking more like:
SELECT lexemeid, COLUMN NAME FROM...
I'd also welcome any ideas for making my SQL Query look/perform better (maybe using LIKE and IN??). I'm basically trying to join lexeme.lexemeid and sense.senselexemeid and do a wildcard lookup on a text string (in this case, "apani").
Thanks!
Assuming you only have a match in one of the columns, you could use a CASE statement.
SELECT lexemeid,
CASE WHEN lexeme.lexeme LIKE '%apani%' THEN 'lexeme'
WHEN lexeme.variant LIKE '%apani%' THEN 'variant'
...
WHEN sense.example LIKE '%apani%' THEN 'example'
END AS ColumnName
FROM ...