How to convert columns of one row into one row each?
Having data in table as below:
+------+------+------+------+
| col1 | col2 | col3 | col4 |
+------+------+------+------+
| a1 | a2 | a3 | a4 |
+------+------+------+------+
required output: all values in the row should go in single column as below.
+---------------+
| All_cols |
+---------------+
| a1,a2,a3,a4 |
+---------------+
Like in image: Here mytable always have one record, so I need the output as below.
want to avoid below sql as it requires multiple joins:
select a1.name from accounts a1 where a1.id='658f3b73-8260-5a7a-ae7e-54c25deded36'
union
Select a2.name from accounts a1
left join accounts a2 on a1.id=a2.parent_id where a1.id='658f3b73-8260-5a7a-ae7e-54c25deded36'
union
Select a3.name from accounts a1
left join accounts a2 on a1.id=a2.parent_id
left join accounts a3 on a2.id=a3.parent_id where a1.id='658f3b73-8260-5a7a-ae7e-54c25deded36'
union
Select a4.name from accounts a1
left join accounts a2 on a1.id=a2.parent_id
left join accounts a3 on a2.id=a3.parent_id
left join accounts a4 on a3.id=a4.parent_id where a1.id='658f3b73-8260-5a7a-ae7e-54c25deded36'
union
Select a5.name from accounts a1
left join accounts a2 on a1.id=a2.parent_id
left join accounts a3 on a2.id=a3.parent_id
left join accounts a4 on a3.id=a4.parent_id
left join accounts a5 on a4.id=a5.parent_id where a1.id='658f3b73-8260-5a7a-ae7e-54c25deded36'
You could make a UNION on four single calls
SELECT col1 FROM mytable
UNION
SELECT col2 FROM mytable
UNION
SELECT col3 FROM mytable
UNION
SELECT col4 FROM mytable
will output
You say that
mytable always have one record.
Be aware that if you have more than one record, it's wise to use the same WHERE condition on each SELECT statement above.
Related
I want to merge data (name + value/s) in different rows based on another column (name). See example below:
name | value
A | ab
A | ac
A | ad
B | bc
B | bd
I want the output to be like this (in rows and concatenated in one cell):
A ab ac ad
B bc bd
Is this possible with Google sheets query?
I have added a new sheet ("Erik Help"). To achieve the exact results you wanted:
=ArrayFormula({"Result";IF(A2:A="",,IF(COUNTIFS(A2:A,A2:A,ROW(A2:A),"<="&ROW(A2:A))>1,,VLOOKUP(A2:A&"*",REGEXREPLACE(TRIM(SPLIT(FLATTEN(QUERY(QUERY({A2:B}, "Select MAX(Col2) where Col2 IS NOT NULL GROUP BY Col2 PIVOT Col1"),, 9^9)),"~")),"[,\s]+$",""),1,FALSE)))})
This is a more complex formula than I can typically share on this free forum, so I will need to leave the understanding of the formula to you and others who may be interested.
I also left a second formula, which shows only the aggregated list, as a "bonus" for you:
=ArrayFormula({"Aggregated List";REGEXREPLACE(TRIM(SPLIT(FLATTEN(QUERY(QUERY({A2:B}, "Select MAX(Col2) where Col2 IS NOT NULL GROUP BY Col2 PIVOT Col1"),, 9^9)),"~")),"[,\s]+$","")})
I have this table:
Name | Age
Ann | adult
Ann | adult
Andrew | adult
Mike | adult
Ann | teenager
John | teenager
John | teenager
I want this output:
Age | count Name (distinct Names)
adult | 3
teenager | 2
Unfortunately, I can't go further then this formula:
=QUERY(table; "select B, count(A) group by B"; 1)
where the 'table' is the named range with input data. And it gives me this:
Age | count Name
adult | 4
teenager | 3
I need something like:
=QUERY(table; "select B, count(unique(A)) group by A"; 1)
which obviously doesn't work.
So, how can I achieve my target output with querying?
I know, I can do that with pivot tables with countunique function, but I want to go without pivot tables.
One option could be QUERY + UNIQUE:
=QUERY(UNIQUE(A2:B),"SELECT Col2, COUNT(Col1) WHERE Col2 IS NOT NULL GROUP BY Col2")
You can also make use of this formula:
=ARRAYFORMULA(QUERY(UNIQUE({B:B, B:B & A:A, A:A}), "SELECT Col1, COUNT(Col1) WHERE Col1 IS NOT NULL GROUP BY Col1 ORDER BY COUNT(Col1) DESC LABEL COUNT(Col1)'count Name'", 1))
After
Explanation
The formula makes use of the following functions:
ARRAYFORMULA
UNIQUE
QUERY
In order to find the unique values, the UNIQUE is used for the range needed for the query (A:B) such that the sorting and counting is done on this range. The LABEL is used as well in order to set the header name for the resulted column.
Reference
ARRAYFORMULA;
UNIQUE;
QUERY.
I have 2 tabs needing worked with. The first tab, market_pull has a function that pulls information from EVE Online's ESI and sorts the information into several columns:
duration | is_buy_order | issued | location_id | min_volume | order_id | price | range | type_id | volume_remain | volume_total
The second tab, bulk_market_data sorts the information into several columns:
Citadel ID | Item Id | Item Name | Volume Ea | Qty Available | Lowest Price | Total Volume | Jita Sell | ISK Difference | % Difference
I need help with the bulk_market_data tab. I need to use the Item Id column from bulk_market_data as a criteria to compare to market_pull the column type_id pull the MIN value from the corresponding row in the price column.
I need to do essentially the same thing except I need to use the Item Id column from bulk_market_data as a criteria to compare to market_pull the column type_id pull the total SUM value from the all the corresponding rows in the volume_remain column.
I'm using array formulas because in the bulk_market_data tab there is about 10,000 rows and when I had a formula in every row for every column the sheet slowed down drastically. Thank you for your time and HERE is a sample spreadsheet with the concept.
use in F4:
=ARRAYFORMULA(IFNA(VLOOKUP(C4:C, QUERY({market_pull!C4:C, market_pull!J4:K},
"select Col2,sum(Col3) where Col2 is not null and Col1 = FALSE group by Col2"), 2, 0)))
use in G4:
=ARRAYFORMULA(IF(C4:C="",,IFNA(VLOOKUP(C4:C, SORT(QUERY({market_pull!C4:J},
"select Col8,Col6 where Col1 = FALSE"), 1, 1, 2, 1), 2, 0), 0)))
use in H4:
=ARRAYFORMULA(IF(C4:C="",,ROUNDUP(IF(
E4:E1004*F4:F1004=0,,E4:E1004*F4:F1004), 1)))
In a Google Sheets document, I've got a column which contains multiple values.
I want to get the first 20 values by count, and then group the count of the others.
I've got this working code for now:
=QUERY(B2:B165,"select B, count(B) group by B order by count(B) desc limit 20 label B 'Pubblication venue'")
This will get me how many times a value appears in the column, and limit the results to 20. Now I need the count of the other results (which are ~100).
Let me explain by example. With my code I'm able to get the count of A, B and C. Now I want the count of others.
+-------+----+
| A | 5 |
+-------+----+
| B | 2 |
+-------+----+
| C | 4 |
+-------+----+
| Other | 90 |
+-------+----+
={QUERY(B2:B165,
"select B, count(B)
where B is not null
group by B
order by count(B) desc
limit 20
label B 'Pubblication venue'", 0);
{"Other", COUNTA(IFERROR(QUERY(QUERY(B2:B165,
"select B, count(B)
where B is not null
group by B
order by count(B) desc
offset 20
label count(B)''", 0),
"select Col1", 0)))}}
_____________________________________________________________
={QUERY(B2:B165,
"select B, count(B)
where B is not null
group by B
order by count(B) desc
limit 20
label B 'Pubblication venue'", 0);
{"Other", SUM(IFERROR(QUERY(QUERY(B2:B165,
"select B, count(B)
where B is not null
group by B
order by count(B) desc
offset 20
label count(B)''", 0),
"select Col2", 0)))}}
First I will explain what I am trying to achieve
I have a list with several columns and rows, like this
| a | b | c
-----------------
1 | f1 | 4 | f
2 | f2 | 9 | k
3 | f3 | 1 | x
In another column outside the list I want to write a list with variable size, that contains values of column 'a'.
This list would be for a query like this (suppose that I write the variable list in the column 'd')
=QUERY(A2:D3,"SELECT a, b, c WHERE A IN D")
I think the problem is that I can not use the IN operator from sql
Is there a way to achieve this? The most important part is that I want to write manually a list with variable size for the query?
Thy this formula, in cell E1:
=FILTER(A:C,REGEXMATCH(A:A,JOIN("|",FILTER(D:D,D:D<>""))))
FILTER formula can take any array as condition parameter. REGEXMATCH + JOIN with pipe | will match true when any text from D matches text from A.
This formula is good for one more reason, you may use it as source for further query, like this: =query(my_formula, "select Col1, Col3 where Col2 > 0")