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

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)

Related

Take values from a column if another contain something

I'm trying to achieve a hard try in google sheet.
Let's start from what I right now, the A structure in the image.
What I would like to achieve using functions like =QUERY, is the B or C (whatever is fine for me) structure.
Can you help me with the syntax?
I appreciate it so much and thank you very much
Luco
I tried a couple of functions, but can't get to the point using QUERY function, maybe I'm using bad syntax.
I don't know if there's an easy way to do it with just one query or filter. What I did is to create the first column with this approach: I joined all the hours column with ", " as separator. Then splitted it again by "," as a result we have all the values listed, then set them as unique:
=unique(arrayformula(trim(transpose(split((join(", ",filter(B:B,B:B <> ""))),",",true,true)))))
Then in the next column a query with byrow to be repeated in each row which finds if column B contains that time:
=byrow(D2:D,lambda(each,join(", ",if(each="","",transpose(query(A:B,"Select A where B contains '"&each&"'"))))))
https://docs.google.com/spreadsheets/d/1cnbfkeuS1LXeD7T6gFVCpXaK2gGtJLvr0-2B3Mq09vY/edit?usp=sharing
all in one B variant:
=INDEX(LAMBDA(x, TRIM(SPLIT(FLATTEN(QUERY(QUERY(SPLIT(FLATTEN(
TRIM(SPLIT(OFFSET(x,,1), ",")&"×")&"​"&x&"×"), "​"),
"select max(Col2) where Col1 <> '×' and Col2 is not null
group by Col2 pivot Col1"),,9^9)), "×")))
(A2:INDEX(A:A, MAX(ROW(A:A)*(A:A<>"")))))
all in one C variant:
=INDEX(LAMBDA(x, REGEXREPLACE(TRIM(SPLIT(FLATTEN(QUERY(QUERY(SPLIT(FLATTEN(
TRIM(SPLIT(OFFSET(x,,1), ",")&"×")&"​"&x&","), "​"),
"select max(Col2) where Col1 <> '×' and Col2 is not null
group by Col2 pivot Col1"),,9^9)), "×")), ",$", ))
(A2:INDEX(A:A, MAX(ROW(A:A)*(A:A<>"")))))

How to get the values for which the format and suffix are known but the exact values are not known and there can be multiple values from the database?

I have a use case as below:
I have thousands of records in the database and let's say I am having one column named myValue.
Now the myValue's actual value can be an alphanumeric string where the first two characters are alphabets, the next 6 characters are numbers and the last character is a fixed alphabet let say 'x', which may be or may not be present in the value. (For Example 'AB123456','AB123456x')
So I know the format of the value for myValue field but not know all the actual values as there are lots of records.
Now I want to retrieve all such values for which the value without last character x (For Example, 'AB123456') and the same value with last character x (For Example, 'AB123456x') exists.
So is there any way I can retrieve such data?
I am right now doing trial and error on existing data but have not found success and there are thousands of such rows, so any help on this would be appreciated a lot.
You can do so like this:
SELECT myvalue
FROM t
WHERE myvalue LIKE '________'
AND EXISTS (
SELECT 1
FROM t AS x
WHERE x.myvalue = CONCAT(t.myvalue, 'x')
)
A (most likely) faster alternate is:
SELECT TRIM(TRAILING 'x' FROM myvalue) AS myvalue2
FROM t
GROUP BY myvalue2
HAVING COUNT(DISTINCT myvalue) > 1

Why is CASE WHEN acting backwards in this query?

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.

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?

MySQL Select when specific digit is x

I have a column that contains values like 5A898, 89KAS, 89ASD.
I'm trying to write a query that will only return rows where the third digit of the column is 'A'. For instance '89ASD' would be returned but '89KAS' would not. I'm not sure what the right way to go about this is. Regular expressions?
So...
SELECT column
FROM table
WHERE column = ?
WHERE column LIKE '__A%'
^^-- 2 underscores
should do the trick. two "whatever" characters, followed by an A, then followed by anything in any amount.
Maybe you can use MySQL's SUBSTRING
SELECT column
FROM table
WHERE SUBSTRING(column,3,1) = 'A'
where right(left(col,3),1) = 'A'
That can help... MarcB's answer is cleaner
You can do this with a regex, but I think it might be easier just coping with a string operation here:
SELECT column
FROM table
WHERE SUBSTRING(column,3,1) = 'A';