Stack multiple columns on top of eachother on Google Sheet? - arrays

I am using Google SpreadSheet, and I'm trying to import data from different sources together using a combination of query, importrange and curly brackets:
={Query(Importrange(SheetA),"select Col1,Col2,Col3,Col4 where Col1 is not null");
Query(Importrange(SheetB),"select Col1,Col2,Col3,Col4 where Col1 is not null")}
But the result formula only returns SheetA data.
Since I only need 4 particular columns from SheetA and SheetB, I'd want the columns from both sheets to stack on top of eachother like this:
Col1
Col2
Col3
Col4
Data SheetA
Data SheetA
Data SheetA
Data SheetA
Data SheetB
Data SheetB
Data SheetB
Data SheetB
I tried running each =query(importrange) individually and combine them afterwards using {A:D;E:H} but it gives Result was not automatically expanded, please insert more rows error.
I tried looking for other similar threads but they were mostly about stacking different columns into one singular column. Can you help me with my case?

first, you need to run every importrange separately as standalone formula to connect your sheets by allowing access
when done, only then, you can run this array:
=QUERY({IMPORTRANGE("url_or_id", "SheetA!A:D");
IMPORTRANGE("url_or_id", "SheetB!A:D")},
"where Col1 is not null", 0)

Turns out the issue was due to the infinite blank rows when trying to combine 2 infinite ranges A:D and E:H,
By enclosing them with a query, remove null value condition, my formula works fine now:
=query({A:D;E:H},"Select * where Col1 is not null")
Nevertheless, are there any ways to stack those 4 columns using the original singular formula using query, importrange and curly brackets?
={Query(Importrange(SheetA),"select Col1,Col2,Col3,Col4 where Col1 is not null");
Query(Importrange(SheetB),"select Col1,Col2,Col3,Col4 where Col1 is not null")}

