0

I have two large tables - Table_A and Table_B - that I want to join on the ID field. "ID" in Table_A is a column and "IDs" in Table_B is an array

Table_A:

ID  | City       | 
----+------------+
101 | London     |     
102 | Paris      | 
103 | Rome       | 
104 | Copenhagen | 
105 | Amsterdam  | 
106 | Berlin     | 
107 | Cardiff    | 
108 | Lisbon     | 

Table_B:

Date  | Sessions | IDs
------+----------+--------------
06-02 | 1        | [107,102]    
06-03 | 1        | [103]  
11-12 | 1        | [105,107,103]  
27-06 | 1        | [104,108]  
31-01 | 1        | [105]  
22-04 | 1        | [106,102]  
08-07 | 1        | [101,105,108]  
02-10 | 1        | [105]  

Desirable Output:

Date  | Sessions | ID          | City
------+----------+-------------+-------------
06-02 | 1        | 107         | Cardiff
      |          | 102         | Paris
06-03 | 1        | 103         | Rome
11-12 | 1        | 105         | Amsterdam
      |          | 107         | Cardiff
      |          | 103         | Rome
27-06 | 1        | 104         | Copenhagen
      |          | 108         | Lisbon
            ...

I have tried using inner joins with unnest and union all but nothing is working. Any help would be appreciated.

1
  • Show what you tried Commented Nov 18, 2022 at 15:43

2 Answers 2

1

Something along those lines should yield the result you are looking for

  select 
    date, 
    sessions, 
    array_agg(id_un) as id, 
    array_agg(city) as city 
  from table_b b, unnest (id) as id_un
  left join table_a a on id_un = a.id
  group by 1, 2

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Xavier, however I'm getting a "No matching signature for operator = for argument types: STRING, INT64. Supported signature: ANY = ANY at..." when I attempted this. How can I get around this?
Hmm can you try: left join table_a a on id_un = cast(a.id as string)
1

Consider also below approach

select date, sessions, ids as id, 
  array(
    select city
    from b.ids id
    left join Table_A
    using(id)
  ) city
from Table_B b              

if applied to sample data in your question - output is

enter image description here

7 Comments

I'm getting the error "Column id in USING clause not found on right side of join at ...". What should I do when this appears?
make sure you have 'id' column in table_a as it is presented in your sample data
I've done that, but it's still returning the same error message
as you can see - i was able produce expected result using your sample data! so the only i can suggest is to carefully check the query you ended up using, most likely you did some adjustment, etc.
Also, in my actual data, the id column in table a and array column in table b have the same column name, would I need to do anything different because of this?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.