Why is CASE WHEN acting backwards in this query? - mysql

I'm trying to use CASE WHEN to group multiple possible values in a SQL view. It works, but backwards and I'd like to understand why.
SELECT DISTINCT
name,
CASE
WHEN name NOT LIKE '%Value1%' THEN 'Group1'
WHEN name NOT LIKE '%Value2%' THEN 'Group2'
WHEN name NOT LIKE '%Value3%' THEN 'Group3'
ELSE name
END AS 'filtered name'
FROM sometable;
This actually gives me the output that I need:
Anything that contains Value1 is put in Group1
Anything that contains Value2 is put in Group2
Anything that contains Value3 is put in Group3
Anything else keeps it's current name
Now, I expected the query for this result to be the one I currently have without any 'NOT' before the 'LIKE' operators, and I am quite confused that this works, I am trying to understand what's happening.

Related

How to check multiple columns for a value in google sheets?

I want to extract the first column value for the rows that any of the other values contain a specific string, I know the hard coded way would look something like this
=QUERY(A3:Q24; "select A where B contains 'No' or where C contains 'No' or where D contains 'No'";0)
However if i want to do this for a lot of columns this doesn't seem as a good way. Is there anything else i can use?
try:
=QUERY({A3:Q24\ FLATTEN(QUERY(TRANSPOSE(B3:Q24);;9^9))};
"select Col1
where Col18 contains 'No'"; 0)

SQL show results for A column first then show results for B column

I want SQL to show / order the results for the column name first then show results for the description column last.
Current SQL query:
SELECT * FROM products WHERE (name LIKE '%$search_query%' OR description LIKE '%$search_query%')
I tried adding order by name, description [ASC|DESC] on the end but that didn't work.
It's for optimizing the search results. If a certain word is found in description it should go last if a certain word is also found in the name column.
You can use a CASE statement in an ORDER BY to prioritize name. In the example below all results where name is matched will come first because the CASE statement will evaluate to 1 whereas all other results will evaluate to 2.
I'm not sure by your problem description what exactly you want the behavior to be, but you can certainly use this technique to create more refined cases to prioritize your results.
SELECT *
FROM products
WHERE (name LIKE '%$search_query%' OR description LIKE '%$search_query%')
ORDER BY CASE WHEN name LIKE '%$search_query%' THEN 1 ELSE 2 END
If you want the names first, the simplest order by is:
order by (name like '%$search_query%') desc
MySQL treats booleans as numbers in a numeric context, with "1" for true and "0" for false.
While this is undocumented, when results sets combined by a UNION ALL and not sorted afterwards, they stay in the order returned, as UNION ALL just adds new results to the bottom of the result set. This should work for you:
SELECT * FROM products
WHERE name LIKE '%$search_query%'
UNION ALL
SELECT * FROM products
WHERE (description LIKE '%$search_query%' AND name NOT LIKE '%$search_query%')

Two different queries on stimulsoft if variables are filled or not

I'm using the Stimulsoft Design to make reports and I'm using two variables to filter.
So I want to make the datasource to build one SQL statement if filter one is filled and the other is empty and another SQL if filter two is filled and the other is empty, and maybe an else statement...
So it would be something like this:
If filter one is filled and filter two is empty, then make SELECT 1, which is:
SELECT * FROM tableExample WHERE column1 LIKE '%{filter1}%'
If filter two is filled and filter one is empty, then make SELECT 2, which is:
SELECT * FROM tableExample WHERE id = '%{filter2}%'
and ID is a primary key.
Well, can I do that?
Maybe I could check it only using SQL, but I can't figure out how to do this, could someone help me please?
Thank you!
If you are looking for a single query which contains your logic you could try the following:
SELECT *
FROM tableExample
WHERE
(COALESCE(filter2, '') = '' AND COALESCE(filter1, '') <> '' AND
filter1 LIKE '%{filter1}%') OR
(COALESCE(filter1, '') = '' AND COALESCE(filter2, '') <> '' AND
id = '%{filter2}%')
Note, the somewhat ugly COALESCE calls are there because I don't know what you mean by empty. Does this mean NULL, empty string, or both?

Separate One Column Into Two Columns

I'm trying to separate one column that contains a blood type into two columns.
For example, if my value is ABNG in the existing blood type column, I want to separate this into two new columns and insert the values: AB and Neg.
Not sure if a case statement will handle this, but I tried different variations and could not find a solution.
Here is what the existing data looks like, there are 9 blood types (including the Unknown which is fine) with some bad data mixed in (' ', 0, and B).
Without having the best grasp on how to handle this I am thinking a view could be created that would split the information into new columns.
And this is what I'm hoping the end result to be:
Is this possible?
You can use CASE statement for checking conditions and wildcard characters for string filtration for the source column.
SELECT PBTYPE [Existing Blood Type],
CASE
WHEN PBTYPE LIKE 'O%' THEN 'O'
WHEN LEFT(PBTYPE,1)='A' AND SUBSTRING(PBTYPE,2,1)<>'B' THEN 'A'
WHEN LEFT(PBTYPE,2)='AB' AND SUBSTRING(PBTYPE,2,1)='B' THEN 'AB'
WHEN PBTYPE LIKE 'B%' THEN 'B'
WHEN PBTYPE = 'UNK' THEN 'UNK'
END [Blood Type],
CASE
WHEN PBTYPE LIKE '%POS' OR PBTYPE LIKE '%PS' THEN 'POS'
WHEN PBTYPE LIKE '%NEG' OR PBTYPE LIKE '%NG' THEN 'NEG'
WHEN PBTYPE LIKE '%B' THEN 'B'
WHEN PBTYPE LIKE '%UNK' THEN 'UNK'
END [Rho]
FROM YOURTABLE
WHERE PBTYPE <> '' AND PBTYPE <> '0'
Click here to view result
Given that there's a limited number of blood types (and hoping that your data is formatted well), you could do something like:
SELECT Type = CASE WHEN col LIKE 'AB%' THEN 'AB' /*gets AB*/
ELSE LEFT(col, 1) /*gets o,a,or b*/ END,
Valence = RIGHT(col, 3) /*gets pos or neg*/

MySQL How to Select only columns where value contains string

I did some searching and from one question already posted on stackexchange, the answer was that it was not possible, but I figured to ask. I did not know if it was possible to form a SELECT query to dynamically select which columns will be displayed in a mysql SELECT statement result. Example:
Say I have column names Person, ID, Phone Number, Alt Number for this table:
John | 79 | 800-499-0000 | 800-499-5555
I would like to form a SELECT statement so that it will only pull down columns where string '800-499' is somewhere in the field. Thus the result from MySQL ideally would be:
800-499-0000 | 800-499-5555
The only problem is that I do not think dynamically selecting columns is possible.
Any help or confirmation is appreciated.
You could try something like:
select * from
(select concat(case when col1 like '%800-499-0000%' then concat('col1: ',col1,';') end,
case when col2 like '%800-499-0000%' then concat('col2: ',col1,';') end,
...
case when coln like '%800-499-0000%' then concat('coln: ',coln,';') end)
as search_results
from my_table) sq
where search_results is not null