Google-sheet: select related cell from imported range - google-apps-script

I have the following google-sheet:
| | A | B | C | D |
| 1| | Item1 | Item2 | Item3 |
| 2| Value1 | yes | no | no |
| 3| Value2 | no | no | yes |
| 4| Value3 | no | yes | no |
I need to import specific data from this sheet into another range so result should contain values from A, which have "yes" between B:E, and related item from the first row, like:
Value1 Item1
Value2 Item3
Value3 Item2
I can import using query and condition for "yes", but no ideas how to read related cell with Item1, Item2.. from first row above:
=query({importrange("my_range_id", "Data!A1:Z999"}, "select Col1 where Col2='yes' or Col3='yes' or Col4='yes'")
Thanks for help in advance!

May be you can first import data, then retrieve the column corresponding to 'yes' by this formula using matrix multiplication
={A2:A5,arrayformula(mmult((if(B2:D5="yes",1,0)),transpose(column(B1:D1)-1)))}
and then retrieve the header of the column
=offset($A$1,,G2)

As an addition, in case you still want to use a query:
Try this in F2:
=ARRAYFORMULA(QUERY(SPLIT(TRANSPOSE(SPLIT(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
IF(A2:A<>"",
"♦"&A2:A&"😊"&B1:D1&"😊"&B2:D, ))
,,999^99)),,999^99), "♦")), "😊"),"Select Col1,Col2 where Col3 contains 'YES'"))

Here is an integrated formula
={A2:A,arrayformula(vlookup(mmult((if($B$2:$D="yes",1,0)),transpose(column($B$1:$D$1)-1)),{arrayformula(transpose(COLUMN(A1:D1)-1)),transpose(A1:D1)},2,0))}

Related

Union in same table with ACCESS

I have a table with this sort of data:
+------------+----------+----------+
| Unique ID | Name | Class |
+------------+----------+----------+
| 1 | Name 1 | Class A |
| 2 | Name 2 | "" |
| 3 | Name 3 | Class C |
| 4 | Name 1 | "" |
| 5 | Name 4 | "" |
| 6 | Name 4 | "" |
+------------+----------+----------+
I am trying to do something I thought was simple, but i did not find so.
I am trying to "extract" only the lines with an empty string value in 'Class' for a group of equal names.
So in this case I would get this result :
+------------+----------+--------+
| Unique ID | Name | Class |
+------------+----------+--------+
| 2 | Name 2 | "" |
| 5 | Name 4 | "" |
+------------+----------+--------+
So not Name 1 because even though there is a line with "" there is another line with 'Class A'.
I thought a UNION would do the job but I am not gettgin anything because I think unions are for two tables but the problem here is I have the data in the same table.
Thank you for your help
Access syntax may be a bit different but this returns what you want in Oracle:
SELECT distinct Name, Class FROM table1 Where Name NOT in (select name from table1 where class is not null)
A Union melds two result sets, whether or not they come from the same table is irrelevant. What you want to do is omit from the result set the rows with the same name AND class is not null. Not having your query to expand on or change is a problem, but if you add a clause that says something like where "name not in (select name from table where class is not null);", that may do it.

Use result of a rails where query for another query

I'm curious if there is an easy way to archive this query:
SomeTable.where(value_b: SomeTable.where(value_a: 1).pluck(:value_b))
For an example, if we have:
SomeTable
|id | a | b |
|1 | 1 | 2 |
|2 | 2 | 2 |
|3 | 3 | 3 |
|4 | 4 | 4 |
And the user enter number 1 into the system, I want to be able to find value of b where value of a = 1, which is 2. Then use this value of b to find all the records with this value.
So, I want to be able to use 1 and extract SomeTable with id: 1 and 2
Thanks!
SomeTable.joins("JOIN some_table as replica on replica.value_b = some_table.value_b")
.where(replica: {value_a: 1})
SomeTable.where("b in (SELECT b FROM some_tables where a = ?)", a_value)
a_value is your 'a' value to find 'b' value

Cross table with multiselect