Where you have 2 source files (Source1, Source2), and you want to use stack the data using IMPORTRANGE into another file (Combined)
There are 2 parts to this solution, and you can do Part 1 and Part 2 in any order.
Part 1
Give approvals to each of the imported ranges.
On a temporary sheet (that can be removed later) in the Combined file.
Use a standard IMPORTRANGE formula like this for Source1 (example, where you must replace "Source1_ID" with the ID of your Source1 file):
=IMPORTRANGE("https://docs.google.com/spreadsheets/d/Source1_ID", "Sheet1!A2:D")
After you have given approval, delete the formula from the cell, and do it again for Source2 (example, where you must replace "Source2_ID" with the ID of your Source2 file)
=IMPORTRANGE("https://docs.google.com/spreadsheets/d/Source2_ID", "Sheet1!A2:D")
Part 2
In the Combined file, go to the sheet where you want the combined data, and enter in the complete QUERY formula (example, where you must replace "Source1_ID" and "Source2_ID" with the ID's of both of your source files)
=QUERY({IMPORTRANGE("https://docs.google.com/spreadsheets/d/Source1_ID", "Sheet1!A2:D"); IMPORTRANGE("https://docs.google.com/spreadsheets/d/Source2_ID", "Sheet1!A2:D")}, "Select Col1, Col2, Col3, Col4 Where Col1 Is Not Null")

So we have 3 things to do:
Transpose so the columns are below the other.
Flatten so that it is ordered as necessary.
Filter to remove extra blank columns we selected.
This will work wonders considering the columns are different lengths and are already next to each other as in a table.
If columns are at multiple places use {A:A;D:D} to select columns and remove flatten and transpose.
=filter(flatten(transpose(A1:D10)), len(flatten(transpose(A1:D10))))

Related

How to match a substring exactly in a string in SQL server?

I have a column workId in my table which has values like :
W1/2009/12345, G2/2018/2345
Now a user want to get this particular id G2/2018/2345. I am using like operator in my query as below:
select * from u_table as s where s.workId like '%2345%' .
It is giving me both above mentioned workids. I tried following query:
select * from u_table as s where s.workId like '%2345%' and s.workId not like '_2345'
This query also giving me same result.
If anyone please provide me with the correct query. Thanks!
Why not use the existing delimiters to match with your criteria?
select *
from u_table
where concat('/', workId, '/') like concat('%/', '2345', '/%');
Ideally of course your 3 separate values would be 3 separate columns; delimiting multiple values in a single column goes against first-normal form and prevents the optimizer from performing an efficient index seek, forcing a scan of all rows every time, hurting performance and concurrency.

How to add values in cells ONLY when other columns contain data from a query result

Link to example file:
https://docs.google.com/spreadsheets/d/1dCQSHWjndejkyyw-chJkBjfHgzEGYoRdXmPTNKu7ykg/edit?usp=sharing
The tab "Source data" contains the data to be used in the query on the tab "Query output". The tab "Desired result" shows what I would like the end result to look like.
The goal I'm trying to achieve is to have the formula in cell A2 on the tab "Query output" to populate the data in all four of the columns, so that it looks exactly like the "Desired result" tab. I know I can get the same result simply by entering additional formulas in C2 and D2, but this is not the objective, I need the results to come specifically from the single formula in A2.
The information in the "Additional data 1" column should simply repeat the word "Test" for every row that contains data in the first two columns. The information in the "Additional data 2" column should simply repeat the data from cell 'Source data'!A1 for every row that contains data in the first two columns.
Please feel free to edit the example file as it only contains dummy data. If you like, you can copy the tab "Query output" to create your own working formula for illustrative purposes.
EDIT:
I'm thinking along the lines of creating an array that consists of the required data for the columns "Additional data 1" and "Additional data 2" and then combining that array with the array of the query result which provides the first two columns. I've been experimenting with this in various ways, but so far the only result I have achieved is an error on the first cell of the query results. I also have no idea yet how I could make sure that the second array contains an equal amount of rows to the query result.
You can add static data into query:
=QUERY('Source data'!A3:B,"SELECT A,B, 'Test', '" & 'Source data'!A1 &"' WHERE A IS NOT NULL LABEL A '', B '', 'Test' '', '" & 'Source data'!A1 &"' ''")
Many thanks to #basic for the provided assistance! The insights were a great help to solving my issue. That said, I have muddled along a bit, and I've come up with a slightly different solution which I find better suited as it gives true blank values instead of a column filled with spaces.
First of all, instead of querying directly on the source data, I built an array and queried on that. I used the two existing columns (A and B) from the source data and added a third column to the array which does not exist in the source data. In order to make sure that the third column would consist of blank values, I used the IFERROR formula.
=IFERROR(0/0)
The formula above returns a blank because dividing by zero forces an error and the IFERROR method returns a blank unless an alternative return value is specified.
In order to be able to use this formula in an array however, it had to be tweaked slightly, because as it is it would only return a single blank cell value instead of a column of blank values. To do this, I used an already existing column from the source data, and then encapsulated it in an ARRAYFORMULA.
=ARRAYFORMULA(IFERROR('Source data'!A3:A/0))
Using this, the resulting array has the following formula.
=ARRAYFORMULA({'Source data'!A3:A,'Source data'!B3:B,IFERROR('Source data'!A3:A/0)})
This creates an array consisting of the two original columns A and B from the source data, plus an additional third column filled with blank values. This array can now be queried upon, and using the tricks previously provided by #basic the desired result as specified in the original question can be achieved.
Due to the query now being used upon a user-defined array, the columns in the SELECT statement now have to be referred to as Col1, Col2, Col3, instead of A, B, C. The final formula now looks like this.
=QUERY(ARRAYFORMULA({'Source data'!A3:A,'Source data'!B3:B,IFERROR('Source data'!A3:A/0)}),"SELECT Col1,Col2,'Test',Col3,'"&'Source data'!A1&"' WHERE Col1 IS NOT NULL LABEL 'Test' '','"&'Source data'!A1&"' ''")
I hope this information may prove of use to someone else as well.

Match a unique column to two separate columns

I have one column in google sheet with unique values. I then want to match this column to two separate columns. See google sheet below for an example. I want the output in column "B".
[https://docs.google.com/spreadsheets/d/10AOYlqsScoDUfcOaJd2opjjC_8zXSOP3_HBTOCCrb6U/edit#gid=0][1]
You can use maxifs:
=maxifs($F$4:$H$14,$E$4:$G$14,A4)
A longer approach could be the following:
=max(vlookup(A4,$E$4:$F$14,2,false),vlookup(A4,$G$4:$H$14,2,false))
try:
=QUERY(FLATTEN(F4:F14, H4:H14), "where Col1 is not null")

Formula to make the outputs of two separate Filter functions display consecutively

So I have a workbook with 3 sheets. The first contains data from A:Z. The second also contains data from A:Z, but the data is different. The third page is used to query the other by using Filter.
I want to be able to write a single formulate that will Filter data from sheet 1 and display the results, and then starting from the next available row, Filter data from sheet2 and display those results, without any gaps or overwriting.
If the two filters are something like this:
=FILTER(sheet1!A:Z, sheet1!A:A="Bob")
and
=FILTER(sheet2!A:Z, sheet2!A:A="Bob")
So, the only difference between the two filters is the sheet name.
I won't know in advance how many rows of output each filter will produce, so I can't just put the second Filter formula in a cell in a lower row, because I don't want there to be any blanks rows between the two sets of output.
I thought that I could use COUNT or COUNTA to work out how many rows of output there's be from the first Filter formula, but even knowing that, how could I choose in which row the second Filter would begin displaying results?
I thought of using &, but that just puts the first result from each sheet into one cell and gives no other rows or columns of results.
I tried ARRAYFORMULA, but I've never used that before and don't think it is suitable.
I tried JOIN, but it gives an error saying it can only be used for a single row or a single column.
UPDATE: Here's my new code, based on player0's advice
={IFERROR(FILTER('2019 (H904)'!B9:AK, '2019 (H904)'!C9:C=A1, '2019
(H904)'!J9:J=E1), SUBSTITUTE(COLUMN(B9:AK)^0, 1, );
IFERROR(FILTER('2018 (H517)'!B9:AK, '2018 (H517)'!C9:C=A1, '2018
(H517)'!J9:J=E1), SUBSTITUTE(COLUMN(B9:AK)^0, 1, )}))
But I get a formula parse error.
put them in array:
={FILTER(sheet1!A:Z, sheet1!A:A="Bob");
FILTER(sheet2!A:Z, sheet2!A:A="Bob")}
the issue is if one of them outputs nothing. then you need something like this:
=ARRAYFORMULA(QUERY({
IFERROR(FILTER(sheet1!A:Z, sheet1!A:A="Bob"), SUBSTITUTE(COLUMN(A:Z)^0, 1, );
IFERROR(FILTER(sheet2!A:Z, sheet2!A:A="Bob"), SUBSTITUTE(COLUMN(A:Z)^0, 1, )},
"where Col1 is not null", 0))

How can I add column names in each row from select query in SQL Server?

My table structure is something like this:
Domain Label1 Label2 ... LabelN
foo.com NULL X ... NULL
bar.com Y Z ... NULL
.
.
xyz.com NULL NULL ... M
I want the output to be something like this (in text format):
foo.com,Label2:X
bar.com,Label1:Y|Label2:Z
.
.
xyz.com,Label3:M
Basically, I want to remove NULL entries as well as I want column labels to be present in the final output. I have tried to use XML path for it but it concatenates all rows together and doesn't remove NULL.
Has someone tried to solve this?
I don't see how you'd be able to do this by joining with the sys table in a single query and get the results you want. It could likely be done in a multi-step stored procedure, but it would be extremely ugly and not the cleanest solution. And I don't think that you really need to because your label columns are part of the fixed schema.
I think you need to perform a simple select from this table, then iterate through the results in code to build your single line of text per row. You can just concat in the label names at that time.

Resources