I am currently successfully union-ing two tables and joining on a third.
select col1,col2
from table1
union
select col1,col2
from table2
join(select distinct(id), date from table3) on table3.id = table2.id
how do I only union table1 and table2 where table1.col3 is in table2.id?
In other words if there is a value in table1.col3, I want to get all the data from table2 where there is a match on table2.id and union the tables.
edit:
table1 (source table) table2 table3
id col1 col2 id col1 col2 id date col4
I want all records from table2
I want to get records from table1 where there is match between table2.col1 and table1.id
I want to get records from table1 where there is a match between table3.id and table1.id where date >='2018-12-01'
Notes: table1 and table2 are exactly the same, why i went with union.
Your requested UNION actually makes for a fairly simple query since it can be built from 3 separate simple queries from your requirements;
SELECT col1, col2 FROM table2
UNION ALL
SELECT col1, col2 FROM table1 WHERE id IN (SELECT col1 FROM table2)
UNION ALL
SELECT col1, col2 FROM table1 WHERE id IN (SELECT id FROM table3 WHERE date >= '2018-12-01')
Note that UNION ALL allows for duplicates (a single row may show up once for each query) while changing to UNION will remove all duplicates.
A runnable dbfiddle example
Related
I wish to join few columns in a importrange,
i am using this formula here
=importrange("URL", "A1:J3") "i would like to join A, D, H here"; importrange("URL", "L1:Z3") "and to join M, P here"
Do you want to concatenate row by row those columns of the imported ranges?
Something like this?? Used INDEX to refer to the columns in each row. If needed you can change CONCATENATE with JOIN or TEXTJOIN to add some separator:
=BYROW(IMPORTRANGE(URL,"A1:J3"),LAMBDA(each, CONCATENATE(INDEX(each,,1),INDEX(each,,4),INDEX(each,,8))))
In Google Sheets, you can use the QUERY function to join specific columns from multiple IMPORTRANGE functions as I previously described.
I have added the code of joining A, D, H from the first IMPORTRANGE function, and columns M, P from the second IMPORTRANGE function:
=QUERY({IMPORTRANGE("URL", "A1:J3"), IMPORTRANGE("URL", "L1:Z3")}, "SELECT Col1, Col4, Col8, Col13, Col16")
I can't find a way to simplify a query to get a similar result to a pivot.
Although a pivot table is easy, I would prefer to use a query because I can then personalise headers.
Here's the link with the data :
https://docs.google.com/spreadsheets/d/1R461nv2lD4efpuUuDonFMYGnLH510n7gwpbzG2WAUaw/edit?usp=sharing.
This is the working embedded queries :
= query(query(Data!A1:C, " SELECT B,count(B) WHERE B is not null GROUP BY B,C ORDER BY B ASC "), "SELECT Col1, Count(Col1) group by Col1 LABEL Col1 'Status',Count(Col1) 'Count'",1 )
This is the query I'd like to use with a pivot clause :
=query(Data!A1:C, "Select B, count(C) group by count(C) Pivot C" ,1)
Here's what I'm looking for :
Try
={"Status","Count(Categories)";transpose(query(unique(B:C),"select count(Col2) where Col2 is not null pivot Col1",1))}
or simplier
=query(unique(B:C),"select Col1, count(Col2) where Col2 is not null group by Col1",1)
if you want to pivot C column it should be:
=QUERY({Data!A1:C, Data!C1:C},
"select Col2,count(Col3) where Col1 is not null group by Col2 pivot Col4", 1)
I have a big table with repeating dates in Column A and some repeating names in Column B. I need to count how many A there are in each month if we count one A for one date. So if there are two A's for 01.06.2021, so it will be counted as 1. The list of A, B, C contains about 20 names.
try:
=INDEX(REGEXREPLACE(""&QUERY(UNIQUE({A:B\ TEXT(A:A; "mm\×mmmm yyyy")});
"select Col2, count(Col2) where Col2 is not null group by Col2 pivot Col3");
"(^\d+×)"; ))
or if you need numeric numbers:
=INDEX(IFERROR(REGEXREPLACE(QUERY(UNIQUE({A:B\ TEXT(A:A; "mm\×mmmm")});
"select Col2, count(Col2) where Col2 is not null group by Col2 pivot Col3");
"(^\d+×)"; ); QUERY(UNIQUE({A:B\ TEXT(A:A; "mm\×mmmm")});
"select Col2, count(Col2) where Col2 is not null group by Col2 pivot Col3")))
I know that it is not possible to "order by" after a pivot in query function, that is why I apply a nested query.
=QUERY(
QUERY(Dataset,"select B,count(B) where A>date'2019-10-01' group by B pivot year(A),month(A)",1),
"Select * order by Col2 Desc ",1)
However the result is not sorted by count(B) (Col2 of the inner query) as a total.
It is sorted only by the Col2 generated because of the pivot. The final table is sorted incorrectly (Option 10 should be in fifth place and not at the bottom).
Example of the result
Any workaround to solve this?
Instead of a second query use the SORT function outside the first query.
Try something like:
=SORT(QUERY(Dataset,"select B,count(B) where A>date'2019-10-01' group by B pivot year(A),month(A)",1),2,0)
I have table1 (col1,col2) and table2(col1,col2) as given below/
Now i need to replace values of col2 of table1 with corresponding value of col1 and col2 from table2. So that the final table should look like this. how can we do this in query??
I assume table1.col2 and table2.col2 have the same text type(?)
update table1 set table1.col2=table2.col2
from table1
join table2 on (table1.col2=table2.col1)