How to filter rows instead of columns in Google Sheets? - google-apps-script

I have a shared google sheet at work that would look like what is down below. I want to filter out all the rows that have 0's in all 3 columns. I've tried a =filter function, but the problem with it is that it only does 1 row/column at a time. Any ideas?
value 1 value 2 value 3
8485 2515 0
121 0 0
36 0 415
0 0 0
0 0 0
0 1551 0

try:
=QUERY({A:C, TRANSPOSE(QUERY(TRANSPOSE(A:C),,9^9))},
"select Col1,Col2,Col3 where Col4 <> '0 0 0'")

Solution:
If you want to do that using Google Apps Script, then:
function myFunction() {
const sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
const data = sheet1.getRange('A1:C'+sheet1.getLastRow()).getValues();
const fdata = data.filter(r=>r[0]!=0 || r[1]!=0 || r[2]!=0);
sheet1.getRange(1,5,fdata.length,fdata[0].length).setValues(fdata);
}
Input of my sheet and output:

Here are a few simple solutions:
=QUERY(
A:C;
"WHERE A IS NOT NULL
AND A * A + B * B + C * C <> 0"
)
=QUERY(
A:C;
"WHERE A IS NOT NULL
AND (A <> 0 OR B <> 0 OR C <> 0)"
)

[Solved]:
You can try this solution:
In new column (as example [Column D]) add a Concatenate formula like this:
=A1&B1&C1
Applicate to All Rows from row D1 to D7.
Then you just Add a Filter to this column: [Column D] Unselect this Value "000" with three zeros.

Related

How to find 7 consecutive non zero values in SQL?

If we have a table of 15 columns, the first one is id, and the other columns have numeric
data type,
in MySQL or a similar languages, how can I find if the record have 7 consecutive non zero values, meaning that 7 of the contiguous columns have a value not equals to zero?
We can write a query has a lot of OR operations to check that each 7 columns don't have 0 value, but I want to know if there is shorter way
Try the following using CONCAT and INSTR functions, explanation is within query comments:
/*
check if the col value is not equal to 0,
this will return 1 if true and 0 if false,
and concatenate all of the returned values.
*/
with concat_cols as
(
select *,
concat(
col1<>0, col2<>0, col3<>0, col4<>0, col5<>0,
col6<>0, col7<>0, col8<>0, col9<>0, col10<>0,
col11<>0, col12<>0, col13<>0, col14<>0, col15<>0
) as con_col
from table_name
)
/*
using the instr function, check if the concatenated
result contains 7 consecutive 1s (7 not 0 values).
*/
select * from concat_cols
where instr(con_col, repeat('1', 7))
See demo
One way:
field(0,col1,col2,col3,col4,col5,col6,col7) = 0 or
field(0,col2,col3,col4,col5,col6,col7,col8) = 0 or
field(0,col3,col4,col5,col6,col7,col8,col9) = 0 or
field(0,col4,col5,col6,col7,col8,col9,col10) = 0 or
field(0,col5,col6,col7,col8,col9,col10,col11) = 0 or
field(0,col6,col7,col8,col9,col10,col11,col12) = 0 or
field(0,col7,col8,col9,col10,col11,col12,col13) = 0 or
field(0,col8,col9,col10,col11,col12,col13,col14) = 0 or
field(0,col9,col10,col11,col12,col13,col14,col15) = 0
SELECT id
FROM your_table
WHERE SUM(CASE WHEN column_2 > 0 THEN 1 ELSE 0 END +
CASE WHEN column_3 > 0 THEN 1 ELSE 0 END +
CASE WHEN column_4 > 0 THEN 1 ELSE 0 END +
CASE WHEN column_5 > 0 THEN 1 ELSE 0 END +
CASE WHEN column_6 > 0 THEN 1 ELSE 0 END +
CASE WHEN column_7 > 0 THEN 1 ELSE 0 END +
CASE WHEN column_8 > 0 THEN 1 ELSE 0 END) >= 7

How to add alphanumeric values in a speardsheet if they are comma separated?

Suppose, we have cells as below:
Cell Value Legend
==========================
A1 1,A // A = 1
A2 2,AA // AA = 2
A3 3,L // L = -1
A4 4,N // N = 0
I want the total to be calculated separately in other cells as:
A5 = SUM(1, 2, 3, 4) = 1 + 2 + 3 + 4 = 10
A6 = SUM(1*A, 2*AA, 3*L, 4*N) = 1 + 4 - 3 + 0 = 2
Considering it may require separate functions in App Script, I tried to use SPLIT and SUM them, but it's not accepting the values. I asked a related question: How to pass multiple comma separated values in a cell to a custom function?
However, being a novice in spreadsheet, I am not sure if my approach is correct.
How to add alphanumeric values separately as stated above?
you can create a small lookup table (legend) and then for the first sum try something like
=ArrayFormula(sum(iferror(REGEXEXTRACT(A1:A4, "[0-9-.]+")+0)))
and for the last
=sum(ArrayFormula(iferror(regexextract(A1:A4, "[0-9-.]+")*vlookup(regexextract(A1:A4, "[^,]+$"),D1:E4, 2, 0 ))))

