Insert Row in between query result or using google app script - google-apps-script

So I have a set of data here, example below
and I am trying to get a result similar to this
I am thinking of using query, and I found this query formula:
=query({Sheet1!$A$1:$A;Sheet1!$B$1:$B;Sheet1!$C$1:$C},"select * where Col1 <>'' ",0)
but this only applies in transforming wide to long dataset
Is there a way to do it using query? or even an app script?

What you can do is to add a set of values with only the first two words. For that you can use:
=INDEX(SPLIT(UNIQUE(IFNA(REGEXEXTRACT(A:A,"(.+ .+) "))&"¿¿"),"¿",1,0))
PS: the two ¿¿ are for adding the extra columns. If you actually have more columns, add more ¿ signs -- (you don't need to do this step, I'm just showing you the process)
Then, with the query:
=QUERY({A:C;INDEX(SPLIT(UNIQUE(IFNA(REGEXEXTRACT(A:A,"(.+ .+) "))&"¿¿"),"¿",1,0))},"Select * where Col1 is not null order by Col1")
You'll get something like this in the right:
Then, with conditional formatting you can get something like this:
=($I1<>"")*($J1="")*($K1="")
PS: adapt the actual columns

Related

Select rows in SQL partially matching an input input

I would like to select rows in my table (I'm using Google Sheet for that purpose) which content is included in the string.
For example, rows included in table called Jobportal, column Test:
How to find work
Work permit
Jobs
Temporary jobs
I want to select all the rows that contain any word of my input, so if I write "i'm looking for a job", I need to select rows Jobs and Temporary jobs. If I write "where is my work?", I need to select How to find work and Work permit.
I've tried this query, but it's returning wrong/unexpected results.
select * from Jobportal where 'im looking for a job' LIKE CONCAT('%',Test,'%');
You can use regular expressions. Assuming that what the user types does not have special characters:
where test regexp replace('im looking for a job', ' ', '|')
That said, for performance you might want to consider using full text search capabilities.

Using IN and LIKE in a search and replace query

I have some fields in a MYSQL database with the following content:
eq":"fieldname1+fieldname2+fieldname3+fieldname4/4
The numbers are always different, so it could also be something like:
eq":"fieldname11+fieldname22+fieldname8/10
I would like to run a query to achieve the following:
eq":"((fieldname1+fieldname2+fieldname3+fieldname4)/4, 2)
I currently have the following query
UPDATE wp_wrdydtdbww_cp_calculated_fields_form_settings
SET form_structure = REPLACE(form_structure, '???', '???')
WHERE id IN (1,2,3);
The problem is, that there are a lot of additional strings, containing 'fieldname' or '/' as well, so I need to replace the exact structure.
Can somebody help me to modify it?
I tried something with the LIKE pattern (%), but can't get it to work as I always replace other parts of strings as well.
Thanks!

Display output of MySQL query in one line/ horizontally

I am validating the results of a MySQL database that I created and, for that, I need some screenshots.
For example, the following query:
select distinct run_ID
from ngsRunStats_FK.failedRuns
where reason_fail regexp 'cannot populate readsInfo'
will return (output from the terminal)
But as we can see, the screenshot is quite too long.
Is there a way to, instead of display the output as a (vertical) column, to display only its values horizontally (e.g. like in a python list)?
Try using GROUP_CONCAT:
SELECT GROUP_CONCAT(run_ID ORDER BY run_ID) AS run_ID_values
FROM ngsRunStats_FK.failedRuns
WHERE reason_fail REGEXP 'cannot populate readsInfo';
Side note:
If you really want to match the three keywords cannot populate readsInfo anywhere inside a larger text within the reason_fail column, then consider using word boundaries with REGEXP:
WHERE reason_fail REGEXP '[[:<:]]cannot populate readsInfo[[:>:]]';

MySql extract data from a database based on two columns

I Am trying to create/run an my sql query in such a way that the sql selects data based a some conditions from Column 1 (USER) but at the same time Excludes some data, based on some conditions from column 2 (ADDRESS)
E.g.:
SELECT ADDRESS,USER
FROM Data1.Table1
WHERE FIELD(USER,'%AMIT%','%JOHN%','%SANDEEP%','%WARNE%')
AND ORIGINATING_ADDRESS NOT LIKE 'MUMBAI','CHINA','PAKISTAN'
This is giving error.Can some one please help ?
Use NOT IN to discard list of values from select. Considering that you want to discard when there is exact match
ORIGINATING_ADDRESS NOT IN ('MUMBAI','CHINA','PAKISTAN')
When you want to use pattern search and discard the use this
ORIGINATING_ADDRESS NOT LIKE '%MUMBAI%' OR
ORIGINATING_ADDRESS NOT LIKE '%CHINA%' OR
ORIGINATING_ADDRESS NOT LIKE '%PAKISTAN%'
For a set of values, use NOT IN, instead of NOT LIKE.
You might find regular expressions simpler for this purpose:
SELECT ADDRESS,USER
FROM Data1.Table1
WHERE USER REGEXP 'AMIT|JOHN|SANDEEP|WARNE' AND
ORIGINATING_ADDRESS NOT REGEXP 'MUMBAI|CHINA|PAKISTAN';

return distinct text which is longtext format (but not returning the repeated text)

I have a simple scenario as follow:
I have 2 column : 1) id and 2) text(which is long text format)
I use the simple query to extract all info from mysql as follow:
select id,text from dbtest
but the problem is for different id I might have the same text but in retrieval time I do not want to return rows with the same text again and again so I do not want to return the repeated texts,I tried to use distinct but it was not working,
How can I do that , any idea?
One option is to use user-defined variables:
select id,
#text:=if(#text=text, '', text) text
from dbtest, (select #text:='') t
order by text
SQL fiddle demo
I would generally recommend doing this on the application side rather than the database though.
Distinct works on all selected columns. You must use a GROUP BY:
SELECT id,text FROM dbtest GROUP BY text
So I have two questions, can you post the code you are using for us? And are some of the fields for the text empty?
The query might be detecting a "Null" kind of event in which if the field is empty, it repeats the last one because the variable might not be getting unset. In which case you might just want to unset your variables at the end of your (I'm assuming) while loop.