Join tables in google sheet - full join - arrays

I have two tables in google spreadsheet. They have a common unique identifier (Account id). Now I need to join these tables into a third table containing all rows from both tables.
Please have a look at this sheet:
or follow the link to an example spreadsheet:
https://docs.google.com/spreadsheets/d/17ka2tS5ysXqJnrpCxCTwNmCsTORPFP1Gatq4p1fPldA/edit?usp=sharing
I have manage to join the tables using this arrayformula:
=ARRAYFORMULA({G3:H8,VLOOKUP(G3:G8,{A3:A7,B3:C7},{2,3},false)})
But with this formula the joined table "misses" two rows:
20 N/A Klaus Berlin
4 VW David Paris
The first missing row is found only in Table 1. The second missing has an ID that is found in Table 2 and has two (2) matching ID's in Table2, but only one row in the joined table
Is there a way to provide a formula that can handle this?

previous answer is incorrect. use:
=ARRAYFORMULA(QUERY(UNIQUE(IFNA({G2:H10,
VLOOKUP(G2:G10, A2:C10, {2, 3}, 0);
VLOOKUP({A3:A10; G3:G10}, {G3:H10; {A3:A10, IF(A3:A10, )}}, {1, 2}, 0), {B3:C10;
VLOOKUP(G3:G10, {A3:C10; {G3:G10, IF(G3:G10, ), IF(G3:G10, )}}, {2, 3}, 0)}})),
"where Col1 is not null order by Col1", 1))

Formula
=ArrayFormula(UNIQUE({
VLOOKUP({A3:A7;G3:G8},{G3:H8;{A3:A7,IF(A3:A7,)}},{1,2},FALSE),
{B3:C7;VLOOKUP(G3:G8,{A3:C7;{G3:G8,IF(G3:G8,),IF(G3:G8,)}},{2,3},FALSE)}
}))
Result
Explanation
{A3:A7;G3:G8} creates a list of all the account ids (it includes duplicates)
{G3:H8;{A3:A7,IF(A3:A7,)}} creates an array having three columns with values from Table 2 and blanks for ids from Table 1
{A3:C7;{G3:G8,IF(G3:G8,),IF(G3:G8,)}} creates an array having three columns with values from Table 1 and blanks for ids from Table 2
The first vlookup populates the first two columns (account id and car)
{B3:C7;VLOOKUP(G3:G8,{A3:C7;{G3:G8,IF(G3:G8,),IF(G3:G8,)}},{2,3},FALSE)} populates the third and fourth columns (name and city)
UNIQUE removes duplicates
Related
Google spreadsheet "=QUERY" join() equivalent function?

Related

Create a cross reference table using Golden record and associate other records to the record

I have a table where I have ranked all the rows based on the created date column and have the rank indicated on the table as below
main table
I would like to create a cross-reference table with the golden record as the recurring column and the other two records as associated records as below.
output desired
I would like to know how I can achieve this using SQL.
I have tried creating a seperate table with all ID numbers (Rank = 1) and then joining it with the main table to get the ones with rank 1,2 and 3 associated with it. But it doesnt seem to work as I intend to.
output
I have not tested but something like this should work. You might want to add a name_id field.
select b.id_number,a.id_number
from table a
join table b on a.name=b.name
where b.rank=1

Summarize rows based on multiple cells

Let's say columns 1-10 are identifiers and 11-15 are values. Identifiers might occur multiple times and I'd like to add up the values in each column. Example:
A1|B1|1|2|
A1|B1|5|3|
A2|B2|3|6|
A2|B2|4|2|
should become:
A1|B1|6|5|
A2|B2|7|8|
This is a straightforward case for a Pivot Table.
Add column headings in the first row: Identifier 1|Identifier 2|Value 1|Value 2.
Select the data (A1 to D5 in this example) and go to Data -> Pivot Table -> Create.
Drag Identifier 1 and Identifier 2 to Row Fields, and Value 1 and Value 2 to Data Fields. Uncheck Total Rows and Total Columns.
The result:

Programming Logic in Storing Multiple table ids in one table