I have a table with 2 Columns, filled with strings
CREATE TABLE [tbl_text]
(
[directoryName] nvarchar(200),
[text1] nvarchar(200),
[text2] nvarchar(200)
)
The Strings are build like the following
| Text1 | Text2 |
|------------|----------|
|tz1 tz3 tz2 | al1 al2 |
| tz1 tz3 | al1 al3 |
| tz2 | al3 |
| tz3 tz2 | al1 al2 |
Now i want to Count how many times the TestN or TextN are resulting in the
| Text1 | al1 | al2 | al3 |
|-------|------|------|------|
| tz1 | 2 | 1 | 1 |
| tz2 | 2 | 2 | 1 |
| tz3 | 3 | 2 | 1 |
i tried solving it with an sql-query like this:
TRANSFORM Count(tt.directoryName) AS Value
SELECT tt.Text1
FROM tbl_text as tt
GROUP BY tt.Text1
PIVOT tt.Text2;
This works fine if i got fields only with one value like the third column (the complete datasource has to be like a one-value-style)
But in my case i'm using the strings for a multiselect...
If i try to conform this query onto a datasource filled with the " " between the values the result is complete messed up
Any suggestions how the query should look like to get this result ?
You'll have to split the strings inside Text1/Text2 before you can do anything with them. In VBA, you'd loop a recordset, use the Split() function and insert the results into a temp table.
In Sql Server there are more powerful options available.
Coming from here: Split function equivalent in T-SQL? ,
you should read this page:
http://www.sommarskog.se/arrays-in-sql-2005.html#tablelists

MySQL structure for 2D array lookup?

sIm trying to create a mysql database for looking up in a big dataset. I have a set of data (n number) samples and a list of genes in my data collection and I would like to able to lookup a value based on pairing one sample with one gene. In the the table below I have the data for genes, samples and value, and I would like to know the best way sort set this up on mysql for fast lookup.
In my data I have about 35.000 different genes and an aomunt of samples that can vmay vary between 2000-15000. (I dont have a specific number, because I have finished my data yet.)
|------------------------------------------------------|
| | sample1 | sample2 |
|------------------|----------------|------------------|
| gene1 | value1 | value2 |
|------------------|----------------|------------------|
| gene2 | value3 | value4 |
|------------------|----------------|------------------|
What is the best way to put this data up on a mysql database? The "easiest" way that I thought of right away is just throwing it in the database like in the following output, which seems like it would be a big disaster. How can I approach this correctly?
|----------------------------------------- ------------|
| gene1 | sample1 | value1 |
|------------------|----------------|------------------|
| gene1 | sample2 | value2 |
|------------------|----------------|------------------|
| gene2 | sample1 | value3 |
|------------------|----------------|------------------|
| gene2 | sample2 | value4 |
|------------------|----------------|------------------|

SSRS compare rows by text

I have table like this:
+---------------------------------+
| Group1 |
+-------+--------+----------------+
| Item1 | Value1 | Compare |
+-------+--------+----------------+
| Item2 | Value2 | Compare |
+-------+--------+----------------+
| Item3 | Value3 | Do not compare |
+-------+--------+----------------+
| Group2 |
+-------+--------+----------------+
| Item1 | Value1 | Compare |
+-------+--------+----------------+
| Item2 | Value2 | Compare |
+-------+--------+----------------+
| Item3 | Value3 | Do not compare |
+-------+--------+----------------+
In a table like this i can have multiple groups with multiple items. I need to compare only items, where "Compare" is written. For example, i need to change the color of Value1 in Group2, if that value is grater thant Value1 in Group1. How can i do that? If there would be only one line to compare in each group, it would be easy, but i don't know what to do when there are multiple lines.
So you need to compare new groups with an old group that you know the name of? This query will give you any new group and compare any items named the same where they have been flagged to be compared with the original group, Group1, and give you the old and new values:
SELECT New.GroupName AS NewGroup, New.ItemName AS NewItem,
Old.Value AS OldValue, New.Value AS NewValue
FROM MyTable New
INNER JOIN MyTable Old ON
Old.GroupName = 'Group1' AND
Old.ItemName = New.ItemName
Old.CompareField = 'Compare' AND
New.CompareField = 'Compare'