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<>"")))))
Related
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)
I am using node.js with squel.js for SQL database.
I am storing array of string into one table field.
Let us say, My table name is like XYZ.
And my table data is like
id col2
1 '["a", "b", "x", "y"]'
2 '["x", "y", "q"]'
3 '["q", "e"]'
4 '["p", "q"]'
Now i want to know how to make query for search like
if i want to how to make query if i want to fetch records like
If i want to fetch all records which contains like ["x", "y"]. In this scenario i want expect result id 1 and 2.
If i want to fetch all records which contains like ["q"]. In this scenario i want expect result 2 and 4.
Please help me here i stuck here. Thanks in advance.
If I understand your question correctly,just use LIKE can do it
For the first requirement,you can use
SELECT * FROM yourtable WHERE col2 LIKE `%"x"%` AND col2 LIKE `%"y"%`
OR
SELECT * FROM yourtable WHERE col2 LIKE `%"x","y"%`
Since I do not know the format exactly,is it just check match x and y? Or just match "x","y" exactly?
For the second requirement,you can use
SELECT * FROM yourtable WHERE col2 LIKE `%"q"%`
col2 can be a JSON column, thus you can do something like:
SELECT * FROM XYZ WHERE JSON_CONTAINS(col2, JSON_ARRAY('x', 'y'));
SELECT * FROM XYZ WHERE JSON_CONTAINS(col2, JSON_ARRAY('q'));
The second query would returns rows 2, 3 and 4, but I guess your expectation might be incorrectly stated, since 'q' exists in the col2 array for all of those.
I tried something out. Here is a simple example in SQL Fiddle: Example
There is a column someNumbers (comma-seperated numbers) and I tried to get all the rows where this column contains a specific number. Problem is, the result only contains rows where someNumbers starts with the specific number.
The query SELECT * FROM myTable where 2 in ( someNumbers ) only returns the row with id 2 and not the row with id 1.
Any suggestions? Thank you all.
You are storing data in the wrong format! You should not be storing multiple values in a single string column. You should not be storing numbers as strings. Instead, you should have a junction table with one row per id and per number.
Sometimes, you just have no choice, because someone else created a really poorly designed database. For these situations, MySQL has the function find_in_set():
SELECT *
FROM myTable
WHERE find_in_set(2, someNumbers ) > 0;
The right solution, however, is to fix the data model.
While Gordon's answer is a good one, here is a way to do this with like
SELECT * FROM myTable where someNumbers like '2,%' or someNumbers like '%,2,%' or someNumbers like '%,2'
The first like checks if your array starts with the number you are looking for (2). The second one checks if 2 is within the array and the last like tests for appearance at the end.
Note that the commas are essential here, because something like '%2%' would also match ...,123,...
EDIT: As suggested by the OP it may happen that only a single value is present in the row. Consequently, the query must check this case by doing ... someNumbers = '2'
I would suggest this query :
SELECT * FROM myTable where someNumbers like '%2%'
It will select every entry where someNumbers contains '2'
Select * from table_name where coloumn_name IN(value,value,value)
you can use it
here is my query
SELECT con_serial,column2,column3
FROM
(SELECT con_serial,column2,column3
FROM big_table
WHERE ISNULL(contact1, '')+'#'+ISNULL(contact2, '')+'#'+ISNULL(contact3, '')+'#'+ISNULL(contact4, '')+'#'+ISNULL(contact5, '')
LIKE '%' + '".$conserial."' + '%') AS a
WHERE con_serial
IN('".$contact1."','".$contact2."','".$contact3."','".$contact4."','".$contact5."')
at the inner select i wish to get the rows which have this value $conserial in one of their 5 columns(contact1...contact5)
and the outer select to choose the rows from it that their column con_serial is one of the variables ($contact1...$contact5)
can anybody see what's wrong here?
Despite your new formulation, it remains very much unclear.
Nevertheless I'll try to give you an answer, based on what I can guess...
First here is how I'd reformulate your need:
you have some values in PHP variables: one $conserial and five $contact# where # is 1-5
the table structure contains at least these columns: con_serial, column2, column3, and five contact# where # is 1-5
you want to select rows where both (here is the most strange part of your need):
at least one of the contact# columns matches the PHP $conserial value
the con_serial column matches at least one of the PHP $contact# values
That said, note that you don't need to have two nested SELECT: you only want each row to satisfy two conditions, so they can be ANDed in the WHERE clause.
Based on that, your query should be:
$query = "
SELECT con_serial, column2, column3
FROM big_table
WHERE con_serial IN ('$contact1', '$contact2', '$contact3', '$contact4', '$contact5')
AND '$con_serial' IN (contact1, contact2, contact3, contact4, contact5)
";
i guess the sum part in where clause is nut allowed
any way i've solved this with using two IN like this
SELECT con_serial,,column2,column3
FROM(SELECT con_serial,column2,column3
FROM
big_table
WHERE '".$conserial."' IN(contact1,contact2,contact3,contact4,contact5)) a
WHERE con_serial IN('".$contact1."','".$contact2."','".$contact3."','".$contact4."','".$contact5."'
this was wat i wanted Tnx any way ;)
I am looking to compare the results of 2 cells in the same row. the way the data is structured is essentially this:
Col_A: table,row,cell
Col_B: row
What I want to do is compare when Col_A 'row' is the same as Col_B 'row'
SELECT COUNT(*) FROM MyTable WHERE Col_A CONTAINS Col_B;
sample data:
Col_A: a=befd-47a8021a6522,b=7750195008,c=prof
Col_B: b=7750195008
Col_A: a=bokl-e5ac10085202,b=4478542348,c=pedf
Col_B: b=7750195008
I am looking to return the number of times the comparison between Col_A 'b' and Col_B 'b' is true.
This does what I was looking for:
SELECT COUNT(*) FROM MyTable WHERE Col_A LIKE CONCAT('%',Col_B,'%');
I see You answered Your own question.
SELECT COUNT(*) FROM MyTable WHERE Col_A LIKE CONCAT('%',Col_B,'%');
is good from performance perspective. While normalization is very good idea, it would not improve speed much in this particular case. We must simply scan all strings from table. Question is, if the query is always correct. It accepts for example
Col_A: a=befd-47a8021a6522,ab=7750195008,c=prof
Col_B: b=7750195008
or
Col_A: a=befd-47a8021a6522,b=775019500877777777,c=prof
Col_B: b=7750195008
this may be a problem depending on the data format. Solution is quite simple
SELECT COUNT(*) FROM MyTable WHERE CONCAT(',',Col_A,',') LIKE CONCAT('%,',Col_B,',%');
But this is not the end. String in LIKE is interpreted and if You can have things like % in You data You have a problem. This should work on mysql:
SELECT COUNT(*) FROM MyTable WHERE LOCATE(CONCAT(',',Col_B,','), CONCAT(',',Col_A,','))>0;
SELECT * FROM MyTable WHERE Col_A = Col_B (AND Col_A = 'cell')
^^ Maybe you are looking for this statement. The part in brackets is optional.
If this is not the solution, please supply us with further information.
The easiest way would be to use the IN operator.
SELECT COUNT(*) FROM MyTable WHERE Col_A IN (Col_B);
More info on the IN operator: http://www.w3schools.com/sql/sql_in.asp
There's also the SUBSTRING() or MID() (depending on what you're using) function if you know that the substring will be in the same position everytime.
MID()/SUBSTRING() function: http://www.w3schools.com/sql/sql_func_mid.asp
You can use SUBSTRING_INDEX to extract a delimited field from a column.
SELECT COUNT(*)
FROM MyTable
WHERE Col_B = SUBSTRING_INDEX(SUBSTRING_INDEX(Col_A, ',', 2), ',', -1)
You need to call it twice to get a single field. The inner call gets the first two fields, the outer call gets the last field of that.
Note that this will be very slow if the table is large, because it's not possible to index substrings in MySQL. It would be much better if you normalized your schema so each field is in a separate column.
If column Col_a has data with format table,row,cell then search expression will be next:
SELECT COUNT(*) FROM MyTable AS MT
WHERE SUBSTRING(Col_A,
INSTR(Col_A, ',b=') + 3,
INSTR(Col_A, ',c=') - INSTR(Col_A, ',b=') + 3) = Col_B