I have Five Tables as Below
1.tblFruits
2.tblGroceries
3.tblVegetables
4.tblPlants
5.tblDescriptions
All the tables except 5th one tblDescriptions will have ids as one column and as primary key and Items as Second Column.
The column in table 1 to table 4 are similar and as follows
ids item_name
Now i want to store description of the items of the four table in the fifth table as below
Desc_Id Description Ids
Now the problem is since i am storing the ids to identify the description of the items in the other four table i might get similar ids when i put ids of four table together.
Let me know the table design for the above requirement
tblDescription
=====================
id | pk_id | description | type
id : auto_generated id of tblDescription
pk_id : foreign key to linked to the tblFruits,tblGroceries.. table
description : the description
type : value either be fruits, groceries,vegetables,plants .To identify the table.
SQL to extract description would be as below:
Select f.item_name, d.description from tblDescription d
inner join tblFruits f on d.pk_id=f.id and d.type='fruits'
inner join tblGroceries g on d.pk_id=g.id and d.type='groceries'
Use Polymorphic Association. As foreign key of your 5th table with description use two columns object_id and object_model.
Example of table content:
Desc_Id Description Object_ID Object_Model
1 'dsferer' 12 `Fruit`
2 `desc2 12 `Vegetable`
2 `descfdfd2 19 `Vegetable`
Remember to add unique index on both columns for performance reasons.
Here you have some article explaining this in PHP
As your tables are similar, the best practice is to combine all of your tables and even description and define row type using a type column.
id name description type
[Fruits, Groceries, Vegetables, Plants]
It's easier to understand and maintain.
But if your tables are different you have two option:
1- use a super table for your types which produce unique IDs which I suggest.
2- use a type row in your description field and define it as primary key beside ID in that table.

Multiple Joins in Grid

I can use join in a grid, but for a single field.
I have the next problem..
I have a table that has two id fields referencig the same field on other table.
Example:
table1 id,name
table2: iduser1,iduser2 (both are fk to id on table1).
I have this values on table 1
id: 1 -> name: user1
id: 2 -> name: user2
and on table2 I have a pair of values 1,2
iduser1: 1
iduser2: 2
If I make a join like this
$g->dq->join('table1','table1.id=table2.iduser1')->field('table1.name iduser1')->field('table1.name iduser2')
$g->addColumn('text','iduser1');
$g->addColumn('text','iduser2');
The result is name of user1 twice on the grid, but not user1,user2
I have tested to add another join() but without success.
Can I have some help or maybe some direction about what I'm doing wrong ?
Thanks a lot
You can perform two joins.
// primary table
$grid->setSource('table1');
// Joins all tables
$grid->dq->join('table2','table2.iduser1=table1')
->join('table1 user2');
// This is how you add fields from linked table
$grid->dq->field('user2.name name2');
$grid->addColumn('text,'name2','Linked User');

Sql query to check combinations and match it to the given combination

I've a scenario in Sql where I've following schema.
If I have 3 items in the Item table then one unique combination of all the items will be assigned to a user. For ex:
Items:
1
2
3
Then combinations will be: {1}, {2}, {3}, {1,2}, {1,2,3}, {1,3}, {2,3) all are unique combinations.
Any of these combination will be assigned to a single user.
Now I want to find out given combination belongs to which user, how can I find that? For ex: I'll pass items list {2,3} then it should return the userid who is having that combination from the table UserItemCombinations. {2,3} is passed as comma separated string to a SP. I've taken 3 items as example, this table may contain n number of items. Users number will be dependent on the number of combinations. For ex: For three items there are 7 combinations so 7 users will be there user table.
UserItemCombinations will have one row for each user-item, and one user can have only one combination, so if the query combination is {2,3}
select userid from user where userid not in
(select distinct userid from UserItemCombinations where itemid in
(select itemid from item where itemid not in (2,3));
If there's not too much updates in UserItemCombinations and the performance of the desired query is critical enough, you would make additional field in User table, i.e. Items and create SP that fills in those values per every user. Stored proc will select sorted items per each user in loop and concatenate them into one string to put in User.Items field. You can also make trigger on UserItemCombinations for INSERT, UPDATE, DELETE and recalc the value again.
You may also create index on that field.
select userid from UserItemCombinations where itemid in (2,3) and itemid not in
(select itemid from Item where itemid not in (2,3));

Resources