Consider the 4-to-1 multiplexer shown below. What values must inputs A, B, C, D be so that the multiplexer implements the function

I am completing some practice quizzes online in preperation for a uni exam next week, and I am a bit stumped as to how to solve this problem:
Consider the 4-to-1 multiplexer shown below. What values must inputs
A, B, C, D be so that the multiplexer implements the function
If anyone would'nt mind giving me a few tips as to how to solve this, I would much appreciate it.
Thanks
Corey
I'll assume: X = S1 * ~S0 + S0 * ~S1 as your posted picture is unreadable.
First you need to know the multiplexor transfer:
S1 S0 X
0 0 A
0 1 B
1 0 D
1 1 C
Then, apply the function to all your possible input combinations.
f(s1,s0)=S1*~S0+~S1*S0
f(0,0) = 0
f(0,1) = 1
f(1,0) = 1
f(1,1) = 0
And finally fill the table with X=f(s1,s0)
S1 S0 X f(s1,s0)
0 0 A 0
0 1 B 1
1 0 D 1
1 1 C 0

SQL statement equivalent to ternary operator

I would like to create a statement that is equivalent to (x - y == 0) ? return 0 : return 100 in MySQL. Something that might look like this:
SELECT id, [(integer_val - 10 == 0) ? 0 : 100] AS new_val FROM my_table
I want to compare an attribute in each row to a certain number, and if the difference between that number and the number in the row is 0, I want it to give me 0, otherwise, I want it to give me 100.
Example:
Applying this query on my_table (with 10 being the 'compared to' number):
id | integer_val
===================
1 10
2 10
3 3
4 9
Would return this:
id | new_val
===================
1 100
2 100
3 0
4 0
How can I do this?
Try this:
SELECT id, IF(integer_val = 10, 100, 0) AS new_val
FROM my_table;
OR
SELECT id, (CASE WHEN integer_val = 10 THEN 100 ELSE 0 END) AS new_val
FROM my_table;
Use case when statement:
select *, (case when integer_val = 10 then 100 else 0 end) as New_Val
from yourtable
Try using the IF function:
SELECT id, IF(integer_val - 10 = 0, 0, 100) AS new_val FROM my_table
(I stuck with your condition expression, but it can be simplified a bit since integer_value - 10 = 0 has exactly the same truth value as integer_value = 10.)
Note that the IF function is different from MySQL's IF statement used for stored programs.

HowTo: select all rows in a cell array, where a particular column has a particular value

I have a cell array, A. I would like to select all rows where the first column (for example) has the value 1234 (for example).
When A is not a cell array, I can accomplish this by:
B = A(A(:,1) == 1234,:);
But when A is a cell array, I get this error message:
error: binary operator `==' not implemented for `cell' by `scalar' operations
Does anyone know how to accomplish this, for a cell array?
The problem is the expression a(:,1) == 1234 (and also a{:,1} == 1234).
For example:
octave-3.4.0:48> a
a =
{
[1,1] = 10
[2,1] = 13
[3,1] = 15
[4,1] = 13
[1,2] = foo
[2,2] = 19
[3,2] = bar
[4,2] = 999
}
octave-3.4.0:49> a(:,1) == 13
error: binary operator `==' not implemented for `cell' by `scalar' operations
octave-3.4.0:49> a{:,1} == 13
error: binary operator `==' not implemented for `cs-list' by `scalar' operations
I don't know if this is the simplest or most efficient way to do it, but this works:
octave-3.4.0:49> cellfun(#(x) isequal(x, 13), a(:,1))
ans =
0
1
0
1
octave-3.4.0:50> a(cellfun(#(x) isequal(x, 13), a(:,1)), :)
ans =
{
[1,1] = 13
[2,1] = 13
[1,2] = 19
[2,2] = 999
}
I guess the Class of A is cell. (You can see in the Workspace box).
So you may need to convert A to the matrix by cell2mat(A).
Then, just like Matlab as you did: B = A(A(:,1) == 1234,:);
I don't have Octave available at the moment to try it out, but I believe that the following would do it:
B = A(A{:,1} == 1234,:);
When dealing with cells () returns the cell, {} returns the contents of the cell.