I've been looking for a solution to my problem, I'm close but not there yet.
I'm sure the key is a clever use of TRANSPOSE, QUERY, JOIN and/or SPLIT ( I think ... ) , but I'm no Google Sheet expert so I figured I asked you for help :)
Bellow are the images to better match my scenario (string with spaces).
Here the result I'd like:
Is there a magic formula for this?
kaam,
Here is one way...
=ArrayFormula(SPLIT(TRANSPOSE(SPLIT(JOIN(" ", QUERY(IF(A2:B<>"",A1:B1&"_"&A2:B,),,ROWS(A2:A))), " ")), "_"))
EDIT: If the values have spaces, try
=ArrayFormula(SUBSTITUTE(SPLIT(TRANSPOSE(SPLIT(JOIN(" ", QUERY(IF(A2:B<>"",A1:B1&"_"&SUBSTITUTE(A2:B, " ", "&"),),,ROWS(A2:A))), " ")), "_"), "&"," "))
Related
I have a query using the IN operator, in an array with 1000+ values. I've searched the error so far but couldn't find what I want : my SELECT is not optimized because I'm using the OR operator and it takes quite a lot of time.
I'm chopping my query into different arrays at the moment :
query = "SELECT\n" +
" filename,\n" +
" status\n" +
"FROM\n" +
" table1\n" +
"WHERE\n" +
" filename IN " + allFileNames[0];
for(int m=1; m<allFileNames.length; m++) {
query +=
" OR\n" +
" filename IN " + allFileNames[m];
}
What this does is essentially I have the allFileNames array at the moment, and each element of the array contains a string with 1000 file names. I'm using a OR operator on each element of the array.
How can I optimize all of this ? Is it possible without creating a temporary table ? Maybe using a substring, but I haven't quite found the solution yet.
Thanks in advance!
EDIT: I can't use temporary table since I don't have writing access to the database
Assuming single allFileNames entry is a single file name, then you can flatten everything under one IN statement.
query = "SELECT\n" +
" filename,\n" +
"FROM\n" +
" table1\n" +
"WHERE\n" +
" filename IN (" + allFileNames.stream().collect(Collectors.joining(","))
+")"
Because all of your conditions, joined by OR statements are using same property, you should merge them.
But if your allFileNames collections is too large, then you may consider executing your query in batches:
split your allFileNames collection to batches of 100-1000 then execute query for each batch
aggregate all results
I have .setFormula() and stuck on this. I think the problem is the comma (&","). I have researched several online forum before posting this question but no luck; perhaps someone here could help me out. I do know the rules about using the "" and '' that's why I have tried several formulas but still, I receive the error message:
"Missing ) after argument list".
Your time and help is appreciated!
Original formula:
=arrayformula(concatenate(filter('Sheet1'!E2:E,'Sheet1'!E2:E<>"")&", "))
code:
me.getRange('B8').setFormula("=arrayformula(concatenate(filter('Sheet1'!E2:E,'Sheet1'!E2:E<>"") & ", "))");
Formulas I have tried but failed:
"=arrayformula(concatenate(filter('Sheet1'!E2:E,'Sheet1'!E2:E<>'') & ', '))");
'=arrayformula(concatenate(filter('Sheet1'!E2:E,'Sheet1'!E2:E<>'') & ', '))');
"=arrayformula(concatenate(filter('Sheet1'!E2:E,'Sheet1'!E2:E<>'') & ", "))");
'=arrayformula(concatenate(filter("Sheet1"!E2:E,"Sheet1"!E2:E<>"") & ", "))');
You dont need single quotes ' around Sheet1.
Try
me.getRange('B8')
.setFormula('=arrayformula(concatenate(filter(Sheet1!E2:E,Sheet1!E2:E<>"") & ", "))"');
If you still need it, you need to escape it with backlashes like this \':
me.getRange('B8')
.setFormula('=arrayformula(concatenate(filter(\'Sheet1\'!E2:E,\'Sheet1\'!E2:E<>"") & ", "))"');
Reference:
String ยง Escape notation
Is it possible to form a query where table1.first_name + " " + table1.last_name matches table2.customer_name?
IE:
customers.first_name = "John"
customers.last_name = "Doe"
orders.customer_name = "John Doe"
It seems to me this would be a common query, but just can't come up with the syntax intuitively.
Also, I am led to believe that this would not be a 'best practice' solution (using id fields would be better), but if I can't control the schema, I just want to know if something like my approach is even possible.
You are looking for concat
where concat(customers.first_name,' ',customers.last_name) = orders.customer_name
You can concatenate the values and compare the result as so:
(customers.first_name || ' ' || customers.last_name) = orders.customer_name
Brackets only for readability
At a high-level this sounds trivial, but it turns out I've been scratching my head for a a couple of hours.
Situation:
I have table T, with columns a,b,c,d,e. Column a holds a string, while b,c,d,e each hold a boolean value.
I am allowing a user to perform a kind of search, where I prompt the user to enter values for a,b,c,d,e, and return all the rows where those values all match.
In a perfect world, the user enters all values (lets say a="JavaScript" , b="true", c="false", d="false", e="true") and a resulting query (In Scala, referencing a remote DB running MySQL) might look something like this:
connection.createStatement().executeQuery("SELECT * FROM T
WHERE a = '" + a_input + "'
and b = " + b_input + "
and c = " + c_input + "
and d = " + d_input + "
and e = " + e_input + ";")
Problem:
I give the user the option to 'loosen' the constraints, so it is possible that a_input="" and b_input="", etc... Potentially all fields a,b,c,d,e can be empty ("") If a field is omitted, it should not affect the resulting response. In other words, if c is not entered, the result can contain entries where c is TRUE or FALSE
Question:
How do I write ONE query that covers the situation where potentially all fields can be empty, or just some, or none?
Just build the WHERE dynamically. Instead of always searching for c = 'whatever', only include c in the WHERE if the user supplied a value for it.
you could use
Select *
from table
where a in ('','othervalue','othervalue')
and b in ('','othervalue','morevalues')
and so on.....that is like using an or for each field and it will match even if it's empty
This is tricky because the DB contains booleans but the parms are strings. Are the parms always blank. 'true', or 'false'? If so try
(B_input=''
Or (b_input=''true' and b)
Or (b_input='false' and ((not b)))
Not sure if this is possible in MySQL, but I have a column that has business names like:
AT&T Store
O'Reilly's Auto Parts
Burger King
which I import into Sphinx Search with a MySQL query. I have MariaDB, so there is a REGEXP_REPLACE(col, regexp, replace) function, but I'm having trouble figuring out the rest.
What I need is to repeat words with non-alphanumeric characters replaced with and without a space. So the above examples would become:
ATT AT T Store
OReillys O Reilly s Auto Parts
Burger King
Is this possible in a MySQL query? Thanks!
This can be done all at once, but maybe not by SQL primitive regex.
I don't know REGEXP_REPLACE, nor modern day SQL.
Typically its done by three regex.
Pseudo code:
$column_val = "O'Reilly's Auto Parts";
$new_column_val = Replace_Globally(
$column_val,
'\b\w+[[:punct:]](?:[[:punct:]]*\w)+\b',
function( $match ) {
$val = $match.value;
$text1 = Replace_Globally( $val, '[[:punct:]]+', "" );
$text2 = Replace_Globally( $val, '[[:punct:]]+', " " );
return $text1 + " " + $text2;
}
);
So, this might not look like something sql can do, so you might have to get creative.
REGEXP_REPLACE is in MariaDB only, MySQL doesn't have it.
select regexp_replace(regexp_replace(
"AT&T Store
O'Reilly's Auto Parts
Burger King",
'([[:alnum:]]+)[[:punct:]]+([[:alnum:]]+)[[:punct:]]+([[:alnum:]]+)',
'\\1\\2\\3 \\1 \\2 \\3'),
'([[:alnum:]]+)[[:punct:]]+([[:alnum:]]+)',
'\\1\\2 \\1 \\2')